Variables are used to containers or you can say store data at runtime. And variable starts with the $ sign followed by variable name.

Conditions for PHP variables:

  • Variables of PHP starts with the $ sign, followed by the name of the variable
  • PHP variable name should be started with a letter or the underscore _ character
  • PHP variable name cannot start with a number and special symbols. Example: $1color and $@car.This is a wrong. This will create error at runtime.
  • PHP variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ) Example: $_car
  • PHP Variable names are case-sensitive ($age and $AGE are two different variables counts)

Example

<?php

$car= "BMW"; 
$x= "20";
 
?>

In above example, after the execution the script, the variable $car store/hold the value BMW and the variable $x store/hold the value 20.



Declaring PHP Variables

Now we will see how to store string, integer and float values in PHP variables.The assignment operator (=) used to assign value to a variable.
In PHP variable can be declared as: $var_name = value; .Let’s see below:

Example

<?php  
// Declaring variables

$str="PHP String";  
$int=123;  
$float=35.5;  

// Displaying variables value

echo "string is: $str <br/>";  
echo "integer is: $int <br/>";  
echo "float is: $float <br/>";  
?>  

Output:

string is: PHP String

integer is: 123

float is: 35.5


PHP Variable: multiplication of two variables

Example

<?php
$x = 10;
$y = 2;
echo $z= $x * $y;
?>

Output:

20




Loosely typed language

If you will have to noticed above example that we did not have to mention PHP which data type the variable is. PHP automatically assigned a data type to the variable



PHP Variables Scope

  • local
  • global
  • static

1. PHP Global Variable Scope

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

Example

<?php
$x1 = 3;
$y1 = 5;
function Sum()
{
 global $x1, $y1;
 $y1 = $x1 + $y1;
} 
Sum();
echo $y1;
?>

Output:

8

The above example will output 8. By declaring $x1 and $y1 global within the function, all references to either variable will refer to the global version. There is no limit to the number of global variables that can be manipulated by a function.

Example #2

A second way to access variables from the global scope is to use the special PHP-defined $GLOBALS array. The previous example can be rewritten as:

Example #2 Using $GLOBALS instead of global

<?php
$x1 = 3;
$y1 = 5;

function Sum()
{
    $GLOBALS['y1'] = $GLOBALS['x1'] + $GLOBALS['y1'];
} 

Sum();
echo $y1;
?>




2. PHP Local Variable Scope

A variable declared within a function has a LOCAL scope and can only be accessed within that function:

Example

<?php
$a = 10; // Local Scope

function local_var() {
    // using a inside this function will generate an error
    echo "<p>Variable a inside function is: $a</p>";
}
local_var();
local $a ;
echo "<p>Variable x outside function is: $a</p>";
?>

Output:

Variable x inside function is: 5
Variable x outside function is:


Pin It on Pinterest

Shares
Share This