PHP If Else
PHP if else statement is used to test condition. There are various ways to use if statement in PHP.
o if
o if-else
o if-else-if
o nested if
PHP If Statement
PHP if statement allows conditional execution of code. It is executed if condition is true.
If statement is used to executes the block of code exist inside the if statement only if the specified condition is true.
Syntax
1. if(condition){
2. //code to be executed
3. }
PHP If-else Statement
PHP if-else statement is executed whether condition is true or false.
If-else statement is slightly different from if statement. It executes one block of code if the specified condition is true and another block of code if
the condition is false.
Syntax
1. if(condition){
2. //code to be executed if true
3. }else{
4. //code to be executed if false
5. }
PHP If-else-if Statement
The PHP if-else-if is a special statement used to combine multiple if?.else statements. So, we can check multiple conditions using this state ment.
Syntax
1. if (condition1){
2. //code to be executed if condition1 is true
3. } elseif (condition2){
4. //code to be executed if condition2 is true
5. } elseif (condition3){
6. //code to be executed if condition3 is true
7. ....
8. } else{
9. //code to be executed if all given conditions are false
10. }
PHP nested if Statement
The nested if statement contains the if block inside another if block. The inner if statement executes only when specified condition in outer if
statement is true.
Syntax
1. if (condition) {
2. //code to be executed if condition is true
3. if (condition) {
4. //code to be executed if condition is true
5. }
6. }
PHP For Loop
PHP for loop can be used to traverse set of code for the specified number of times.
It should be used if the number of iterations is known otherwise use while loop. This means for loop is used when you already know how many
times you want to execute a block of code.
It allows users to put all the loop related statements in one place. See in the syntax given below:
Syntax
1. for(initialization; condition; increment/decrement){
2. //code to be executed
3. }
PHP While Loop
PHP while loop can be used to traverse set of code like for loop. The while loop executes a block of code repeatedly until th e condition is FALSE.
Once the condition gets FALSE, it exits from the body of loop.
It should be used if the number of iterations is not known.
The while loop is also called an Entry control loop because the condition is checked before entering the loop body. This means that first the
condition is checked. If the condition is true, the block of code will be executed.
Syntax
1. while(condition){
2. //code to be executed
3. }
PHP do-while loop
PHP do-while loop can be used to traverse set of code like php while loop. The PHP do-while loop is guaranteed to run at least once.
The PHP do-while loop is used to execute a set of code of the program several times. If you have to execute the loop at least once and the number
of iterations is not even fixed, it is recommended to use the do-while loop.
It executes the code at least one time always because the condition is checked after executing the code.
The do-while loop is very much similar to the while loop except the condition check. The main difference between both loops is that while loop
checks the condition at the beginning, whereas do-while loop checks the condition at the end of the loop.
Syntax
1. do{
2. //code to be executed
3. }while(condition);
PHP foreach loop
The foreach loop is used to traverse the array elements. It works only on array and object. It will issue an error if you try to use it with the variables
of different datatype.
The foreach loop works on elements basis rather than index. It provides an easiest way to iterate the elements of an array.
In foreach loop, we don't need to increment the value.
Syntax
1. foreach ($array as $value) {
2. //code to be executed
3. }
PHP Jumping Statements
PHP Jumping Statements are the statements that is used to jump the controller from one line into another line, inside a progr am.
PHP Break Statement
PHP break is a keyword, which is used to terminate the execution of a loop.
Example 1: PHP Break Statement
<html>
<body>
<?php
$i=1;
while($i<=10)
{
if($i==5)
break;
echo "While Loop $i<br>";
$i++;
}
?>
</body>
</html>
While Loop 1
While Loop 2
While Loop 3
While Loop 4
PHP Continue Statement
PHP Continue statement is used to skip the current iteration of a loop but it does not terminates the loop.
Example 2: PHP Continue Statement
<html>
<body>
<?php
$i=1;
while($i<=10)
{
if($i==5)
continue;
echo "While Loop $i<br>";
$i++;
}
?>
</body>
</html>
While Loop 1
While Loop 2
While Loop 3
While Loop 4
While Loop 6
While Loop 7
While Loop 8
While Loop 9
While Loop 10
PHP Goto Statement
PHP goto statement is used to send the controller to a certain location in the program. The location is specified by a user d efined label.
Example 3: PHP goto Statement
<?php
echo "<div>First Line</div>";
goto bottom;
echo "<div>Second Line</div>";
bottom:
echo "<div>Third Line</div>";
?>
First Line
Third Line
PHP Arrays
PHP array is an ordered map (contains value on the basis of key). It is used to hold multiple values of similar
type in a single variable.
There are three different kind of arrays and each array value is accessed using an ID c which is called array index.
• Numeric array − An array with a numeric index. Values are stored and accessed in linear fashion.
• Associative array − An array with strings as index. This stores element values in association with key values rather than in a strict
linear index order.
• Multidimensional array − An array containing one or more arrays and values are accessed using multiple indices
Numeric Array
These arrays can store numbers, strings and any object but their index will be represented by numbers. By
default array index starts from zero.
Associative Arrays
The associative arrays are very similar to numeric arrays in term of functionality but they are different in terms
of their index. Associative array will have their index as string so that you can establish a strong association
between key and values.
To store the salaries of employees in an array, a numerically indexed array would not be the best choice.
Instead, we could use the employees names as the keys in our associative array, and the value would be their
respective salary.
Multidimensional Arrays
A multi-dimensional array each element in the main array can also be an array. And each element in the sub-
array can be an array, and so on. Values in the multi-dimensional array are accessed using multiple index.
.