1 of 19

PHP Unit-II contd..

  • Variable scope
  • Saving state between Function calls with the static statement
  • Arrays Introduction

samueljohn.m@gmail.com

2 of 19

Variable scope

  • Variable scope is known as its boundary within which it can be visible or accessed from code
  • In other words, it is the context within which a variable is defined
  • There are only two scopes available in PHP namely local and global scopes.

samueljohn.m@gmail.com

3 of 19

Variable scope

  • Local variables (local scope)
  • Global variables (special global scope)
  • Static variables (local scope)
  • Function parameters (local scope)

samueljohn.m@gmail.com

4 of 19

Variable scope-local scope

  • A local scope is a restricted boundary of a variable within which code block it is declared
  • That block can be a function, class or any conditional span
  • The variable within this limited local scope is known as the local variable of that specific code block

samueljohn.m@gmail.com

5 of 19

Variable scope-local scope

<?php

function myfun()

{

$count = 5;

echo $count;

}

myfun();

?>

samueljohn.m@gmail.com

6 of 19

Variable scope-global scope

  • Variables in global scope can be accessed anywhere from outside a function or class independent of its boundary
  • PHP global variables can be defined by using global keyword.
  • If we want to use global variables inside a function, we have to prefix the global keyword with the variable. 

samueljohn.m@gmail.com

7 of 19

Variable scope-global scope

<?php

$count = 20;

function findcount()

{

global $count;

echo $count;

}

findcount();

echo $count;

?>

samueljohn.m@gmail.com

8 of 19

Static variable-local scope

  • A static variable is a variable with local scope.
  • A static variable does not lose its value when the program execution goes past the scope boundary
  • But it can be accessed only within that boundary

samueljohn.m@gmail.com

9 of 19

Static variable-local scope

<?php

function counter()

{

static $count = 1;

echo $count;

$count++;

}

counter();

counter();

counter();

?>

samueljohn.m@gmail.com

10 of 19

Function parameters-local scope

  • Function parameters (arguments) are local variables defined within the local scope of the function on which it is used as the argument.

<?php

  function mynumber($x)

   {

     echo “Number is $x”;

   }

     mynumber(12);

?>

samueljohn.m@gmail.com

11 of 19

Arrays in PHP

samueljohn.m@gmail.com

12 of 19

What is an array?

  • An array is a collection of elements of the same data type
  • How to create an array in PHP?
  • In PHP, the array() function is used to create an array

$cars = array(“Maruti”, “BMW”, “volvo”);

samueljohn.m@gmail.com

13 of 19

Types of arrays in PHP

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

samueljohn.m@gmail.com

14 of 19

Indexed arrays in PHP

  • An array with a numeric index where values are stored linearly

<?php$cars = array(“Maruti”, “BMW”, “Toyota”);echo $cars[0].“,”.$cars[1].“and”. $cars[2];

?>

samueljohn.m@gmail.com

15 of 19

Indexed arrays in PHP

  • The index can be assigned manually

$cars[0] = “Maruti”;�$cars[1] = "BMW";�$cars[2] = “volvo”;

samueljohn.m@gmail.com

16 of 19

Associative Arrays in PHP

  • Associative arrays are arrays that use named keys that you assign to them

$age=array(“Ravi”=>"35"“Kiran”=>“37”);echo “Ravi is”.$age[‘Ravi’]. “years old”;

samueljohn.m@gmail.com

17 of 19

Multidimensional arrays in PHP

  • A two-dimensional array is an array of arrays

$cars = array  (  array("Maruti",21,13),  array("Volvo",15,13),  array("BMW",5,2),  );

samueljohn.m@gmail.com

18 of 19

The count() Function�

  • The count() function is used to return the length (the number of elements) of an array.

<?php$cars = array("Maruti""BMW""Volvo");echo count($cars);

?>

samueljohn.m@gmail.com

19 of 19

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