PHP Unit-II contd..
samueljohn.m@gmail.com
Variable scope
samueljohn.m@gmail.com
Variable scope
samueljohn.m@gmail.com
Variable scope-local scope
samueljohn.m@gmail.com
Variable scope-local scope
<?php
function myfun()
{
$count = 5;
echo $count;
}
myfun();
?>
samueljohn.m@gmail.com
Variable scope-global scope
samueljohn.m@gmail.com
Variable scope-global scope
<?php
$count = 20;
function findcount()
{
global $count;
echo $count;
}
findcount();
echo $count;
?>
samueljohn.m@gmail.com
Static variable-local scope
samueljohn.m@gmail.com
Static variable-local scope
<?php
function counter()
{
static $count = 1;
echo $count;
$count++;
}
counter();
counter();
counter();
?>
samueljohn.m@gmail.com
Function parameters-local scope
<?php
function mynumber($x)
{
echo “Number is $x”;
}
mynumber(12);
?>
samueljohn.m@gmail.com
Arrays in PHP
samueljohn.m@gmail.com
What is an array?
$cars = array(“Maruti”, “BMW”, “volvo”);
samueljohn.m@gmail.com
Types of arrays in PHP
samueljohn.m@gmail.com
Indexed arrays in PHP
<?php�$cars = array(“Maruti”, “BMW”, “Toyota”);�echo $cars[0].“,”.$cars[1].“and”. $cars[2];�
?>
samueljohn.m@gmail.com
Indexed arrays in PHP
$cars[0] = “Maruti”;�$cars[1] = "BMW";�$cars[2] = “volvo”;
samueljohn.m@gmail.com
Associative Arrays in PHP
$age=array(“Ravi”=>"35", “Kiran”=>“37”);�echo “Ravi is”.$age[‘Ravi’]. “years old”;
samueljohn.m@gmail.com
Multidimensional arrays in PHP
$cars = array� (� array("Maruti",21,13),� array("Volvo",15,13),� array("BMW",5,2),� );
samueljohn.m@gmail.com
The count() Function�
<?php�$cars = array("Maruti", "BMW", "Volvo");�echo count($cars);
?>
samueljohn.m@gmail.com
Loop Through an Indexed Array�
<?php�$cars=array("Maruti","BMW","Volvo");�$len=count($cars);��for($x = 0; $x < $len; $x++)
{� echo $cars[$x];� echo "<br>";�}
?>
samueljohn.m@gmail.com