PHP Variables

Just like other programming languages, PHP has variables and constants. A variable is a storage space paired with an associated symbolic name that stores a particular value. All PHP variables start with the $ (dollar) sign, followed by the name of the variable.

This article explains the usage of variables in a block of PHP code.

What Is a PHP Variable?

A variable is a storage site that is useful to store various types of values, such as numeric values, characters, etc., that uses in any part of a program. Variables are associated with a particular name. 

Read Also: PHP Comments

Features of PHP Variables

  • PHP directly converts the variable to the correct data type, depending on its value. There is no need to declare data-type before adding value to it.
  • Once we declare a variable, it reuses throughout the code.
  • PHP assigns value to the variable using the assignment (=) operator.

Syntax of PHP Variable

$variablename=value;

Example

$color = Blue;

Naming Conventions for PHP Variables

These are the following rules for naming variables in PHP:

  1. In PHP, all variables start with a $ sign, followed by the name of the variable.
  2. Variable name in PHP starts with a letter or underscore(_).
  3. A PHP variable contains alphanumeric (A-z,0-9) characters and underscores.
  4. A PHP variable cannot start with a number.
  5. PHP variables are Case-Sensitive.
  6. A PHP variable name cannot contain spaces.
  7. A PHP variable can have long descriptive names like ($factorial, $even_nos) or short names like ($n or $f or $x).

Example 1

<?php

//valid variable declarations in PHP

$val = 5;

$val2 = 2;

$x_Y = "errorsea";

$_X = "Suman";

// Invalid declarations in PHP, uncomment the below variables and you will get errors

// begins with a number

// $10_val = 10;

// This is also invalid as it contains special character other than underscore(_)

// $a.b = "num";

?>

Example 2

<?php

$color="red";  

echo "My car is " . $color . "<br>";  

echo "My house is " . $COLOR . "<br>";  

echo "My boat is " . $coLOR . "<br>";  

?>

Output

My car is red
My house is 
My boat is

PHP variable names are case-sensitive. So variable name “color” is different from Color, COLOR, COLor etc.

PHP Variable Scope

In PHP, the variables can be declared anywhere in the script. In PHP, we can declare the variable for a particular scope.

PHP has three variable scopes.

  1. Local Variables
  2. Global Variables
  3. Static Variables

Local Variables

The variables declared within the function are called local variables. In simple words, it cannot be accessed outside that particular function.

Example

<?php

$num = 60;

function abc() {

    $num = 50;
    echo "local num = $num <br>";

}

abc();
echo "Variable num outside abc() is $num";

?>

Output

local num = 50 
Variable num outside abc() is 60 

This variable $num is local to the abc() function and the variable $num outside this function is a different variable.

Global Variables

The variables declared outside the function are known as global variables. These variables can be accessed from anywhere within the PHP code block. To use within a function, we need to use the “global” keyword before the variable.

Example

<?php

$num = 20;

function abc() {
    global $num;
    echo "Variable num inside function : $num <br>";
}

abc();
echo "Variable num outside function : $num";

?>

Output

Variable num inside function : 20 
Variable num outside function : 20 

We have to use the global keyword before the variable $num to access within the function.

Static Variables

When a function executes all of its variables, the variables lose their original values due to regular increment or decrement or other operations performed in it. But the original value might be needed later by the developer. To retain the original value of the variable even when other operations are performed on it, we have to use the static keyword when we first declare the variable.

Example

<?php

function abc() {

    // static variable
    static $num = 5;
    $sum = 2;

    $sum++;
    $num++;
    echo $num, "<br>";
    echo $sum, "<br>";

}

abc();
abc();

?>

Output

6
3
7
3

The variable $num retains its value and regularly increments even after the first function call. However, $sum doesn’t because $sum is not a static variable.

Read More: PHP Echo & Print Statements

Conclusion

Variables play an important role in programming because they enable the programmer to write flexible programs rather than writing long pieces of code with frequently occurring values. A programmer uses variables to store and represent the data.

We hope we have been able to help you on your next step in the endeavor of conquering the PHP language. Keep reading and implementing the further modules’ steps, and check out the previous posts if you haven’t. Happy Learning!

Leave a Reply

Your email address will not be published. Required fields are marked *

Read previous post:
php comments
PHP Comments

Comments in PHP are similar to the comments used in HTML. A comment in PHP code is the non-executable part...

Close