PHP Variables

PHP also has variables and constants, just like other programming languages. Variable is the storage space where we can store the values. All variables in PHP start with a $ (dollar) sign, followed by the name of the variable.

This article explains to us how to use the PHP variables in the source code.

What Is a PHP Variable?

A variable is a simple container, which is useful to store various type of values such as numeric values, characters etc. that uses in any part of a program.

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 assignment (=) operator.

Syntax of PHP Variable

$variablename=value;

Example

$color = Blue;

Naming Conventions for PHP Variables

There are 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 have a 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 = "abc";

$_X = "Suman";

//invalid declarations in PHP

// 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 from outside of the function.

Example

<?php 

 $num = 60; 

 function abc() {   
   $num = 50; 
   echo "local num = $num \n"; 
 } 
 
 abc(); 
 echo "Variable num outside abc() is $num \n"; 

?> 

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 directly from anywhere. 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 \n"; 
 } 

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

?> 

Output

Variable num inside function : 20 

Variable num outside function : 20 

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

Static Variables

When a function executes all of its variables lose its value sometimes, we want a local variable for further process. For this, 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, "\n"; 
    echo $sum, "\n"; 

 } 
 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 entering data directly into a plan. A programmer uses variables to represent the data.

I hope you have a great understanding of PHP variables.

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 not a part...

Close