Pages

Thursday 12 February 2015

Integrating PHP with Embedded System : Chapter -3

User Defined Function in PHP

              Functions are group of statements that can perform a task.

Syntax:
                   function functionName()
               {
                      code to be executed;
               } 

User Defined Function Example


<html>

   <body>

        <?php



             // Function definition

             function myFunction()

             {

                   echo "Hello world";

             }

            

             // Function call

             myFunction();

        ?>

</body>


</html>

OUTPUT of the above given Example is as follows:
Hello world

Swap Numbers PHP Example

<html>
   <body>
        <?php
             $num1=10;
             $num2=20;
             echo "Numbers before swapping:<br/>";
             echo "Num1=".$num1;
             echo "<br/>Num2=".$num2;
            
             // Function call
             swap($num1,$num2);
            
             // Function definition
             function swap($n1,$n2)
             {
                   $temp=$n1;
                   $n1=$n2;
                   $n2=$temp;
                   echo "<br/><br/>Numbers after
                               swapping:<br/>";
                   echo "Num1=".$n1;
                   echo "<br/>Num2=".$n2;
             }

        ?>
   </body>
</html>


OUTPUT of the above given Example is as follows:
Numbers before swapping:
Num1=10
Num2=20

Numbers after swapping:
Num1=20

Num2=10


PHP Functions - Adding parameters


<html>

   <body>

        <?php

             // Function definition

             function writeName($fname)

             {

                   echo $fname . " Refsnes.<br />";

             }



             echo "My name is ";

             writeName("Kai Jim"); //Function call



             echo "My sister's name is ";

             writeName("Hege"); // Function call



             echo "My brother's name is ";

             writeName("Stale"); // Function call



        ?>

</body>
<html>

              OUTPUT of the above given Example is as follows:
 My name is Kai Jim Refsnes.
              My sister's name is Hege Refsnes.
              My brother's name is Stale Refsnes.



PHP Functions - Return values

<html>

   <body>

        <?php

             // Function definition

             function add($x,$y)

             {

                  $total=$x+$y;

                  return $total;

             }

                             // Function call

             echo "1 + 16 = " . add(1,16);

        ?>

   </body>
           </html>


OUTPUT of the above given Example is as follows:
1 + 16 = 17

Break statement


  •                 Break statement is used to terminate the loop
  • After the break statement is executed the control goes to the statement      immediately after the loop containing break statement.

Break statement example

<html>
     <body>
           <?php
                /* when $i value becomes 3, the loop is
Terminated*/
                for($i=0;$i<5;$i++)
                {
                     if($i==3)
                           break;
                     else
                           echo "$i ";
                }
          ?>
</body>
</html>

OUTPUT of the above given Example is as follows:


0 1 2

Continue statement


  •          There are cases in which, rather than terminating a loop, you simply want to skip over the remainder of iteration and immediately skip over to the next . Continue statement is used to skip a particular iteration of the loop.



Continue statement example



<html>
     <body>
           <?php
                /* when $i value becomes 3, it will skip the
                particular of the loop*/

                for($i=0;$i<=5;$i++)
                {
                     if($i==3)
                           continue;
                     else
                           echo "$i ";
                }
          ?>
</body>


</html>


OUTPUT of the above given Example is as follows:

0 1 2 4 5



PHP Global Variables - Superglobals


  •      Superglobals" are predefined variables in PHP.
  •         They are always accessible, regardless of scope - and can access them from any function, class or file



The PHP superglobal variables are:
$GLOBALS
$_SERVER
$_REQUEST
$_POST
$_GET
$_FILES
$_ENV
$_COOKIE
$_SESSION


$GLOBALS

  •          $GLOBALS is a PHP super global variable which is used to access global variables from anywhere in the PHP script (also from within functions or methods)
  •        PHP stores all global variables in an array called $GLOBALS[index]. The index holds the name of the variable.

Example:



<html>
<body>
        <?php
        $a = 20;
        $b = 40;
        function addition()
        {
             $GLOBALS['c'] = $GLOBALS['a'] + $GLOBALS['b'];
        }
        addition();
        echo $c;
        ?>
</body>
</html>

OUTPUT of the above given Example is as follows:

60

$_SERVER

  •       $_SERVER is a PHP super global variable which holds information about headers, paths, and script locations


Example




<html>

<body>

<?php

echo $_SERVER['PHP_SELF'];

echo "<br>";

echo $_SERVER['SERVER_NAME'];

echo "<br>";

echo $_SERVER['HTTP_HOST'];

echo "<br>";

echo $_SERVER['HTTP_USER_AGENT'];

echo "<br>";

echo $_SERVER['SCRIPT_NAME'];

?>
</body>
</html>


OUTPUT of the above given Example is as follows:


/User/server.php
localhost
localhost
Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; GTB7.5;
SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729;
Media Center PC 6.0; InfoPath.2; .NET CLR 1.1.4322)
/User/server.php



 


                                      

Array in PHP         

  • An array stores multiple values in one single variable
  • In PHP, there are three kinds of arrays:
  • Numeric array
  • Associative array
  • Multidimensional array

Numeric Array in PHP

  • Numeric array is an array with a numeric index.
  Numeric Array Example


 <html>
   <body>
        <?php 
             /* An array $flower_shop is created with three
             Values- rose, daisy,orchid*/
             $flower_shop = array ( 
                             "rose", 
                             "daisy", 
                             "orchid"
             ); 
             /* Values of array $flower_shop is displayed based
on index. The starting index of an array is Zero */
             echo "Flowers: ".$flower_shop[0].",
                  ".$flower_shop[1].", ".$flower_shop[2]."";
        ?>
   </body>
</html>


OUTPUT of the above given Example is as follows:
Flowers: rose, daisy, orchid