Friday 11 December 2015

PHP Array

PHP Array
All the variables you have used have held only a single value.
Array variables are “special” because they can hold more than one value in one single variable.
There are three types of array:-
Indexed array : An array with a numeric index
Associative array : An array where each key is associated with a value

Multidimensional array : An array containing one or more array

How to define an array
 FIRST METHOD:-
<?php
  $arr = array(10,11,12,13,14,15);
  echo $arr[0];
?>

2nd method
<?php
$arr[ ]=10;
$arr[ ]=20;
$arr[ ]=30;
$arr[ ]=40;
$arr[ ]=50;
  echo $arr[0];
?>

3rd method
<?php
$arr[0]=10;
$arr[1]=20;
$arr[2]=30;               
$arr[3]=40;               
$arr[4]=50;      
  echo $arr[0];
?>

PHP Associative Array

In associative array index( key ) can initialized according to Your own requirement.
An array in PHP is actually an ordered map. A map is a type that associates values to keys.
In this association use ( => ) sign to define index and values.

There are two ways to create an associative array
First Way to define associative Array
<?php
$Personage=array("Ravi"=>"30","Vishu"=>"21","Harmeet"=>"43");
echo "Ravi is ".$Personage["Ravi"]."Years old";
?>
Another Way to define associative Array
<?php
$Personage['Ravi']=30;
$Personage['Vishu']=21;
$Personage['Harmeet']=43;
echo "Harmeet is ".$Personage["Ravi"]."Years old";
?>

Loop Through an Associative Array
<?php
 $state=array("Dl"=>"Delhi","Hr"=>"Haryana","Pn"=>"Punjab","Br"=>"Bihar");   
foreach($state as $val)
       {
  echo $val." ";
       }
?>
Output
Delhi Haryana Punjab Bihar

Using foreach display index(key) value of $state array
<?php
$state=array("Dl"=>"Delhi","Hr"=>"Haryana","Pn"=>"Punjab","Br"=>"Bihar");
    
foreach($state as $key=>$val)
{
  echo $key."---".$val."<br/>";
}
?>
Output 
Dl — Delhi
Hr — Haryana
Pn — Punjab
Br — Bihar

PHP Indexed Array
        PHP numeric array
Numeric array can stores numbers, strings etc.
Default array index will be represented by numbers.
By default array index starts from 0
In PHP array( ) is used to crate array.Inside this can pass multiple values separated by comma( , )
Syntax:- 
  array(value1,value2..)

Example
<?php
  $arr=array(10,20,30,40,50);
  $col=array("red","green","blue","black");
  //print first value of $arr and $col array
  echo $arr[0];
  echo $col[0];
?>

We can define array in three ways
<?php 
$col=array("blue","red","green","white","pink");

//OR
$col[ ]="blue";
$col[ ]="red";
$col[ ]="green";

//OR
$col[0]="blue";
$col[1]="red";
$col[2]="green";
?>

Loop Through an Numeric(Indexed) Array
Find the sum of given array
<?php      
$sum=0;
$arr=array(10,20,30,40,50);
for($i=0;$i<count($arr);$i++)
{
  $sum=$sum+$arr[$i];
}        
echo "Sum of given array = ".$sum; 
?>

Display all the colors name i.e stored in array
<?php      
$col=array("blue","red","green","white","pink");
for($i=0;$i<count($col);$i++)
{
  echo $col[$i]." ";
}
?>
Find Sum of even and odd numbers stored in array
<?php
$arr=array(10,11,12,13,14,15);
for($i=0;$i<count($arr);$i++)
{  
  if($arr[$i]%2==0) 
  {   $even=$even+$arr[$i];         }
  else 
  {  $odd=$odd+$arr[$i];           }
}
echo "Sum of even=".$even."<br/>";
echo "Sum of odd=".$odd;
?>

PHP Nested Array
Nested / Multi-dimensional Array
A multidimensional array is an array containing one or more arrays.
The dimension of an array indicates the number of indices you need to select an element.
For a two-dimensional array, you need two indices to select an element
For a three-dimensional array, you need three indices to select an element.
Syntax
array(
  array(val1, val1, val2..),
  array(val1,val2,val3..)
       );
