PHP - Variables

  »   PHP  »   PHP Tutorial - Variables

PHP variables are just a name for a value and you can simply use it like this $var_name = value;. They usually store sting characters and numeric values for later use in the programming block. Here is a simple example:

$a 	= "Welcome";
$b 	= "Hello";
$first 	= "John";
$last 	= "Doe";
$c 	= ", how are you today?";

echo "$a $first $last <br />"; 	// Returns: Welcome John Doe
echo "$b $first $c <br />";    // Returns: Hello John, How are you today?

echo $x+$y;  // Returns: 15.1

Note: There is no need to previously declare the variable before assigning it a value like in C or Java for example. The type of variable is assigned dynamically depending on its value.

What is a variables scope types in PHP?

In PHP, the scope of a variable is the part of the script where a variable was defined and can be referenced. There are three types of scopes in PHP.

  • local - declared within a function and can only be accessed within that specific function
  • global - declared outside a function and can only be accessed outside a function. There is an exception for this when the global keyword is used. Keep on readding
  • static - declared withing a function and maintaining his value in that function

Special keywords for declaring variables socpes

There are cases when you declare a variable outside of a function and you want to use it inside and the other way around.

This is were the global keyword enters.

$x = 1;
$y = 2;

function sum() {
    global $x, $y;
    $result = $x + $y;

echo $result; // Outputs: 3

On the other side static keyword is used to store a value of a varible declared inside a function, for consecutive cals. Normally all the local variable are resseted when the fnction closes, unless static keyword is used.

function count_table() {
    static $x = 0;
    echo "Value of x is now $x <br />\n";
    return $x++;

while(count_table() < 10){
    // do some other stuff

/* Outputs:
Value of x is now 0 
Value of x is now 1 
Value of x is now 2 
Value of x is now 3 
Value of x is now 4 
Value of x is now 5 
Value of x is now 6 
Value of x is now 7 
Value of x is now 8 
Value of x is now 9 
Value of x is now 10 

Things to remember about variables names

Good things:

  • PHP variables must start with a dollar simbol ($)
  • PHP variables must start (after the dollar symbol) with a letter or underscore ( _ ).
  • PHP variables only admits alpha-numeric characters and underscores (a-z, A-Z, 0-9, or _ ).
  • PHP variables are case sensitive so $myVar si not the same as $MyVar

Bad things:

  • PHP variables can not start with a number.
  • PHP variables names can not contain symbols (except the starting dollar sign).

CamelCase vs snake_case

It is a good to use descriptive-not-to-long names. If your variable name contains various words you can use different techniques to make your programming blocks readable and self self explainable. Regardless of witch one you chose, it is a good practice to stick to it.

  • $snake_case - saparate words using underscore
  • $UpperCameleCase - use capitalization to separate words
  • $lowerCamelCase - same as previous exept that the first letter is a lower case

Just chouse one and stick to it acrose your php documents.

Are you kidding me: Variable variables and function variables?

Well, I am not kidding. PHP allows you to use a dynamically assigned name for the variable or a function. Take a look at the flowing code:

$a = 'hello';
$$a = 'world';

echo $a." ".$$a."<br />";    // Returns hello world
echo $a." ".$hello;  // Returns hello world

There is a quite cool example of variable variable on the page that I would like to post it here:

$Bar = "a";
$Foo = "Bar";
$World = "Foo";
$Hello = "World";
$a = "Hello";

$a; // Contains Hello
$$a; // Contains World
$$$a; // Contains Foo
$$$$a; // Contains Bar
$$$$$a; // Contains a

You can read more on the php manual site:

Using a variable as function name can be quite usefull in some scenarious, like hooks, filters, callbacks, and meny others. PHP will try to call the function using the value of your variables.

Variable function example:

function foo() {
    echo "I am a string inside foo";

$bar = "foo";
$bar();    // outputs: I am a string inside foo

This way you can dynamically call one function or another. There are much more examples of implementations and you can read more about function variable on the php manual site: