Sponsored Links :
  • PHP


    Variables are probably one of the most commonly used code constructs you’ll come across while programming. If you have any experience of mathematics, especially algebra, you’ll be aware of the concept of using symbols to represent unknown quantities in equations, like in the following example

    x + 5 = 7;
    from this we can deduce that x = 2

    In PHP it works in a similar fashion, except we usually know the value of x, and simply assign it a specific value, such as x = 5. We can then use that value to perform calculations using other symbols and numbers. For example

    $x = 5  //x now equals 5
    $y = $x + 10  /* y now equals 5 + 10 */

    These symbols in PHP are known as variables and require you to follow a specific syntax to use them properly. Once you understand the principles behind them, you’ll begin to see how they form a major component of any PHP application, and can be used to store many things other than just numbers. Let us begin though, by guiding you through a few simple examples of the use of variables.

    A simple introduction

    A very simple example of the use of a variable in PHP is to hold just one number, e.g.

    $x = 5;

    All variables in PHP start with a dollar sign, followed by a string of numbers, letters (both lower-case and capitals) and underscores. Variables are also case sensitive, meaning that $Variable is not equal to $variable. The only other requirement of variable naming is that the first digit cannot be a number. These are examples of valid variable names

    $variable_name = 5; //valid - integer
    $VariableName = 25.5; //valid - floating point
    $variable3 = 155; //valid
    $_variable_number_four = "value"; //valid - string
    $_x = "five"; //valid

    These are not valid variable names

    $1st_variable = 5; //invalid
    $Variable.name = 25; //invalid, cannot have dots in names

    Variables are assigned values through the use of the equals sign (=) operator. Once a variable has been assigned to a value, that variable symbol can be used in place of the value anywhere in the code. The value of the variable can also be changed at any point during the execution of the code. The same value can also be assigned to multiple variables at the same time, like so

    $a = $b = $c = 5;
    echo "$a, $b, $c";

    This produces the output

    5, 5, 5

    Flexible Types

    The “type” of a variable is defined by the value you’ve assigned to it. PHP supports a wide range of types, including strings, integers and floats.

    $string = "This is a string";
    $integer = 5; //an integer value;
    $float = 4.55; //a "floating point" value

    PHP doesn’t explicitly require that you maintain the same type for a certain variable though. If you tried to assign a string value to a variable which previously contained an integer, you’ll encounter no problems. Similarly with any other types, floating point variables can hold integers or strings and so on.

    <<>>

Leave a Comment

Want to ask a question about anything in this tutorial? Have you spotted an inaccuracy, or noticed areas for improvement? Fancy just having a chat? Leave your comments below...

Recommended Reading from Amazon.com

Previous Tutorial
Combining PHP with HTML


Next tutorial
Arrays in PHP