Php Functions


user Defined Functions
      functions(){
      //content
      }
  
functions with Arguments
         functions($i,$p){
         //contents
       }
     
PHP Functions - Returning values <?php function sum($x, $y) { $z = $x + $y; return $z; } echo "5 + 10 = " . sum(5, 10) . "<br>"; echo "7 + 13 = " . sum(7, 13) . "<br>"; echo "2 + 4 = " . sum(2, 4); ?>

Php Array


An array stores multiple values in one single variable: <?php $cars = array("Volvo", "BMW", "Toyota"); echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . "."; ?> How to Create array In PHP, the array() function is used to create an array:
         $cars = array("Volvo", "BMW", "Toyota");
         
         or

         $cars[0] = "Volvo";
         $cars[1] = "BMW";
         $cars[2] = "Toyota";

            
In PHP, there are three types of arrays: 1. Indexed arrays - Arrays with a numeric index 2. Associative arrays - Arrays with named keys 3. Multidimensional arrays - Arrays containing one or more arrays

Associative Array

I am covering only Associative array because its unique in this programing language.

Associative arrays are arrays that use named keys that you assign to them. There are two ways to create an associative array:

   
            <?php
      $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
     echo "Peter is " . $age['Peter'] . " years old.";
      ?>
    
           

Implode and Explode Functions


Implode() The implode function in PHP is easily remembered as "array to string", which simply means that it takes an array and returns a string. It rejoins any array elements and returns the resulting string, which may be put in a variable. Suppose you have an array like this $arr = Array ("A","E","I","O","U"); and you wish to combine it into a string, by putting the separator '-' between each element of the array. Example <html> <body bgcolor="pink"> <?php $arr=array ('I','am','simple','boy!'); echo implode(" ",$arr); ?> Explode() The explode function in PHP allows us to break a string into smaller text with each break occurring at the same symbol. This symbol is known as the delimiter. Using the explode command we will create an array from a string. The explode() function breaks a string into an array, but the implode function returns a string from the elements of an array. For example you have a string $str="A E I O U"; Output: I am simple boy! <h3>Explode Function</h3> <?php $str="I am simple boy!"; print_r(explode(" ",$str)); ?> Its look like this: Array( [0] => A [1] => E [2] => I [3] => O [4] => U ) which is equal to: $arr = Array ("A","E","I","O","U");
CONTENTS