PHP - tutorial - 01 - basics

revision:


basic PHP syntax

A <PHP script> can be placed anywhere in the document and starts with <?php and ends with ?>.

Syntax:

        <?php
            // PHP code goes here
        ?>
    

The default file extension for PHP files is ".php". A PHP file normally contains HTML tags, and some PHP scripting code. PHP statements end with a semicolon (;).

example

My first PHP page

Hello World!
code:
                <!DOCTYPE html>
                <html>
                <body>
                    <h3>My first PHP page< /h3>
                    <?php
                        echo "Hello World!";
                    ? >
                </body>
                </html>
            

In PHP, keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-defined functions are not case-sensitive. However, all variable names are case-sensitive!


comments in PHP

A comment in PHP code is a line that is not executed as a part of the program. Its only purpose is to be read by someone who is looking at the code.

PHP supports several ways of commenting:

        < ? php
            //: this is a single-line 

            #: this is also a single-line 

            /*
            this is a multiple-lines comment block
            that spans over multiple
            lines
            */

            // You can also use comments to leave out parts of a code line
            $x = 5 /* + 15 */ + 5;
        ? >
    

variables

Variables are "containers" for storing information.

In PHP, a variable starts with the $ sign, followed by the name of the variable.

examples:

        < ?php
        $txt = "Hello world!";
        $x = 5;
        $y = 10.5;
        ? >
    

When you assign a text value to a variable, put quotes around the value.

PHP has no command for declaring a variable. It is created the moment you first assign a value to it.

Following rules apply on PHP variables:

- a variable starts with the $ sign, followed by the name of the variable;
- a variable name must start with a letter or the underscore character (_);
- a variable name cannot start with a number;
- a variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ );
- variable names are case-sensitive ($age and $AGE are two different variables).

The PHP echo statement is often used to output data to the screen.

example:

        < ?php
        $txt = "my website";
        echo "I love $txt!";
        ? >
    

Variable scope

The scope of a variable is the part of the script where the variable can be referenced/used. PHP has three different variable scopes: local, global, static

A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function.

A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function.

You can have local variables with the same name in different functions, because local variables are only recognized by the function, in which they are declared.

The "global" keyword is used to access a global variable from within a function. To do this, use the "global" keyword before the variables (inside the function).

PHP also stores all global variables in an array called $GLOBALS[index]. The index holds the name of the variable.
This array is also accessible from within functions and can be used to update global variables directly.

Normally, when a function is completed/executed, all of its variables are deleted. However, sometimes we want a local variable NOT to be deleted. To do this, use the "static" keyword when you first declare the variable.


PHP echo and print statements

With PHP, there are two basic ways to get output: echo and print. "echo" and "print" are more or less the same. They are both used to output data to the screen. The differences are small:

- return : echo has no return value, while print has a return value of 1, so it can be used in expressions.
- parameters : echo can take multiple parameters (although such usage is rare) while print can take one argument.
- speed : echo is marginally faster than print.

The echo statement can be used with or without parentheses: echo or echo(). It can be used to display text and to display variables.

The print statement can be used with or without parentheses: print or print(). It can be used to display text and to display variables.