FIRST PHP PROGRAM

php logoLet’s start by deleting the contents of the htdocs folder located under the path C:\xampp\htdocs. The PHP web server when we connect with the browser at http://localhost or http://127.0.0.1 is actually accessing the htdocs folder. Once the content has been deleted, we create a corsi folder inside htdocs and nest a php folder. Now let’s open V.S Code and connect the php folder. We are now ready to write our first PHP program. We create a file and call it index.php, the name index is not random; in fact, the Apache web server will automatically search for either an index.php or index.html file. In the created file we build the structure of an HTML page; in fact, remember that a PHP file can contain HTML, CSS and Javascript. If we want to write PHP instructions we have to open them with the tag.

Code Hello World

Let’s connect with the browser at the address http://localhost/corsi/php/index.php.

Hello world

As I told you before, Apache looks for a .php or .html file called index. Let’s try to remove the last part of the URL, ie index.php. http://localhost/corsi/php. We will always get Hello World for the reason explained. If we return to the editor and rename index.php to index1.php this is what we get:

Directory course

If we want to run the index1.php script, just click on it.

ABBREVIATED SYNTAX

We can rewrite the previous script in the following abbreviated form.

Syntax abbreviation

COMMENTS IN PHP

Let’s delete the HTML code at index.php. Each programming language provides characters to comment the code. In PHP these characters are: // and # for single-line comments, / * * / for multi-line comments.

Comments

VARIABLES IN PHP

A PHP application is a set of many small elements that combined together give life to our projects. The values of an application must be stored in the memory of the device, of course we must be able when we need to recover these values. We store the values in memory and retrieve them thanks to the variables.

Variables

Technically we have carried out the assignment of a value of 28 to the variable $anni. In case we want to perform a quick test on the script we can use the terminal. In addition to the integrated terminal we can use the PHP interactive shell, just give the command php -a. To exit, use the exit command.

Interactive shell

When defining a variable there are some important ground rules to follow. All variables in PHP must start with a dollar sign. A variable name cannot start with a number, but it can start with a letter or an underscore $_a is perfectly legal, numbers may exist but inside the variable name $_123a is still valid. Variable names are case sensitive $a is different from $A.

NAMES OF VARIABLES

It is advisable to use variable names relevant to the purpose of the variable itself, if we need a variable years we declare it as $ years rather than $abc. When a variable is made up of several words, we make the words following the first begin with a capital letter (Camel Case notation), for example $yearsUser. Another way to separate words is by using an underscore $user_years is perfectly acceptable. PHP is a weakly typed language, in fact it is not necessary to specify the data type of a variable, moreover we can assign a value to a variable and then use the same variable with a different type.

Variables

ASSIGNMENT BY VALUE AND BY REFERENCE

An assignment by value is the classic assignment we have done so far.

assignment by value

If we want $nome1 and $nome2 to refer to the same memory area, we make an assignment by reference. To do this, it is preceded by the $ a &. After the assignment by reference the 2 variables point to the same memory area. If we change one reference, the change will also be reflected in the other reference.

assignment by reference

THE CONSTANTS

We may need to have values that subsequently cannot be changed in the script. In this case we are talking about constants. To define a constant we have several methods available, for example we can use a function.

Constant

We can define constants even more easily with the const keyword. There are differences between using function syntax and the const keyword. Constants defined with const are created at compile time, those with define at Runtime. When we execute a script there are two phases, a first compilation phase in which the code is evaluated and the second in which the code is actually executed. In practice this has consequences in the sense that we cannot define a constant with const within the control structures.

Constant

THE DYNAMIC CONSTANTS

Constant

MENTION TO THE FUNCTIONS

A PHP script is interpreted from top to bottom, however we can define blocks (functions) where we decide the moment of execution.

Function

Once a function has been created, we can invoke it several times, move the point where it is called etc. It is not a good practice to have functions with many instructions inside, in this case we break the function into several parts. A function can take input values and return an output value. When we pass values to functions we are talking about function arguments while $valore is called function parameter.

Instead of echo the $valore variable, if we return it with the return statement, the function will output a value.

Function

VAR_DUMP AND ISSET FUNCTIONS

A native PHP function that we will often use is var_dump which checks both the type and the value of a variable. With isset we can know if a variable is declared and has a value other than null;

funzione var_dump
function isset

The isset test returns false if a variable has not been declared or its value is null. If we just want to know the type of a variable we use gettype.

function gettype

LINKS TO PREVIOUS POST

THE PHP LANGUAGE