Pages

Thursday 12 February 2015

Integrating PHP with Embedded System : Chapter -2

String Operators in PHP




String Operators Example


<html>
   <body>
           <?php
                $a = "Hello";
                $b = $a . " Friend!";
                echo $b;
                echo "<br/>";

                $c="Good";
                $c .= " Day!";
                echo $c;
          ?>
 </body>
 </html>
OUTPUT of the above given Example is as follows:
Hello Friend!
Good Day!

The if Statement in PHP 
  • If statement executes some code only if a specified condition is true 
Syntax:

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

The if Statement Example 

<html>

     <body>     

           <?php

                $i=0;



                /* If condition is true, statement is
                executed*/

                if($i==0)
                     echo "i is 0";
           ?>
     <body>
</html>


OUTPUT of the above given Example is as follows:

i is 0

The if…else Statement in PHP 

  • If…else statement executes some code if a condition is true and some another code if the condition is false

    Syntax: 

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

     
    The if…else Statement Example
    <html>
         <body>
               <?php
                    $i=1;

                    /* If condition is true, statement1 is
                    executed, else statement2 is executed*/

                    if($i==0)
                         echo "i is 0";  //statement1
                    else
                         echo "i is not 0";  //statement2
               ?>
    <body>
    </html>
      

    OUTPUT of the above given Example is as follows:
    i is not 0


  The if…elseif…else Statement in PHP

    • If…elseif…else statement selects one of several blocks of code to be executed
    Syntax:

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

    The if…elseif…else Statement Example (Comparing two numbers) 

    <html>

       <body>

            <?php

                 $i=22;

                 $j=22;

               /* If condition1 is true, statement1 is executed,
                if condition1 is false and condition2 is true,
                statement2 is executed, if both the conditions
     are false statement3 is executed */
                 if($i>$j)
                       echo "i is greater"; //statement1
                 elseif($i<$j)
                       echo "j is greater"; //statement2
                 else
                      echo "numbers are equal";  //Statement3
            ?>
    <body>
    </html>
    OUTPUT of the above given Example is as follows:
    numbers are equal

    Switch Statement in PHP
      

    • Switch statement selects one from multiple 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;
            ...
              default:
                     code to be executed if n is different from all labels;

    Switch Statement Example

    <html>

         <body>
               <?php

                    $x=3;

                    /* Expression value is compared with each case

                    value. If it matches, statements following

    case would be executed. Break statement is

    used to terminate the execution of

    statement.*/

                    switch ($x)

                    {

                      case 1:

                               echo "Number 1";

                               break;

                      case 2:

                              echo "Number 2";

                              break;

                      case 3:

                              echo "Number 3";

                              break;

                      default:

                              echo "No number between 1 and 3";

               }

         ?>

    </body>

    </html>




     
    OUTPUT of the above given Example is as follows:
    Number 3

    For loop in PHP


    • PHP for loop executes a block of code, a specified number of times 

      Syntax: 

      for (initialization; test condition; increment/decrement) {
                code to be executed;

      For loop Example
      <html>
           <body>
                 <?php

                      echo "Numbers from 1 to 20 are: <br>";

      /*in for loop, initialization usually declares a loop variable, condition is a Boolean expression such that if the condition is true, loop body will be executed and after each iteration of loop body, expression is executed which usually increase or decrease loop variable*/

                      for ($x=0; $x<=20; $x++) {
                           echo "$x  ";
                      }
                 ?>
           </body>
      </html>
      OUTPUT of the above given Example is as follows:
      Numbers from 1 to 20 are: 
      0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20



    Declaring multiple variables in for loop Example

    <html>
         <body>
               <?php

    /* Multiple variables can be declared in
     declaration block of for loop */

                    for ($x=0,$y=1,$z=2;$x<=3;$x++) {
                         echo "x = $x,  y = $y,  z = $z <br>";
                    }
               ?>
         </body>
    </html>
    OUTPUT of the above given Example is as follows:
    x = 0, y = 1, z = 2
    x = 1, y = 1, z = 2
    x = 2, y = 1, z = 2
    x = 3, y = 1, z = 2 

    While Loop in PHP

    • While loop, loops through a block of code as long as the specified condition is true
    Syntax:

    while (condition) {
          code to be executed;
    }

    While Loop Example

     

    <html>
         <body>
              <?php
                    $i=1;

    /* here <condition> is a Boolean expression. Loop body is executed as long as condition is true*/

                    while($i<5){
                         echo "i is = $i <br>";
                         $i++;
                    }
               ?>
    </body>
    </html>

    OUTPUT of the above given Example is as follows:

    i is = 1
    i is = 2
    i is = 3
    i is = 4


    Do While loop in PHP 


    • Do while loop will always execute the block of code once, it will then check the condition, and if the condition is true then it repeats the loop 
    Syntax:

     
    do {
          code to be executed;
    } while (condition ); 



    Do While loop Example 


    <html>

       <body>

            <?php

                 $i=1;



    /* here <condition> is a Boolean expression. Please

    note that the condition is evaluated after executing

    the loop body. So loop will be executed at least

    once even if the condition is false*/



                 do

                 {

                       echo "i is = $i <br>";

                       $i++;

                 }while($i<5);

            ?>

    </body>

    </html>

    OUTPUT of the above given Example is as follows:

    i is = 1
    i is = 2
    i is = 3
    i is = 4