Monday, April 30, 2012

PHP str shuffle Function Tutorial


str shuffle Function
Syntax:
str_shuffle(string)

The str_shuffle() function randomly shuffles all the characters of a string.
The str_shuffle() function is mainly used for to to generate a random password or produce random characters for CAPTCHA validation image.
Each time this function executes and it will randomly re-shuffle the string.
Example: 1
        <?php
        $string = 'This is a test';
        $x=str_shuffle($string);
        echo $x;
        ?>
Output
aih istT sest

Random character generator.
Example: 2
        <?php
        $string = 'abcdefghijklmnopqrstwxyz0123456789';
        $x=str_shuffle($string);
        $y = substr($x,0, 5);
        echo $y;
        ?>
Output
g1c3w

Example: 3
        <?php
       $string = 'abcdefghijklmnopqrstwxyz0123456789';
       $x=str_shuffle($string);
       $y = substr($x,0, strlen($string)/2);
       echo $y;
        ?>
Output
57zo04xp3hykcl1ei 

See a password generator program below the link:
Password Generator Program 

PHP Explode Function Tutorial


Explode Function
syntax:
$array_name = explode($delimiter, $string);
The explode() function is used to split up strings based on the specified delimiter.
It is similar to the split function in Javascript.
Once the string is split or broken up, the pieces are stored in an array and they are numbered in order, starting from 0.
Example: 1
        <?php
        $str = "I am responsible for my own health.";
        print_r (explode(" ",$str));
        ?>
Another way:
        <?php
        $str = "I am responsible for my own health.";
        $parts = (explode(" ",$str));
        print_r ($parts);
        ?>
Output
Array ( [0] => I [1] => am [2] => responsible [3] => for [4] => my [5] => own [6] => health. ) 

We can apply explode function on date or phone number.
Let's see an example.
Example: 2
        <?php
        $D_M_Y = date("d/m/Y");
        $parts = (explode('/' ,$D_M_Y));
        print_r ($parts);
        ?>
Output
Array ( [0] => 30 [1] => 04 [2] => 2012 ) 

Notice that today's date is broken.

Sunday, April 29, 2012

PHP gatedate Function Tutorial


gatedate Function
The getdate() function returns an array that contains date ten elements with relevant information needed when formatting a date string:
      [seconds] - seconds
      [minutes] - minutes
      [hours] - hours
      [mday] - day of the month (numeric) 
      [wday] - day of the week (numeric) 
      [mon] - month 
      [year] - year
      [yday] - day of the year (numeric) 
      [weekday] - name of the weekday
      [month] - name of the month
Syntax
getdate(timestamp)
timestamp is optional.

Example: 1
<?php
print_r(getdate());
?>
Output
Array ( [seconds] => 51 [minutes] => 58 [hours] => 2 [mday] => 30 [wday] => 1 [mon] => 4 [year] => 2012 [yday] => 120 [weekday] => Monday [month] => April [0] => 1335769131 ) 

Example: 2
       <?php
       $today = getdate();
       $Month = $today['month'];
       $Day = $today['mday'];
       $Year = $today['year'];
       $Hour = $today['hours'];
       $Minutes = $today['minutes'];
       $Seconds = $today['seconds'];
       echo "Today's date is $Day $Month $Year"."<br>";
       echo "The time is now $Hour $Minutes $Seconds";
       ?>
Output
Today's date is 30 April 2012
The time is now 2 58 51 

PHP Date Function Tutorial


Date Function
To display in numeric format:
1. d, a lower case d, stands for date of the month( 1 to 31).
2. m, a lower case m, stands for the month( 1 to 12).
3. y, a lower case y, stands for the year(in two digits).

To display in text format:
1. D, a capital D, stands for day of the month in tex( Saturday, Sunday, etc. ).
2. M,a capital M, stands for the month in text( 1 to 12).
3. Y, Capital Y, stands for date of the year(in four digits).


To display in numeric format:
Example: 1
        <?php
        echo date("d/m/y");
        ?>
Output
30/04/12

You can change the format like period.
Example: 2
        <?php
        echo date("d.m.y");
        ?>
Output
30.04.12

You can also use minus sign.
Example: 3
        <?php
        echo date("d-m-y");
        ?>}
Output
30-04-12

To display in text format:
Example: 1
        <?php
        echo date("D/M/Y");
        ?>
Output
Mon/Apr/2012

You can change the format like period.
Example: 2
        <?php
        echo date("D.M.Y");
        ?>
Output
Mon.Apr.2012

You can also use minus sign.
Example: 3
        <?php
        echo date("D-M-Y");
        ?>}
Output
Mon-Apr-2012 

To display full date:
Example: 4
        <?php
        echo date("d D M Y");
        ?>}
Output
30 Mon Apr 2012 

The following example will output "Have a nice weekend!" if the current day is Sunday, otherwise it will output "Have a nice day!":
Example: 5
<?php
$d=date("D");
if ($d=="Sun")
  echo "Have a nice weekend!";
else
  echo "Have a nice day!";
?>
Output
Have a nice day! 

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