PHP Variables
PHP Variables
A variable is a name given to a memory location that stores data at runtime. Variables are "containers" for storing information. In PHP, a variable starts with the $ sign, followed by the name of the variable.
Unlike other programming languages, PHP has no command for declaring a variable. It is created the moment you first assign a value to it. Think of variables as containers for storing data.
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)
Output Variables
The PHP echo statement is often used to output data to the screen.
However; all variable names are case-sensitive!
<!DOCTYPE html>
<html>
<body>
<?php
$txt = "W3Schools.com";
echo "I love $txt!";
?>
</body>
</html>
The following example will output the sum of two variables:
<!DOCTYPE html>
<html>
<body>
<?php
$x = 5;
$y = 4;
echo $x + $y;
?>
</body>
</html>
PHP Variables Scope
The scope of a variable determines its visibility.A Php global variable is accessible to all the scripts in an application. A local variable is only accessible to the script that it was defined in.
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 referenced/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:
Variable with global scope:
<!DOCTYPE html>
<html>
<body>
<?php
$x = 5; // global scope
function myTest() {
// using x inside this function will generate an error
echo "Variable x inside function is: $x
";
}
myTest();
echo "<p>Variable x outside function is: $x</p>";
?>
</body>
</html>
A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function. You can have local variables with the same name in different functions, because local variables are only recognized by the function in which they are declared.
<!DOCTYPE html>
<html>
<body>
<?php
function myTest() {
$x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
using x outside the function will generate an error
echo "<p>Variable x outside function is: $x</p>";
?>
</body>
</html>
PHP The global Keyword
A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function.
PHP also stores all global variables in an array called $GLOBALS[index]. The index holds the name of the variable. This array is also accessible from within functions and can be used to update global variables directly.
<!DOCTYPE html>
<html>
<body>
<?php
$x = 5;
$y = 10;
//function myTest() {
// $GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];
// }
//myTest();
echo $y; // outputs 15
?>
</body>
</html>
PHP The static Keyword
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:
Then, each time the function is called, that variable will still have the information it contained from the last time the function was called. Note: The variable is still local to the function.
<!DOCTYPE html>
<html>
<body>
$_Global x;
</body>
</html>
PHP is a Loosely Typed Language
PHP automatically associates a data type to the variable, depending on its value. Since the data types are not set in a strict sense, you can do things like adding a string to an integer without causing an error.
In PHP 7, type declarations were added. This gives an option to specify the data type expected when declaring a function, and by enabling the strict requirement, it will throw a "Fatal Error" on a type mismatch.