PHP Variables

Variables are "containers" for storing information.

Creating or Declaring PHP Variables


  • In PHP, a variable starts with the $ sign, followed by the name of the variable
  • A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume).
  • 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)

Code:
<?php
$text = "Hello world!";
$x = 5;
$y = 10.5;
echo $text . "<br>";
echo "My Text is $text <br>";
echo "My Text is " . $text . "<br>";
echo $x + $y . "<br>";
?>

Output:
Hello world!
My Text is Hello world!
My Text is Hello world!
15.5

PHP is a Loosely Typed Language


In the example above, notice that we did not have to tell PHP which data type the variable is.

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.

PHP Variables Scope


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

Code: Variable with global scope
<?php
$x = 5; // global scope
function myTest() {
// using x inside this function will generate an error
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
echo "<p>Variable x outside function is: $x</p>";
?>

Output:

Variable x inside function is:

Variable x outside function is: 5

Code: Variable with local scope:
<?php
function myTestLocal() {
$y = 5; // local scope
echo "<p>Variable y inside function is: $y</p>";
}
myTestLocal();
echo "<p>Variable y outside function is: $y</p>";
?>

Output:

Variable y inside function is: 5

Variable y outside function is: 10.5

PHP The global keyword


The global keyword is used to access a global variable from within a function. To do this, use the global keyword before the variables (inside the function)

Example:
<?php
$x = 5;
$y = 10;
function myTest() {
global $x, $y;
$y = $x + $y;
}
myTest();
echo $y; // outputs 15
?>

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.

Example:
<?php
$x = 5;
$y = 10;
function myTest() {
$GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];
}
myTest();
echo $y; // outputs 15
?>

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.

Example:
<?php
function myStaticTest() {
static $z = 0;
echo $z;
$z++;
}
myStaticTest();
echo "<br>";
myStaticTest();
echo "<br>";
myStaticTest();
?>

Output:
0
1
2