Sunday, April 29, 2012

PHP Associative Array Tutorial


Associative Array
Associative array = Associative word with another word.

In the script below, we create the basis for a Acronym Lookup program in PHP. We will use an associative array to reference the meaning of each acronym, and use the acronym as the associative array's index:

We have three associative arrays: MPLS (links the Multi-Protocol Label Switching), SSTP (links the Secure Socket Tunneling Protocol), and LDAP (links the Lightweight Directory Access Protocol).
Example: 1
<?php
   
   $protocols = array('MPLS'=>'Multi-Protocol Label Switching', 'SSTP'=>'Secure Socket Tunneling Protocol', 'LDAP'=>'Lightweight Directory Access Protocol');
   echo $protocols['MPLS']."<br />";
   echo $protocols['SSTP']."<br />";
   echo $protocols['LDAP'];
?>
Output
Multi-Protocol Label Switching
Secure Socket Tunneling Protocol
Lightweight Directory Access Protocol

Example: 2
<?php
$protocols = array();
$Protocols['MPLS']= 'Multi-Protocol Label Switching';
$Protocols['SSTP']= 'Secure Socket Tunneling Protocol';
$Protocols['LDAP']= 'Lightweight Directory Access Protocol';
echo 'MPLS stands for: '.$Protocols['MPLS']."<br />";
echo 'SSTP stands for: '.$Protocols['SSTP']."<br />";
echo 'LDAP stands for: '.$Protocols['LDAP']."<br />";
?>
Output
MPLS stands for: Multi-Protocol Label Switching
SSTP stands for: Secure Socket Tunneling Protocol
LDAP stands for: Lightweight Directory Access Protocol

PHP Arrays with Loops Tutorial


Arrays with Loops
Example: 1
        <?php
          
            $people = array('Oliva', 'Nicole', 'Natasha');
           for ($x=0; $x<sizeof($people); $x++)
           echo $people[$x]."<br />";
        ?>
Output
Oliva
Nicole
Natasha

Another way
Example: 2
        <?php
            
         $people = array('Oliva', 'Nicole', 'Natasha');
           foreach ($people as $x)
           echo $x."<br />";
         
        ?>
Output
Oliva
Nicole
Natasha

PHP Array Tutorial


Arrays
1. The Array object is used to store multiple values or more than one value in a similar data type variable.

2. Accessing a particular element in an array by referring to the name of the array and the index number. 

3. The index number of array starts from 0.

4. If you specify numbers or true/false values inside the array then the variable type will be Number or Boolean, instead of String.

5. Note: Array is a key word.
An array can be defined in three ways:
Numeric array - An array with a numeric ID key 
Associative array - An array where each ID key is associated with a value 
Multidimensional array - An array containing one or more arrays

Example: 1
        <?php
            $people[0] = "Oliva";
            $people[1] = "Nicole";
           $people[2] = "Natasha";
           echo $people[1];
        ?>
Output
Nicole 

Another way
Example: 2
        <?php
              $people = array('Oliva', 'Nicole', 'Natasha');
              echo $people[1];
        ?>
Output
Nicole 

PHP Do While Loop Tutorial


Do While Loop
Do while loop allows you to execute an action at least one time no matter if it is true or false. The loop executes round and round while the condition is true. When the condition becomes false, the loop breaks out or terminates of the loop. 
Syntax
do
{
// statements that will be executed
}
while(condition)
Example
<?php
        $num=1;
        do
        {
          $num++;
          echo $num."<br />";
        }
         while ($num<10);
?>
Output
2
3
4
5
6
7
8
9
10

PHP While Loop Tutorial


While Loop
While loop is used to execute a set of statements repeatedly until a condition becomes false.
Syntax:
while(condition)
{
// statements that will be executed
}
Example
        <?php
        $num=1;
        while($num<=5)   //[While the variable ‘num’ is less than or equal to 5.]
        {
            echo $num."<br />";
            $num++;
        }
        ?>
Output
1
2
3
4
5

PHP Foreach Loop Tutorial


Foreach Loop
Syntax
foreach ($array_variable as $value) 

  [code to execute] 
}
or
foreach ($array_variable as $key => $value) 

  [code to execute] 
}

Example: 1
<?php
   $x=array(1,2,3,4,5); 
   foreach ($x as $value)
   {
     echo “New value is “.$value*10 . "<br />";
    }
?>
Output
New value is 10
New value is 20
New value is 30
New value is 40
New value is 50

PHP For Loop Tutorial


For Loop
For loop executes a certain condition as many times as you want.

Syntax

for(intial value; condition; increment)
{
// set of statements that will be executed
}
All the three parameters are separated by semicolon ";".
Loop is used to execute the same set of statements with a specified number of times(until a condition is satisfied) in incremental or detrimental way.
Example: 1
        <?php
       for($num=1; $num<=5; $num++)
        {
        echo $num."<br />";
        }
        ?>
Output
1
2
3
4
5
Note: When “i<=5” executes, then condition remain true and loop terminates when “i=6”. “i<=5” says “i is less than or equal to 5”.

Example: 2
        <?php
       $sum=0;
       for($i=1; $i<5; $i++)
       {
       $sum = $sum+$i;
       }
       echo "The sum of 1 through 4 is: ".$sum; 
        ?>
Output
The sum of 1 through 4 is: 10