Google News
logo
PHP Variables
PHP Variables are used for storing data such as numeric values, characters, character strings etc. All PHP variable names must be pre-fixed with a ( $)It is prefix which informs the PHP pre-processor that it is dealing with a variable name must start with a letter or the underscore character. A variable name cannot start with a number. Variable names are case-sensitive they are ($var and $VAR are two different variables).

Syntax of declaring a PHP valid and invalid variable names :

$varName         // valid
$_varName       // valid
$__varname    // valid
$var_name       // valid
$varName21     // valid
$_var-Name  // invalid contains non alphanumeric character (-)
$_9Var  // invalid - underscore must be followed by a letter
$99Var   // invalid - must begin with a letter or underscore

Example :
<!DOCTYPE html>
<html>
<head>
<title>PHP Variables</title>
</head>

<body>

	<?php
        $ftl = "www.freetimelearning.com";
        echo "My Website is : " . $ftl;
    ?>
    <p>(or)</p>
    <?php
        echo "$ftl";
    ?>
    <p>(or)</p>
    <?php
        echo $ftl;
    ?>
    
</body>
</html>
Output :
PHP Variable - case sensitive
In PHP, variable names are case sensitive. So variable name "ftl" is different from fTl, ftl, FTL etc.
<!DOCTYPE html>
<html>
<head>
<title>PHP Case Sensitivity</title>
</head>
<body>

<h4>PHP Case Sensitivity</h4>
<?php
$ftl = "www.freetimelearning.com";
echo "My Website is : " . $ftl . "<br><br>";
echo "My Website is : " . $fTl . "<br><br>";
echo "My Website is : " . $FTL;
?>
    
</body>
</html>
Output :
PHP is a Loosely typed language

In other languages such as C, C++, and Java, the programmer must declare the name and type of the variable before using it.

The PHP automatically converts the variable to its correct data type.

PHP variable($) and variables($$)

The $var it is a normal variable with the name var that stores any value like string, integer, float, etc. And the $$var is a variable that stores the value of the $variable inside it.

<!DOCTYPE html>
<html>
<head>
	<title>PHP variable($) and variables($$)</title>
</head>

<body>
	<?php  
	$name = "FreeTimeLearning";  
	$$name = "www.freetimelearning.com";  
	echo $name."<br/>";  
	echo $$name."<br/>";  
	echo $FreeTimeLearning;  
	?>   
</body>
</html>
Output :

In the above example, we have assigned a value to the variable name as FreeTimeLearning. Value of reference variable $$name is assigned as www.freetimelearning.com.

Now we have printed the values $name, $$anme and a string.

Another Example
<!DOCTYPE html>
<html>
<head>
	<title>PHP variable($) and variables($$)</title>
</head>

<body>
	<?php
		$x = "FTL";
		$$x = "900";
		echo $x."<br/>";
		echo $$x."<br/>";
		echo $FTL;
	?> 
</body>
</html>
Output :

In the above example, we have assigned a value to the variable x as FTL. Value of reference variable $$x is assigned as 900.

Now we have printed the values $x, $$x and $FTL.

Example
<!DOCTYPE html>
<html>
<head>
	<title>PHP variable($) and variables($$)</title>
</head>

<body>  
    <?php  
	$name="FreeTimeLearning";  
	${$name}="freetimelearn";  
	${${$name}}="www.freetimelearning.com";  
	echo $name. "<br>";  
	echo ${$name}. "<br>";  
	echo $FreeTimeLearning. "<br>";  
	echo ${${$name}}. "<br>";  
	echo $freetimelearn. "<br>";  
	?>  
</body>
</html>
Output :

In the above example, we have assigned a value to the variable name FreeTimeLearning. Value of reference variable ${$name} is assigned as freetimelearn and ${${$name}} as www.freetimelearning.com.

Now we have printed the values as $name, ${$name}, $FreeTimeLearning, ${${$name}} and $freetimelearn.

PHP Variable Scopes

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

There are three different types of variable scopes.

  • local
  • global
  • static
Global and Local Scope

The local scope where variables are created and accessed inside a function and global scope where variables are created and accessed outside a function.

<!DOCTYPE html>
<html>
<head>
	<title>Global and Local Scope</title>
</head>
<?php
//don't disply erroes
error_reporting(0);
?>
<body>
	<?php  
		//global scope  
		$x = 18;  
		function Sample()  
		{  
		//local scope  
		$y=90;  
		echo "The x value is :  $x "."<br /><br />";  
		echo "The y value is  :  $y"."<br />";  
		}  
		Sample();  
		echo "The x value is :  $x"."<br /><br />";  
		echo "The y value is  :  $y";  
	?>   
</body>
</html>
Output :
The global keyword

The global keyword is used to access a global variable from within a function.

To use these variables inside a function the variables must be declared global in that function. To do this we use the global keyword before the variables. They are following example :

<!DOCTYPE html>
<html>
<head>
	<title>Global Keyword</title>
</head>

<body>
	<?php  
		$x=3;  
		$y=6;  
		$z=9;  
		$xyz=0;  
		function multiple()  
		{  
		global $x, $y, $z, $xyz;  
		$xyz=$x*$y*$z;  
		}  
		multiple();  
		echo $xyz;  
	?>  
</body>
</html>
Output :
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. To do this, use the static keyword when you first declare the variable :

<!DOCTYPE html>
<html>
<head>
	<title>Static Keyword</title>
</head>

<body>
	<?php
		function Sample() {
			static $x = 3;
			echo $x;
			$x++;
		}
		
		Sample();
		echo "<br>";
		Sample();
		echo "<br>";
		Sample();
	?> 
</body>
</html>
Output :