php syntax

PHP Syntax

In the What is PHP, it was said that PHP is a server-side scripting language that is standardly written inside HTML codes and returns to the user. PHP syntax is so simple that even beginner programmers can understand it.

All PHP codes are placed inside <?php and ?>.

<?php
//code goes here
?>

PHP codes must be placed inside the file with .php extension to be executed.

As we said, PHP codes are placed inside HTML codes.

<!DOCTYPE html>
<html>
<body>
    <?php echo "Hello World!"; ?>
</body>
</html>

Of course, PHP codes can be executed without HTML codes, but they are written inside HTML tags as a standard.

<?php 
    echo "Hello World!"; 
?>

PHP codes are also like C or C++ ends with ;.

Predefined expressions such as if, for, and… are not case-sensitive.

<?php
echo "Honar Systems </br>";
ECHO "Honar Systems </br>";
eCho "Honar Systems";
?>
----------------
Honar Systems
Honar Systems
Honar Systems

But the variable names are case sensitive.

$var="Honar Systems";
ECHO $Var;
-------------------
Error: Undefined variable: Var

Comments in PHP syntax

In this language, you can easily comment wherever you need. Comments are not implemented in PHP and are written only so that in the future, in case of development, their information can be used to understand the code.

PHP supports all types of comments.

// This is a single-line comment
echo "a</br>";

# This is also a single-line comment
echo "b</br>";

/*
This is a multiple-lines comment block
that spans over multiple
lines
*/
echo "c</br>";

// You can also use comments to leave out parts of a code line
$val = 5 /* + 15 */ + 5;
echo $val;
-----------------------
a
b
c
10

As you can see, the comments have not been executed.

PHP Variables

Variables are used to store data temporarily. In PHP, variables can be defined anywhere in the code.

Variable defining

To define a variable in PHP, we add the $ sign to the beginning of the variable name.

$val = "Honar Systems";
$val2 = 10;

Unlike other programming languages, PHP has no command to declare a variable type. All variables are converted to their data type at the moment of definition.

Rules for defining variables:

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

PHP syntax is such that we can define a variable without defining its type, and we can even change its type without causing an error.

$val = "Honar Systems";
echo $val . "</br>";
$val = 5 + 5;
echo $val;
-------------
Honar Systems
10

In PHP 7, the data type definition has been added so that by activating strict mode, if the data types are different, an error will occur.

If a variable is defined but its value is not specified, its value is automatically set to NULL.

PHP variables scope

The scope of the variable is the area in which each variable is valid and should not be used outside of it, otherwise, it will cause an error. In PHP we have three types of variable scope:

  • local
  • global
  • static

local scope and global

Local variables are valid in that area relative to the place of definition. In general, it can be said that if a variable is defined between { }, it is valid inside the same brackets and the variable cannot be used outside of them.

The variable that is defined at the beginning of the PHP file is valid only in the same file and the appended files after it.

$g_var = "A";

function print_var(){
    $l_var = "B";
    echo $l_var;
}

if(true){
    $l_var2 = "C";
    echo $g_var . $l_var2;
}

echo $g_var . print_var();
----------------
ACBA

The $g_var variable is known as a global variable. It can be used anywhere in the file, but inside the functions, it must be sent to the function as a parameter to use them.

$l_var variable is accessible only inside the function (local variable). Also, the variable $l_var2 can be accessed inside if. But global variables can be used inside if, for, etc.

In this example, we chose the variables $l_var and $l_var2 to be specified with different names, but you can choose them with the same name and there will be no conflict because these variables are local.

The global word in PHP syntax

In the previous example, we cannot use the variable $g_var inside the function. But this is possible by using the global word.

$g_var = "A";

function print_var()
{
    global $g_var;
    $g_var = "B";
    echo $g_var;
}

echo print_var();
---------------
B

Inside the function, we define the global word along with the variable name outside the function. Make sure that the name of the variable inside the function is exactly the same as the name of the variable outside the function. Also, it is not possible to set the value in the global definition, it is possible to set the value in the next line. For example, the following code is incorrect.

global $g_var = "B";
-----------
Error

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

$g_var1 = "A";
$g_var2 = "B";

function print_var()
{
    echo $GLOBALS['g_var1'] . $GLOBALS['g_var2'];
}

print_var();
----------------
AB

static

In the functions, after the execution of the code, the values of the variables are deleted. Sometimes we need these values to be preserved. For this, we use static variables.

function my_func()
{
    static $x = 1;
    print $x . "</br>";
    $x++;
}

my_func();
my_func();
my_func();
-----------------
1
2
3

The $x variable is a local variable to the function and cannot be used outside the function.

Shopping Cart