Web Technologies
PHP Variables & Scope
Smt.M.Jeevana Sujitha
Assistant Professor
Department of Computer Science and Engineering
SRKR Engineering College, Bhimavaram, A.P. - 534204
OBJECTIVES
The Objectives of this lecture are
PHP Variables & Scope
Variable:
Example:
<?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
?>
PHP Variables & Scope
Rules for PHP Variables:
PHP Variables & Scope
Output Variables:
the screen.
Example-1:
<?php
$txt = "Srinu Tottempudi";
echo $txt;
?>
Example-2:
<?php
$x = 5;
$y=10;
echo $x+$y;
?>
PHP Variables & Scope
PHP is loosely typed language:
PHP Variables & Scope
declared
PHP Variables scope:
PHP Variables & Scope
Global scope:
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";
?>
PHP Variables & Scope
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";
?>
PHP Variables & Scope
Static scope:
are deleted.
need it for a further job.
<?php
function test()
{
static $x = 0; echo $x;
$x++;
}
test();
test();
test();
?>
PHP Variables & Scope
The PHP global keyword:
function).
Example:
<?php
$x = 5;
$y = 10;
function test()
{
global $x, $y;
$y = $x + $y;
}
test();
echo $y;
?>
THANK YOU