1 of 12

Web Technologies

PHP Variables & Scope

Smt.M.Jeevana Sujitha

Assistant Professor

Department of Computer Science and Engineering

SRKR Engineering College, Bhimavaram, A.P. - 534204

2 of 12

OBJECTIVES

The Objectives of this lecture are

  • To learn about PHP variables and its scope.

3 of 12

PHP Variables & Scope

Variable:

  • Variables are "containers" for storing information. Declaring variables:
  • In PHP, a variable starts with the $ sign followed by the name of the variable.

Example:

<?php

$txt = "Hello world!";

$x = 5;

$y = 10.5;

?>

4 of 12

PHP Variables & Scope

Rules for PHP Variables:

  • A variable starts with the $ sign, followed by the name of the variable.
  • A variable name must start with a letter or the underscore character.
  • A variable name cannot start with a number.
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Variable names are case-sensitive ($age and $AGE are two different variables)

5 of 12

PHP Variables & Scope

Output Variables:

  • The PHP echo statement is often used to output data to

the screen.

Example-1:

<?php

$txt = "Srinu Tottempudi";

echo $txt;

?>

Example-2:

<?php

$x = 5;

$y=10;

echo $x+$y;

?>

6 of 12

PHP Variables & Scope

PHP is loosely typed language:

  • In PHP we did not have to tell which data type the variable is.
  • PHP automatically associates a data type to the variable, depending on its value.

7 of 12

PHP Variables & Scope

declared

PHP Variables scope:

  • In PHP, variables can be anywhere in the script.
  • The scope of a variable is the part of the script where the variable can be referenced/used.
  • PHP has three different variable scopes:
  • local
  • global
  • static

8 of 12

PHP Variables & Scope

Global scope:

  • A variable declared outside a function has a GLOBAL

SCOPE and can only be accessed outside a function.

Example:

<?php

$x = 5; // global scope

function display()

{

echo "Variable x inside function is: $x";

}

display();

echo "Variable x outside function is: $x";

?>

9 of 12

PHP Variables & Scope

Local scope:

  • A variable declared within a function has a LOCAL SCOPE

and can only be accessed within that function.

Example:

<?php

function display()

{

$x = 5; // local scope

echo "Variable x inside function is: $x";

}

display();

echo "Variable x outside function is: $x";

?>

10 of 12

PHP Variables & Scope

Static scope:

  • Normally, when a function is completed/executed, all of its variables

are deleted.

  • However, sometimes we want a local variable NOT to be deleted. We

need it for a further job.

  • To do this, use the static keyword when you first declare the variable. Example:

<?php

function test()

{

static $x = 0; echo $x;

$x++;

}

test();

test();

test();

?>

11 of 12

PHP Variables & Scope

The PHP global keyword:

  • The global keyword is used to access a global variable from within a function.
  • To do this, use the global keyword before the variables (inside the

function).

Example:

<?php

$x = 5;

$y = 10;

function test()

{

global $x, $y;

$y = $x + $y;

}

test();

echo $y;

?>

12 of 12

THANK YOU