Skip Navigation Links
EZWEB
PHPExpand PHP

PHP Control Structures I

PHP Operators

This section lists the different operators used in PHP.
Arithmetic Operators


Operator

Description

Example

Result

+

Addition

x=2
x+2

4

-

Subtraction

x=2
5-x

3

*

Multiplication

x=4
x*5

20

/

Division

15/5
5/2

3
2.5

%

Modulus (division remainder)

5%2
10%8
10%2

1
2
0

++

Increment

x=5
x++

x=6

--

Decrement

x=5
x--

x=4

Assignment Operators

Operator

Example

Is The Same As

=

x=y

x=y

+=

x+=y

x=x+y

-=

x-=y

x=x-y

*=

x*=y

x=x*y

/=

x/=y

x=x/y

%=

x%=y

x=x%y

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

x=6
y=3
(x < 10 && y > 1) returns true

||

or

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

!

not

x=6
y=3
!(x==y) returns true


Operator Precedence

Operator

Associativity

( )

N/A

[ ]

right

!       ++     – –

right

*     /     %

left

+     –     .

left

<    <=    <    <=

N/A

==        ! =

N/A

&&

left

| |

left

?  :

left

=   +=    – =   *=   /=   %=   .=

left

Conditional Statements

  • Conditional statements in PHP are used to perform different actions based on different conditions.
  • Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this.
  • In PHP we have two conditional statements:
  1. if (...else) statement - use this statement if you want to execute a set of code when a condition is true (and another if the condition is not true)
  2. switch statement - use this statement if you want to select one of many sets of lines to execute

The if Statement

If you want to execute some code when a condition is true use an if statement

Syntax:
if (condition)
code to be executed if condition is true; 

Or

Syntax:

if (condition)
{
code to be executed if condition is true;
code to be executed if condition is true; 
}

 

Example:

1

<?

2

$dogName=”Bosko”;

3

if( $dogName == “Bosko” ){

4

   echo “You are a Boxer”;

5

}

6

?>

The if/else Statement

If you want to execute some code if a condition is true and another code if a condition is false, use the if....else statement.

Syntax:
if (condition)
code to be executed if condition is true; 
else
code to be executed if condition is false; 

 

If more than one line should be executed when a condition is true, the lines should be enclosed within curly braces:

Syntax:
if (condition)
{
code to be executed if condition is true;
code to be executed if condition is true; 
}
else
{
code to be executed if condition is false;
code to be executed if condition is false;
code to be executed if condition is false;
} 

 

Example:

1

<?

2

$dogName=”Teddy”;

3

if( $dogName == “Bosko” ){

4

   echo “You are a Boxer”;

5

}

6

else

7

{

8

   echo “You are not my dog”;

9

}

10

?>

The if/elseif/else Statement

If you want to execute some code if a condition is true and then check another condition and finally another code if both  conditions are false, use the if....elseif…else statement.

Syntax:

if( condition1 )
{
      statement1;
}
elseif ( condition2 )
{
      statement2;
}
 else
{
      statement3;
}

 

Example:

1

<?

2

if( $dogName == “Bosko” ){

3

   echo “You are a Boxer”;

4

}

5

elseif($dogName=”Teddy”){

6

   echo “You are a Cardigan Welsh Corgi”;

7

}

8

else{

9

   echo “You are not my dog”;

10

}

 

?>

           

Nested if statements

Syntax:

if( condition1 ){
    if( conditionA ){
         statementA;
         }
    if( conditionB ){
         statementB;
         }
    } // end outer if
else{
    statement;
}

  • Note the placement of curly brackets – the 2nd and 3rd if statements are inside the curly brackets of the 1st statement. In this example, the 2nd and 3rd if statements will only be evaluated if the 1st  is TRUE. The else statement applies only to the 1st if statement.

  • you should indent your code to make it readable, and comment where you need to.


Ternary or Trinary Operator

?  :             it’s called ternary because it has 3 operands

this is used as a shortcut to replace if/else statements:

IF Example:

1

<?

2

if ( $age < 18 ) {

3

  echo “You’re young”;

4

}

5

else {

6

   echo “You’re old”;

7

}

8

?>

                       

Ternary or Trinary Example:

1

<?

2

$age < 18 ? echo “You’re young” : echo “You’re old”;

3

?>

                

  • The part before the ? is the conditional to be tested, the if statement
  • The statement between the ? and the  :  is executed if the first part is TRUE
  • The statement after the  :  is executed if the first part is FALSE
  • It can be confusing, especially if the statements involve complex expressions. But if you learn to use them well, and you get comfortable using them, trinary operators can save coding


switch statements

  • If you want to select one of many blocks of code to be executed, use the Switch statement.

 

Syntax:

switch ( condition) {
    case “case 1”:
         statements;
         break;
    case “case 2”:
         statements;
         break;
    case “case 3”:
         statements;
         break;
    default:
         statements
         break;
}   // end switch

  • The condition can take any number of different values, as long as it evaluates to a simple type (string, integer, or decimal).
    • Then you provide a case for each of the values you expect.
    • Optional is a default case for any condition for which you do not supply a specific case.
  • Notice the “break” statement.
    • Once a match is found between a condition and a case, the switch statement will run until it hits a “break”.
    • If you forget to include a “break” for each of the cases, all the statements will run once a match is found. 
  • When you are evaluating a condition that is a string, make sure you place the cases in quotes.

Example:

1

<?

2

switch ( $dogName ) {

3

   case “Bosko”:

4

      echo “<p>You are a Boxer.</p>”;

5

  break;

6

   case “Teddy”:

7

      echo “<p> You are a Cardigan Welsh Corgi .</p>”;

8

break;

9

   case “Boo”:

10

      echo “<p>You are a Monster.</p>”;

11

  break;

12

   default:

13

      echo “<p>You aren’t one of my dogs.</p>”;

14

      break;

15

}   // end switch

16

?>

           
Embedded PHP

  • You can use conditionals to execute specific blocks of HTML code. We can go back and forth between PHP mode and HTML mode, intertwining each as we need.

Example:

1

<?

2

if($value==5){

3

?>

4

 <body bgcolor=”blue”>

5

<?

6

{

7

else{

8

?>

9

<body bgcolor=”red”>

10

<?

11

}

12

?>

End of PHP Conditionals





Free Tutorials and Training