PHP

Variable declaration in PHP-

Variables in PHP are declared by a dollar sign followed by the name of the variable. The variable name is case-sensitive.Rules for declaring PHP variables:

  • A variable name starts with the $(dollar) sign, followed by the name of the variable.
  • A variable name can only begin with a letter or the underscore character.
  • A variable name cannot begin with a number.
  • A variable name can only have alpha-numeric characters and underscores (A-z, 0-9, and _ ).
  • PHP Variable names are case-sensitive ($age and $AGE are two different variables).
  • A variable is name of memory address where the information or data are stored.

Syntax of declaring variable in php-

$variableName = value








$year = 2019;

$salary = 50000.0;

$name = "PHP";

?>





The following example will demonstrate the sum of two variables:




$m = 20;

$n = 6;

echo "The sum of $m and $n are = ".$m + $n;

?>

Output: The sum of 20 and 6 are = 26

PHP is a Loosely Typed Language -

A loosely typed language that does not require you to declare a variable type when declaring a variable.

PHP automatically change the variable to the correct data type, depending on its value.

In other programming languages such as objective-c , swift ,C, C++ and Java, the programmer must declare the name and type of the variable before using it.

Scope of PHP variables -

Variables can be declared anywhere in PHP script.

The scope of a variable is the section of the code in which the variable is visible or can be used.

PHP has three different variable scopes:-

  • local
  • global
  • static
  • Global and Local Scope

A variable declared outside a function has a GLOBAL SCOPE for example -



$x = 10; // global scope

function globalTest() {

// Throw error when using x inside this function.

echo "

Variable x inside function is: $x

";


}

globalTest();

echo "

Variable x outside function is: $x

";


?>

A variable declared within a function has a local scope and can only be accessed within the function for example-



function localTest() {

$x = 20; // local scope

echo "

Variable x inside function is: $x

";


}

localTest();

echo "

Variable x outside function is: $x

";


?>

The global keyword-

PHP global variables must be defined as global inside a function if they are going to be used in that function for example-




$a = 1;

$b = 2;

function Sum()

{

global $a, $b;

$b = $a + $b;

}

Sum();

echo $b;

?>
Note- PHP also stores all global variables in an array called $GLOBALS[index]. The index holds the name of the variable.The previous example can be rewritten as by using PHP $GLOBALS keywords:


$a = 1;

$b = 2;

function Sum()

{

$GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];

}

Sum();

echo $b;

?>
Note:- The $GLOBALS array is an associative array with the name of the global variable being the key and the contents of that variable being the value of the array element.

Static keywords in php:-

Declaring class properties or methods as static makes them accessible without creating an instance of that class. A member variable declared as static cannot be accessed with an instantiated class object.

when a function is executed, all of its variables are destroyed. but sometimes we want a local variable not to be destroyed. programer can achieve it by  using static keyword. for example-

 



function staticTest() {

static $x = 0;

echo $x;

$x++;

}




staticTest();

staticTest();

staticTest();

?>

Output:

0

1

2

Back to Top