It is now time to learn how you make things work in php, with basic statements in PHP.
There are several types of statements in PHP programing. We will negotiate the basics in this article: declaration statements, expression statements, selection statements and iteration statements.
-
Declaration statements
Declaration statements introduce in your script new variables, or constants. Variables, can also have assigned values in the declaration, and constants require assigning a value.
1 2 3 4 5 6 7 8 9 10 11 12 | <?php // declaration without assignment var $someVariable; // declaration with assignment var $otherVariable=2; // constant defining define("SOMECONSTANT", "value of constant"); ?> |
-
Expression statements
The statement that calculate a value, and must store it in a value.
1 2 3 4 5 6 7 8 9 10 | <?php // assignments $sum = 2 + 8; $newSum = $sum * 6; // Error. Value not assigned to any variable $sum - 4; ?> |
-
Selection statements
These statements allow you to run specific sections of code, depending on certain conditions, evaluated to boolean TRUE or FALSE (1 or respectively 0)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | <?php // how to use if statement if (1 == 1) { // code to execute if condition is true $var = "condition is true"; }else { // code to execute if condition is false $var = "condition is false"; } echo $var; // will output condition is true $var = "some value"; if ($var == "value") { $var = "modified value"; } echo $var // will output some value $var = "some value"; if ($var == "some value") { $var = "modified value"; } echo $var // will output modified value if ($var == "modified value") $var = 1; echo $var; ?> |
Note: the code between { and } is called a block of code, and tells the parser that the instructions are grouped into a block. If a block is next to a iterative or decisional statement, the entire block is executed. If the block isn’t present, only the first line of code is considered to be a part of the structure, the rest being parsed normally.
The if statement evaluates the condition between ( and ), and if true executes the block of code between { and }. The else keyword is optional, and introduces a block of code that is executed if the condition is evaluated to false.
-
Iteration statements
These statements allow to to run sections of code in a repetitive way and controlled by conditions (evaluated to TRUE or FALSE) that need to be met.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?php // how to use the for statement for ($i=0; $i<=5; $i++) { // some code echo $i.'<br />'; } // how to use the while statement while ($i<5) { // some code echo 'code executed because '.$i.' < 5<br />'; } ?> |
The for statement is an iterative operation, it executes the code block for a known number of steps, in this care, six steps, incrementing at each step the $i variable (because of $i++ inside the for). The code is executed as long as the condition in the middle ($i<=5) is TRUE.
The while statement is an iterative operation with unknown number of steps, and is executed while the condition between parenthesis is TRUE.
These are the basic PHP statements. We will discuss each one in detail.




