PHP simple : PHP tutorial, MySQL tutorial
PHP Tutorial - Free PHP Script

Home | Tutorials | Php vs Node.Js | Web Tools

PHP Control Structures


There are 2 groups of the PHP control structures.

Condition Control

  • if, else, ifelse
  • switch

Loop Control

  • while
  • for

For use the PHP control structures. You need to know about the operators using in control structures.

Comparison Operators

Operator Description Example
== is equal to 5==8 returns false
!= is not equal 5!=8 returns true
> is greater than 5>8 returns false
< is less than 5<8 returns true
>= is greater than or equal to 5>=8 returns false
<= is less than or equal to 5<=8 returns true

Logical Operators

Operator Description Example
&&, and and x=6
y=3

(x < 10 && y > 1) returns true

||, or or x=6
y=3

(x==5 || y==5) returns false

! not x=6
y=3

!(x==y) returns true


if, else, ifelse

The if statement evaluates the truth value of it's argument. If the argument evaluate as TRUE the code following the if statement will be executed. And if the argument evaluate as FALSE and there is an else statement then the code following the else statement will be executed.

syntax :

if(condition){
codes and commands when evaluate is true.
}

if(condition){
codes and commands when evaluate is true.
}else{
codes and commands when evaluate is false.
}

if(condition1){
codes and commands when evaluate is true.
}elseif(condition2){
codes and commands when evaluate is true.
}else{
codes and commands when all evaluate is false.
}

Example
<?
$a=10;
$b=5;

if($a<$b){ echo '$a is less than $b'; }
elseif($a>$b){ echo '$a is greater than $b'; }
else{ echo '$a is equal to $b'; }


// Result : $a is greater than $b

?>


while

The while statement is used to execute a piece of code repeatedly as long as the while expresssion evaluates as true.

syntax :

while(condition){
codes and commands while evaluate is true.
}

Example
<?
$num= 1;

while ($num< 10)
{
echo "$num<br>"';
$num+= 1;
// this code is same of $num=$num+1
}

// Result :
1
2
3
4
5
6
7
8
9


?>

That loop will continue to run forever as long as the loop expression ($number < 10) evaluates as true. When you are creating a loop please make sure you already put the code to make sure the loop will end in timely manner.

Break

The break statement is used to stop the execution of a loop. As an example the while loop below will stop when $number equals to 5.

Example
<?
$num= 1;

while ($num< 10)
{
echo "$num<br>"';

if($number==5)
{
break;
}

$num+= 1;
// this code is same of $num=$num+1
}

// Result :
1
2
3
4
5


?>


for

syntax :

for(define parameter ; condition ; add or decrease parameter){
codes and commands while evaluate is true.
}

Example
<?
for($i=1;$i<= 5;$i++){
echo "$i<br>";
}

// Result :
1
2
3
4
5


?>

Advertise
Put Ebay RSS Feeds onto your website
SEO Elite Software
Domain Dashboard CPanel & Seo Manager
Work at Home Ideas and Opportunities
Introduction & Get Start
Using Form
PHP
PHP Tags
PHP Variables
PHP Control Structures
MySQL
Create Database & Table
Database Connection
Insert Record
Select Record
Update Record
Delete Record

PHPsimple.net
info@phpsimple.net