Create a two dimensional numeric array and find the sum.
<?php
 error_reporting(‘ERROR’);
$arr=array(array(10,10,10),array(10,10,10),array(10,10,10));
$s=0;  $s1=0;
//using for loop:
for($i=0;$i<3;$i++)
{  for($j=0;$j<3;$j++)
  {  echo $arr[$i][$j]." ";
  $s=$s+$arr[$i][$j];
  }
  echo "<br>";
}
echo "sum of array:".$s;
?>


Create a two dimensional numeric array and find the sum using for-each
<?php
 error_reporting(‘ERROR’);
$arr=array(array(10,10,10),array(10,10,10),array(10,10,10));
$s=0;  $s1=0;
//using foreach loop:
foreach($arr as $k)
  foreach($k as $v)
     {
     echo $v;
     $s1=$s1+$v;
     }
echo "<br>";
}
echo "sum of array:".$s;
?>

More Examples
Two dimensional associative array( user’s name and mobile number)

Two dimensional associative array

Multidimensional associative array
Syntax
array(
  array(
  array(
  array(val1, val1, val2....)
  )
  )
  );






Function in PHP

function in PHP
Function are self contained block of statement which used to perform any specific task.
A function is a block of statements that can be used repeatedly in a program.
A function will not execute immediately when a page loads.
A function will be executed by a call to the function.
Types of Function:-
system defined/library/inbuilt
user defined

Advantages Of Function
function is created once but used many times, often from more than one program.
It reduces duplication within a program.

Debugging and testing a program becomes easier when the program is subdivide.
 
How to define Function
A function will be executed by a call to the function.
Syntax:-
function function_name( )
{
  code to be executed;
}
Note: A function name can start with a letter or underscore (not a number).

Example:-
<?php
//define function and implements it
function f1()
{
  echo “Hello";
}
//call function whenever you need
f1();
?>

PHP Function Arguments

Information can be passed to functions through arguments. An argument is just like a variable.
Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just seperate them with a comma.
<?php
function familyName($fname)
{
    echo "Hello ".$fname."..<br>";
}
familyName("Jani");
familyName("Hege");
?>
PHP Default Argument Value

how to use a default parameter, If we call the function.
<?php
function setHeight($minheight=50) {
  echo "The height is : $minheight <br>";
}
setHeight(350);
setHeight(); // will use the default value of 50
setHeight(135);
setHeight(80);
?>

PHP Functions - Returning values
To let a function return a value, use the return statement. 
<?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);
?>

Scope Of Variables
There are 3 types of variables i.e used inside a function:-
Local
Global
Static

Local Variable
Local variables are always initialize inside the function body. Local variable cant work outside function body.
<?php
function add()
 {
  $x=100;
  $y=200;
  $sum=$x+$y;
  echo "sum of given no=".$sum;
}
add();
?>
Output : Sum of given no = 300
Here define a variable $x and $y . Variable $x and $y has been defined inside add( ) function so these variables are not accessible outside the add( ) function.


Global Variable
Global variables are always initialize outside the function body.
Global variable work inside the function body as well as outside function body.
To access Global variable we have to use global keyword.
<?php
$x=100;
$y=200;
function add()
{
 global $x,$y;
$sum=$x+$y;
echo "sum of given no=".$sum;
}
add();
?>
There are two variables declare $x and $y outside the function body.
Now if we want to call these global variable $x and $y inside add( ) and sub( ) function
then used “global” keyword before the variable name like “global $x” and “global $y”.


Static Variable
When a function is completed, all of its variables are normally deleted. However, sometimes you want a local variable to not be deleted then use static keyword.
<?php
function Test()
{
  static $x=0;
  echo $x;
  $x++;
}
Test();
Test();
Test();
?>
Output : 0 1 2

PHP array
All the variables you have used have held only a single value.
Array variables are “special” because they can hold more than one value in one single variable.
This makes them particularly useful for storing related values.
There are three types of array:-
Indexed array : An array with a numeric index
Associative array : An array where each key is associated with a value
Multidimensional array : An array containing one or more array