Php
- Selection Structure
- if statement - executes some code if one condition is true
- if statement - executes some code if one condition is true
Syntax
if (condition) {
code to be executed if condition is true;
}
if (condition) {
code to be executed if condition is true;
}
code to be executed if condition is true;
}
Example:
<?php
if ($a > $b)
echo "a is bigger than b";
?>
<?php
if ($a > $b)
echo "a is bigger than b";
?>
- if... else statement - executes some code if a condition is true and another code if that condition is false
- if... else statement - executes some code if a condition is true and another code if that condition is false
Syntax
if (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;}
Example
<?php
if ($a > $b) {
echo "a is greater than b";
} else {
echo "a is NOT greater than b";
}?>
- if... elseif... else statement - executes different codes for more than two conditions
if (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;}
if ($a > $b) {
echo "a is greater than b";
} else {
echo "a is NOT greater than b";
}?>
- if... elseif... else statement - executes different codes for more than two conditions
Syntax
if (condition) {
code to be executed if this condition is true;} elseif (condition) {
code to be executed if this condition is true;} else {
code to be executed if all conditions are false;}
Example
if($a > $b){
echo $a." is greater than ".$b;
}
elseif($a == $b){
echo $a." is equals to ".$b;
}
else{
echo $a." is neither greater than or equal to ".$b;
}
- switch statement - selects one of many blocks of code to be executed
if (condition) {
code to be executed if this condition is true;} elseif (condition) {
code to be executed if this condition is true;} else {
code to be executed if all conditions are false;}
echo $a." is greater than ".$b;
}
elseif($a == $b){
echo $a." is equals to ".$b;
}
else{
echo $a." is neither greater than or equal to ".$b;
}
- switch statement - selects one of many blocks of code to be executed
Syntax
switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all labels;
}
Example
<?php
$i = "bar";
switch ($i) {
case "apple":
echo "i is apple";
break;
case "bar":
echo "i is bar";
break;
case "cake":
echo "i is cake";
break;
}
}?>
- Php Loops
- while statement - execute a block of code while the specified condition is true.
switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all labels;
}
$i = "bar";
switch ($i) {
case "apple":
echo "i is apple";
break;
case "bar":
echo "i is bar";
break;
case "cake":
echo "i is cake";
break;
}
}?>
- while statement - execute a block of code while the specified condition is true.
Syntax
while (condition is true) {
code to be executed;
}
Example
<?php /* example 1 */
$i = 1;
while ($i <= 10) {
echo $i++; /* the printed value would be
$i before the increment
(post-increment) */}
/* example 2 */
$i = 1;
while ($i <= 10):
echo $i;
$i++;
endwhile;?>
- do... while statement - loops through a block of code once, and then repeats the loop as long as the specified condition is true
while (condition is true) {
code to be executed;
}
$i = 1;
while ($i <= 10) {
echo $i++; /* the printed value would be
$i before the increment
(post-increment) */}
/* example 2 */
$i = 1;
while ($i <= 10):
echo $i;
$i++;
endwhile;?>
- do... while statement - loops through a block of code once, and then repeats the loop as long as the specified condition is true
Syntax
do {
code to be executed;} while (condition is true);
Example
<?php
$x = 1;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
- for statement - loops through a block of code a specified number of times
do {
code to be executed;} while (condition is true);
$x = 1;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
- for statement - loops through a block of code a specified number of times
Syntax
for (init counter; test counter; increment counter) {
code to be executed;
}
Example
for ($i = 1; $i < 10 ; $i++) {
echo $i;
}
- foreach statement - loops through a block of code for each element in an array
for (init counter; test counter; increment counter) {
code to be executed;
}
echo $i;
}
- foreach statement - loops through a block of code for each element in an array
Syntax
foreach ($array as $value) {
code to be executed;
}
Example
<?php
foreach (array(2, 3, 4, 5) as $value) {
$value = $value * 2;
echo $value."<br/>";
}
?>
foreach ($array as $value) {
code to be executed;
}
foreach (array(2, 3, 4, 5) as $value) {
$value = $value * 2;
echo $value."<br/>";
}
?>
No comments:
Post a Comment