PHP

Variable declaration in PHP-

Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive.

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).

A variable is name of memory location where the value or data are stored. Syntax of declaring variable in php-

$variableName = value

<!DOCTYPE html>
<html>
<body>

<?php
$year = 2019;
$salary = 50000.0;
$name = "PHP";

?>

</body>
</html>

The following example will output the sum of two variables:


<?php
$x = 20;
$y = 6;
echo "sum of $x and $y = ".$x + $y;
?>
Output: sum of 20 and 6 = 26

PHP is a Loosely Typed Language-


A loosely typed language such as PHP is a language that does not require you to declare a variable type when declaring a variable. In the above examples the data type of variables are not mentioned.
PHP automatically converts the variable to the correct data type, depending on its value.

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

PHP variables Scope-


In PHP, variables can be declared anywhere in the script.

The scope of a variable is the part of the script where the variable 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 and can only be accessed outside a function:

<?php
$x = 10; // global scope

function globalTest() {
// using x inside this function will generate an error
echo "<p>Variable x inside function is: $x</p>";
}
globalTest();

echo "<p>Variable x outside function is: $x</p>";
?>

A variable declared within a function has a local scope and can only be accessed within that function:

<?php
function localTest() {
$x = 20; // local scope
echo "<p>Variable x inside function is: $x</p>";
}
localTest();

// using x outside the function will generate an error
echo "<p>Variable x outside function is: $x</p>";
?>

The global keyword

In PHP global variables must be declared global inside a function if they are going to be used in that function. for example-
Example #1 Using global

<?php
$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:
<?php
$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 needing an instantiation of the class. A property declared as static cannot be accessed with an instantiated class object.
when a function is completed/executed, all of its variables are deleted. However, sometimes we want a local variable NOT to be deleted. You can achieve it by static keyword. for example-

<?php
function staticTest() {
static $x = 0;
echo $x;
$x++;
}

staticTest();
staticTest();
staticTest();
?>

Output:

0
1
2

Back to Top