PHP Variables


In this article, you will learn about PHP variables, how they are declared and what is their scope in PHP.

php variables

PHP Variables: Definition

A variable is a name given to a memory location that stores data at runtime. Variables are used to store data, like a string of text, numbers, etc. So it’s fair to say that variables are like containers that hold or store some information that can be of any type depending on the data type. Variable values can change over the course of a script.

Here’re some important things to know about variables:

  • In PHP, a variable does not need to be declared before adding a value to it. PHP automatically converts the variable to the correct data type, depending on its value.
  • After declaring a variable it can be reused throughout the code.
  • The assignment operator (=) used to assign value to a variable.
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • All variable names must start with the dollar sign e.g. $var
  • Variable names are case sensitive; this means $my_var is different from $MY_VAR
  • All variable names must start with a letter followed by other characters e.g. $my_var1.  $1my_var is not a legal variable name.
  • Variable names must not contain any spaces i.e $first name is not a valid variable name.

How to declare PHP variables?

In PHP, a variable starts with the $ sign:

Example:

<?php
$my_var = 5;
echo $my_var;
?>

Output:

5

Floating point numbers

<?php
$my_var = 3.1416;
echo $my_var;
?>

Output:

3.1416

 

Character strings

<?php
$my_var ="hello there";
echo $my_var;
?>

Output:

Hello World

Use of PHP Variables

  • Variables help separate data from the program algorithms.
  • The same algorithm can be used for different input data values.
  • For example, suppose that you are developing a calculator program that adds up two numbers, you can create two variables that accept the numbers then you use the variables names in the expression that does the addition.

PHP: Loosely typed language

By loosely typed language, it means PHP automatically converts the variable to its correct data type. We don’t need to tell PHP about the data type of the variable.

PHP Variables Scope

In any programming language, the scope defines the lifetime of a variable and also where a variable can be used or accessed from.

PHP has three different variable scopes:

  • Local

A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function.

<!DOCTYPE html>
<html>
<body>
<?php
function trytoprogram() {
$x = “try to program”; // local scope
echo "<p>I love $x</p>";
}
trytoprogram();
// using x outside the function will generate an error
echo "<p>I love $x</p>";
?>
</body>
</html>

Output:

I love try to program
  • Global

A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function

<!DOCTYPE html>
<html>
<body>
<?php
$x=  “try to program”;
function trytoprogram() {
global $x;
}
trytoprogram();
echo "<p>I love $x</p>";
?>
</body>
</html>

Output:

I love try to program
  • static

Static is a keyword which makes local variable not to be deleted. Usually, local variables are deleted after the execution of a  function is finished. Sometimes we might need local variables for further operations and in such cases, to prevent the deletion of local variables static keyword is used.

<!DOCTYPE html>
<html>
<body>
<?php
function trytoprogram() {
static $x = 0;
echo $x;
$x++;
}
trytoprogram();
echo "<br>";
trytoprogram ();
echo "<br>";
trytoprogram ();
?>
</body>
</html>

Output:

0
1
2

As you can see in the above example, the static variable will not be destroyed once the function is called and executed. Instead, it will store the value that it held in the previous execution of the function.