The PHP variables are the identifier names that allow us to store the data type inside it, such as strings, integers, arrays, objects, floats, and so on.

Read the PHP data types tutorial to learn more about the data types.

Anyway, the PHP variable should start with a dollar sign – “$”. But there are some conditions that are required when you write a PHP variable.

  • The PHP variable should not start with numbers or signs.
  • The variable should be started with a dollar sign and the identifier would start with only the character or an underscore.
  • The identifiers can include numbers, but again they shouldn’t start with a number.
  • You can use capital characters or small characters in the name of the variable.
<?php 
  $var_1_iable = "Hello PHP variables";
  $var = "Variable Text";
  $variable2 = array();
  $variable3 = 'single quotes';
?>

The previous section was a basic overview of the PHP variables. Let’s see the common uses.

PHP Variable References and Operators

Only one char allowed to use beside the dollar sign is a reference flag – “&”. Its mission is to get the main reference for the assigned variable.

Let’s see an example.

<?php 
  $var = "This is a reference";
  $variable = &$var;
  echo $variable; // This is a reference
?>

Use the sum operator with PHP variables

<?php 
  $one = 1;
  
  // To add another number for the previous number use the follow
  $one += 1;
  echo $one; // 2
?>

Also, you can use the concatenate operator to collect two strings together.

<?php 
  $string_1 = "Hello";
  $string_2 = "Peter";
  echo "$string_1 " . $string_2 . ", you are too late today.";
?>

PHP Variable Scope

The variable scope means a variable that is defined in another file or outside the PHP function. Let’s see an example.

<?php 

  $username = "Montasser";

  function username() {
    echo $username;
  }

  username();

?>

This function will show an error with an undefined $username variable. And that is happening because we already have a variable scope that should take a global word or $GLOBALS[“…..”] array inside the function.

<?php 

  $username = "Montasser";

  function username() {

    global $username; 
    echo $username;

  }

  username();

?>

Or using the $GLOBALS array

<?php 

  $username = "Montasser";

  function prnt() {
    $GLOBALS['username'] = "Peter";
  }

  prnt();
  echo $username; // Output: Peter

?>

Also, the scope variable can be included from another file.

<?php 

  $username = "Montasser";
  include "options.php"; // this file can use the $username variable

?>

Use PHP Variables in the Class

The PHP variables can appear in the class as an attribute. So they may take access modifiers like protected or private, etc.

Let’s see the following example and how to assign a value to the PHP variable located in the PHP class.

<?php 
  
  class Car {
     public $speed;      
  }
  
  // To assign a value for the speed attribute
  $obj = new Car();
  $obj->speed = 120;
  echo $obj->speed;

?>

In the next section, you will see how to use the PHP variables in the double-quoted.

Use PHP Variables in the Double Quotes and Curly Braces

As I mentioned in the PHP string tutorial, you will be able to use the PHP variable inside the double quotes or with curly braces. Let’s see an example for the double quotes.

<?php 
  $username = "Peter";
  $wife     = "Isabella";
  echo "It is a good day to see you again $username with $wife";
?>

Also, you can use the same example with the curly braces like the one below.

<?php 
  $username = "Peter";
  $wife     = "Isabella";
  echo "It is a good day to see you again {$username} with {$wife}";
?>

But you will not able to use the variable directly with the single quotes.

Anyway, let’s see what is PHP variable’s variable.

PHP Variables Variable.

The variable’s variable is a variable that contains a value, this value becomes another variable.

<?php 
  $name= "peter";
  $$name = "Montasser";
  echo $peter; // Montasser
  echo ${$name} // Montasser 
?>

Predefined PHP Variables

The predefined variables are the incoming data from the HTML form such as $_POST, $_GET, $_REQUEST, and so on.

And we can use these variables when submitting an HTML form like the below example.

Consider the following code created inside a form.php file.

<!-- form.php file  -->
<form action="submit.php" method="post">
  <input name="email" placeholder="Email" type="text"/>
  <input name="password" placeholder="Password" type="password"/>
  <input type="submit" name="submit" value="Login" />
</form>

To access these two fields, email and password, you have to create the submit.php file. Then receive all the form values with the predefined variables like the below code.

<?php 
  $username = $_POST["email"];
  $password = $_POST["password"];

 echo "Your username is {$username} and password is {$password}";
?>

Wrapping Up

In this tutorial, you understood what are PHP variables, variable scope, and variables variable. Also learned how to use them with examples.

With the global PHP keyword, you can access the variable scope.