DEV Community

Cover image for Modern PHP crash course
Eric The Coder
Eric The Coder

Posted on • Updated on

Modern PHP crash course

Welcome to this modern PHP crash course. Why modern? PHP has been around since 1994 and has evolved considerably since then. Now, at version 8.1, PHP is a complete language and perfectly suited to modern web development.

In this crash course we will cover all the important aspects to know about PHP in 2022.

Once the course is finished, you will be able to create your first PHP applications and also you will have sufficient knowledge in PHP to continue your training with a PHP framework like Laravel and / or Symfony.

The crash course is free and will be posted here on dev.to. I'll be releasing a new article/post every two days or so. To not miss anything, you can follow me on twitter: Follow @EricTheCoder_

What is PHP? Why PHP?

PHP is an object oriented server side programming language. That is used to develop web applications.

PHP is the most widely used server programming language on the web today. Just under 80% of websites have PHP code. It's enormous! PHP is not going to disappear anytime soon.

PHP has evolved a lot in recent years. The addition of object oriented and several other modern concepts have dramatically changed the way PHP web applications are coded.

PHP is now used by millions of websites. Including Facebook, Wordpress, Wikipedia, Tumblr, Slack and many more.

The PHP community is very large, very dynamic and inclusive.

PHP can also rely on high quality frameworks like Symfony and Laravel (and several others). These frameworks allow you to develop web applications quickly and reliably.

In short, with PHP you can carry out small, big and very big projects. PHP can handle a few clicks per day or millions of clicks per day.

PHP Installation

Installing PHP can be tricky at times, don't be put off by this task.

If you want to start the training right away and do the installation later, it is possible to use the site https://replit.com/languages/php7 in order to execute PHP code from your browser.

Install PHP on your computer

You will need PHP version 8.1

To check which version is installed on your machine, you can

run this command from the terminal:

$ php -v
Enter fullscreen mode Exit fullscreen mode

If PHP is missing or if the installed version is lower than PHP 8.1 you will need new install:

Here are the link for PHP installation:

MacOS (hombrew): https://www.php.net/manual/en/install.macosx.packages.php

Windows (XAMPP): https://www.apachefriends.org/index.html

For more details, see the official PHP documentation: https://www.php.net/manual/fr/install.php

PHP Basic concepts

For this tutorial we will create a folder to place all our files for the examples to come.

$ mkdir demo-php
$ cd demo-php
Enter fullscreen mode Exit fullscreen mode

From this folder we will create a file named index.php

$ code index.php
Enter fullscreen mode Exit fullscreen mode

This command will open vscode and create an index.php file.

Note that for this crash course we will be using Visual Studio Code but this is not a prerequisite. You are free to use your favorite editor.

On the first line of the file type

<?php
Enter fullscreen mode Exit fullscreen mode

This tag will tell the PHP server that all subsequent lines are PHP code that the server should execute.

So we can now start writing PHP code and to start, nothing better than the classic Hello World!

<?php

echo 'Hello World';
Enter fullscreen mode Exit fullscreen mode

The echo function allow text and even html to be display in the browser current page.

To test this code you can run the PHP web server which is included with PHP

$ php -S localhost:5000
Enter fullscreen mode Exit fullscreen mode

The -S option starts the web server on localhost:5000

If you open a browser and go to the address http://localhost:5000, the PHP server will render the default file, index.php so you will see:

Hello World
Enter fullscreen mode Exit fullscreen mode

HTML and PHP

Note that in a PHP file it is possible to have HTML codes mix with PHP codes

<h1>
    <?php echo 'Hello World'; ?>
</h1>
Enter fullscreen mode Exit fullscreen mode

This code is perfectly valid. The ?> tag indicates the end of the PHP code.

It would also be possible to have several sections of PHP code

<h1>
    <?php echo 'Hello World'; ?>
</h1>

<h2>
    <?php echo 'Sub title'; ?>
</h2>
Enter fullscreen mode Exit fullscreen mode

Although PHP can be mixed with HTML, it is considered good practice to separate PHP code and HTML code each into its own file. We will come back to this concept later.

Variables

Variables are used to store different values ​​that you want to process in your code.

Once the variable is stored in program memory, it can be used later.

For example, suppose you want to store the username, you can use a variable and give it the name: $name and set its content equal to 'Mike Taylor'

