0% found this document useful (0 votes)
353 views

PHP Variables

Variables in PHP can have global, local, or static scope. Global variables declared outside of functions can be accessed anywhere, local variables declared within functions can only be accessed within the function, and static variables allow a local variable to persist between function calls. PHP supports basic data types like strings, integers, floats, booleans, arrays, objects, NULL values, and resources.

Uploaded by

Annu Jaiswal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
353 views

PHP Variables

Variables in PHP can have global, local, or static scope. Global variables declared outside of functions can be accessed anywhere, local variables declared within functions can only be accessed within the function, and static variables allow a local variable to persist between function calls. PHP supports basic data types like strings, integers, floats, booleans, arrays, objects, NULL values, and resources.

Uploaded by

Annu Jaiswal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

PHP Variable scope

-- variables can be declared anywhere in the script


-- Scope of a variable is the part of script where variable can be
referenced/used.
-- There are 3 types of scopes:
Local global static

Global Scope
A variable declared outside a function has a GLOBAL SCOPE
and can only be accessed outside a function
<?php
$x = 5; // global scope
function myTest()
{
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
echo "<p>Variable x outside function is: $x</p>";
?>
O/P: Variable x inside function is:
Variable x outside function is: 5

Local Scope
A variable declared within a function has a LOCAL SCOPE and
can only be accessed within that function
Code is same
O/P: Variable x inside function is: 5
Variable x outside function is:
global Keyword
It is used to access a global variable from within a function
<?php
$x = 5;
$y = 10;
function myTest()
{
global $x, $y;
$y = $x + $y;
}
myTest();
echo $y;
?>
O/P: 15
static Keyword
Normally, when a function is executed, all of its variables are
deleted. If want a local variable NOT to be deleted and we need
it for a further job then declare them with static keyword.
<?php
function myTest()
{
static $x = 0;
echo $x;
$x++;
}
myTest();
myTest();
myTest();
?>
O/P: 012
PHP String
A string is a sequence of characters.
A string can be any text inside single or double quotes.
<?php
$x = "Hello world!";
$y = 'Hello world!';
echo $x;
echo "<br>";
echo $y;
?>
O/P: Hello world!
Hello world!

PHP Integer
An integer data type is a non-decimal number
PHP Float
A float is a number with a decimal point or a number in
exponential form.
PHP Boolean
A Boolean represents two possible states: TRUE or FALSE.
PHP Array
An array stores multiple values in one single variable.
<?php
$cars = array("Volvo","BMW","Toyota");
?>
PHP Object
An object data type stores data and information on how to
process that data.
In PHP, an object must be explicitly declared.

<?php
class Car
{
function Car()
{
$this->model= "VW";
}
}
$c1 = new Car(); // create an object
echo $c1->model;
?>

PHP NULL Value


A variable of data type NULL has no value assigned to it.

PHP Resource
The special resource type is not an actual data type.
It is the storing of a reference to functions and resources external
to PHP.
A common example of using the resource data type is a database
call.

You might also like