This topic, we'll show you some examples of using PHP commands, which are if, else, elseif, if/then, if/then/else, if/ifelse/else, switch, etc.
PHP Commands Example
If
<?php
$a = 5;
$b = -5;
$c = 3;
if($a < $b) {
echo"\$a is less than \$b";
}
if($b < $c) {
echo"\$b is less than \$c";
}
?>
Else
<?php
$a = 5;
$b = -5;
if($a < $b) {
echo"\$a is less than \$b";
}
else {
echo"\$a is more thank or same as \$b";
}
?>
Elseif
<?php
$b = -5;
$c = 3;
if($b > $c) {
echo"$b is more than $c";
}
elseif($b < $c) {
echo"$b is less than $c";
}
else {
echo"$b is same as $c";
}
?>
If...Then
<?php
$a = 1;
if($a) {
echo "True!";
}
?>
If...Then...Else
<?php
$a = 5;
$b = "10";
if($a > $b) {
echo "$a is more than $b";
} else {
echo "$a is less than $b";
}
?>
If...Elseif...Else
<?php
if($a == $b) {
//if $a same value as $b, do the first command
//do second command if true
}
elseif($a > $b) {
//if $a is more than $b, do this command
}
elseif($a < $b)) {
//if $a is less than $b, do this command
}
else {
//if all commands false, do this command
}
?>
Switch
<?php
$a = "100";
switch($a) {
case(10):
echo "variable keep 10";
break;
case(100):
echo "variable keep 100<br>";
case(1000):
echo "variable keep 1000";
break;
default:
echo "<p>are you sure that it was number or not";
}
?>
For Loop
<?php
for($i = 0; $i < 10; $i++) {
echo $i<br>;
}
?>
Foreach Loop
<?php
$array = array("name" => "Jet","Occupation" => "Bounty Hunter");
foreach($array as $val) {
echo "<p>$val";
}
?>
While
<?php
$bg = "DDDDDD";
while($data = mysql_fetch_array($result)) {
if($bg = "DDDDDD") {
$bg = "EEEEEE";
}
else {
$bg = "DDDDDD";
}
echo '<tr bgcolor="$bg">',
echo $data[name];
echo'</tr>';
?>
Do...While
<?php
$i = 0;
do {
print$i;
}
while($i>0);
?>
For
<?php
for($a = 1; $a < 13; $a++) {
echo"<font color=green>";
echo"2 x $a ="2*$a,"<br>";
echo"</font>";
}
?>
Information from website:
https://sites.google.com