$name = 'Mike Taylor';
Enter fullscreen mode Exit fullscreen mode

Note that in PHP:

  • The name of a variable begins with a $ sign
  • A character string is surrounded by quotes.
  • We do not have to specify the type of variable (eg: string). PHP is an interpreted language. It automatically recognizes the data type based on the value stored in it.
  • On the other hand, it is possible to specify the type of the variable when declaring it. We will see this concept a little later.

Here are the 4 basic types of PHP variables and the syntax to create them

// string
$fullName = 'Mike Taylor';

// integer 
$count = 20;

// float (decimal number)
$bookPrice = 15.80;

// booleans
$isActive = true;
$isAdminUser = false;
Enter fullscreen mode Exit fullscreen mode

PHP also has more advanced types of variables like arrays and classes. We'll cover all of this in detail later.

Finally, PHP also has some special types, like “null” which means no values.

At this point you should be able to understand that these two variables do not have the same content.

$value = '100';

$value2 = 100;
Enter fullscreen mode Exit fullscreen mode

$value is a string variable which contains de text ‘100’ as content. This variable is not a number that can be used in a mathematical operation.

$value2 is an integer variable which has the number 100 as content. This variable is a number and can therefore be used for mathematical operations.

Constant

Constants, are variables whose value cannot be changed in any way. There are two ways to create them.

const MAX_USERS = 50;

define('MAX_USERS', 50);
Enter fullscreen mode Exit fullscreen mode

A constant does not start with a $ sign and by convention, constants are defined in uppercase letters. After creation, if you try to assign another value to it, PHP will give you an error.

Typecasting

PHP automatically determines the type of the variable at runtime.

Output

In PHP there are several ways to display the content of a variable, here are some of them:

  • echo $name; (Displays the value)
  • var_dump($isAdmin); (Displays the type of the variable as well as the value)
  • print_r($items); (Displays the value legibly)

Determine the type of a variable

It is possible to get the type of a variable with the gettype() function

echo gettype($name); // string
echo gettype(20); // integer
echo gettype(9.95); // double
echo gettype(true); // boolean
Enter fullscreen mode Exit fullscreen mode

We can also check if the variable is of a certain type with the functions is_init(), is_float(), is_string() and is_bool()

echo is_int($name); // false
echo is_float(12.5); // true
echo is_string($name); // true
echo is_bool(10); // false
Enter fullscreen mode Exit fullscreen mode

Force type (cast)

It is possible to force PHP to use a particular type and if necessary force the conversion. To do this, you must add the type in parentheses before the variable or the function.

$age = (int)readline('Your age:');
echo 'Your age is'. (string)$age;
Enter fullscreen mode Exit fullscreen mode

Here the cast (int) will force an integer as the return value of the readline() function. The cast (string)$age will convert the integer to a string before displaying it.

Comments

To leave a comment in the code. That is to say a line which will not be executed, you can use one of the following two techniques:

// This is a single line comment

/*
This is a comment
on several lines
*/
Enter fullscreen mode Exit fullscreen mode

Naming convention

Here are some conventions that we will use to create our code:

$firstName = 'Mike';  // variables name will be camelCase
function updateProduct() // functions name will be camelCase
class ProductItem // class name will be StudlyCaps
const ACCESS_KEY = '123abc'; // all upper case with underscore separators
Enter fullscreen mode Exit fullscreen mode

For more details on naming conventions yon can check the PRS web site: https://www.php-fig.org/psr/

Conclusion

That's it for today, I'll be releasing a new article every two days or so. To be sure not to miss anything you can follow me on twitter: Follow @EricTheCoder_

Discussion (6)

Collapse
david_barclay_29e0974a441 profile image
David Barclay

What’s modern about this?

Collapse
dopitz profile image
Daniel O. • Edited on

In the multiline comment is a syntax error in / * and * /. It should be start with /* and end with */.

Collapse
ericchapman profile image
Eric The Coder Author

Ooups… Thanks

Collapse
peter279k profile image
peter279k

The following code snippets are not correct:

$ age = (int)readline('Your age:');
Enter fullscreen mode Exit fullscreen mode

It should change into:

$age = (int)readline('Your age:');
Enter fullscreen mode Exit fullscreen mode
Collapse
ericchapman profile image
Eric The Coder Author

Thanks for your contructive comment