PHP variables start with a $ sign and can contain numbers and strings

<?php
$txt = "Hello PHP Programmer!";
$x = 25;
$y = 30;
$z = $x + $y;
?> 

Once you have added a value to a variable by assigning the value with a = sign, the variable can be used to perform calculations.

Difference between = and ==

Note that when assigning a variable you will use the = sign but when you perform a check you use the == sign

<?php
// Assign a variable to $x
$x = 5;

// Check to see if the value is 5
if ($x == 5){then do this...};
?>

Using Quotes with variables

When using string variables you have to close the string in quotes. Both single and double quotes are valid

<?php
// Add single quotes to a string variable
$x = 'This is my string data with a single quote';

// Add double quotes to a string variable
$x = "This is my string variable with double quotes";

// No quotes are being used with numbers
$x = 12;
$x = 15.2345;

?>

Declaring variables in PHP

Unlike other programming languages you do not have to declare the variable type. The moment you assign a value to the variable, PHP will detect the type.

Creating Variable Names

When naming your variables you can use short or long names like $x or $this_is_my_variable. When you assign names to variables it is important to consider how you will identify the variable in the future. Giving more specific names will make your life much easier.

<?php

// Assigning proper names to variables
$hair_color = "brown";
$eye_color = "blue";

// Compare to using non identifiable variables
$x = "brown";
$y = "blue";

?>

Rules for naming PHP Variables

  • A Variable starts with the $ sign and is followed by the name of the variable
  • A Variable can not start with a number
  • A Variable must always start with an underscore character or a letter
  • A Variable can only contain alpha-numeric characters and underscores ( a – Z , 0-9 , _ )
  • Variables are case sensitive ($myvariable, $MyVariable and $MYVARIABLE are all different variables
<?php

// Valid Variables
$x = 1;
$thisismyvariable = 1;
$This_Is_My_Variable = 1;

// Variables that is NOT Valid
$1 = 1;
$this&variable = 1;
$this variable = 1;

?>

How to display Variables on the screen

To display the variables on the screen you use the echo command. Note that you can use the variable with quotes or without. I personally like using it within the quotes as it offers me better readability, but the choice is yours.

<?php

// Using Variables with the echo command without quotes
$name = "Peter";
echo "My name is $name";

// Using Variables with the echo command with quotes
$name = "Peter";
echo "My name is ".$name."";

/* If you want to use quotes you have to append the variable using the . character */

?>

PHP Variable Scopes

Although variables can be declared anywhere within your PHP script, the scope determines where the variable can be used.

Variables in PHP has three different scopes

  • Local
  • Global
  • Static

PHP Local and Global Variable Scopes

When a variable is declared inside a function, the variable has a local scope and can only be used inside the function.

When a variable is declared outside a function it has a global scope and can only be used outside the function.

Variables can be passed to a function. Once a variable has been passed then the variable can be use inside the function.

<?php
// Declaring a global scope variable
$x = 5;

// Using the variable outside a function is valid
echo "The value of my variable is".$x."";

// Using the variable inside a function is not valid
function testingVariables()
{
   echo "My variable is ".$x."";
}

// Running this function will produce an error
testingVariables();

?>

A variable inside a function has a local scope and can only be used inside the function.

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

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