Variables in PHP

In today’s topic, we will discuss variables in PHP, but before you know about variables you must know about data type in PHP. Previously we describe the various topic of PHP like what is php?, datatypes in php, Installation Of XAMPP In Any Windows, etc. If you haven’t seen it, hurry up!

Variables in PHP

Today we will discuss Variables in PHP, variables are used to store data and that variable can be used later in the program. The variable is assigned with the ‘$’ sign. Just like in other programming languages, there are some rules when we are creating variables in PHP, they are:

  • In PHP, variable starts with the $ sign, i.e: $var
  • In PHP, variable name must start with a letter or the underscore character, i.e: $var_php
  • In PHP, a variable name cannot start with a number.
  • In PHP, the variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ).
  • In PHP, variable names are case-sensitive ($php and $PHP are two different variables).

Remember that the variable names in PHP are case-sensitive!

Example

<?php
$x = 5;
$y = 4;
echo $x + $y;
?>
OUTPUT: 9
<?php
$txt = "GeektoCode.com";
echo "Provide by " . $txt . "!";
?>
OUTPUT: Provide by GeektoCode.com!

Variables Scope in PHP

Variables can be declared anywhere in the script.

There are three different variable scopes in PHP, they are:

  • local
  • global
  • static

Global Scope

The variables are declared outside a function that is GLOBAL SCOPE and it can only access outside a function.

Example

<!DOCTYPE html>
<html>
<body>
<?php
$x = 78; // global scope
 
function myTest() {
  // using x inside this function will generate an error
  echo "<p>Variable x inside function is: $x</p>";
} 
myTest();
echo "<p>x variable outside function is: $x</p>";
?>
</body>
</html>
OUTPUT:
Variable x inside function is:
Variable x outside function is: 89

LOCAL SCOPE

The variable is declared within a function that is LOCAL SCOPE and it can only access within that function.

In PHP  local variables are with the same name in different functions because local variables are only recognized by the function in which they are declared.

<!DOCTYPE html>
<html>
<body>

<?php
function myTest() {
  $x = 5; // local scope
  echo "<p>Variable x inside function is: $x</p>";
} 
myTest();

// using x outside the function will generate an error
echo "<p>Variable x outside function is: $x</p>";
?>

</body>
</html>
OUPUT:
Variable x inside function is: 89
Variable x outside function is:

global Keyword in PHP

In PHP, the global keyword is used to access a global variable from within a function.

To do this, an example is given below:

<!DOCTYPE html>
<html>
<body>

<?php
$x = 23;
$y = 33;

function myTest() {
  global $x, $y;
  $y = $x + $y;
} 

myTest();  // run function
echo $y; // output the new value for variable $y
?>

</body>
</html>
OUTPUT: 56

All global variables in PHP are stored in an array called $GLOBALS[elements]. The elements hold the name of the variable. This array can accessible from functions and can be used to update global variables directly.

static Keyword in PHP

When a function is executed in PHP, all of its variables are deleted. then, if we want a local variable that is NOT to be deleted.

To do this, we use the static keyword when we first declare the variable:

<!DOCTYPE html>
<html>
<body>

<?php
function myTest() {
  static $x = 6;
  echo $x;
  $x++;
}

myTest();
echo "<br>";
myTest();
echo "<br>";
myTest();
?> 

</body>
</html>
OUTPUT: 
 6
 7
 8

Leave a Reply

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