PHP Variables and constants

Website Theme

PHP Variables and constants

March 2nd, 2007

Variables introduction

Variables are used by programmers to store values and to refer to those values in code. Variables in PHP need not be defined. Variable types also do not exist although variable scope does. All variables begin with a $ sign. Variable names can contain numbers, letters and underscores. Variable names can only begin with letters or underscores. PHP variables are case sensitive. For ex: $a is different from $A. Below are given some examples of valid variable names:

$x = '5'; // $x contains the string '5'
 
$y = "Abdussamad's webspace"; // $y contains the string "Abdussamad's webspace"
 
$welcome = '$y is this site'; //$welcome contains the value $y is this site
 
$welcome2 = "$y is this site";//$welcome2 contains the value Abdussamad's webspace is this site

Assigning data to variables

To assign data to a variable you place the variable name followed by an equal sign and then the value to be assigned to the variable. Numbers are assigned in this fashion

$x = 5; // $x contains the value 5
$y = 5.5; // $y contains the value 5.5
$BigNumber = 5678; // $BigNumber contains the value 5678

Strings of characters must be quoted when they are assigned to a variable. Strings maybe single quoted or double quoted. The difference between single quoting and double quoting a string is that double quoting a string allows you to use special characters such as line feeds. Furthermore if a variable is specified in double quotes its value is used but this is not the case when you use single quotes. My recommendation to you is to always double quote a string. Below are some examples:

$x = '5'; // $x contains the string '5'
 
$y = "Abdussamad's webspace"; // $y contains the string "Abdussamad's webspace"
 
$welcome = '$y is this site'; //$welcome contains the value $y is this site
 
$welcome2 = "$y is this site";//$welcome2 contains the value Abdussamad's webspace is this site

Arithmetic operations

Some examples of common arithmetic operations is given below:

$x = 6 + 7;//Addition operation. $x is equal to 13
 
$x = $x + 2; //$x is now equal to 15
 
$x += 2; //This is the same as $x=$x+2. Therefore $x is now equal to 17
 
$y = 5 - 3 ; //subtraction
 
$y = 6 / 2 //$y now equals 3

It is possible to perform arithmetic using C type short notation. For ex:

$x = 15;
$x += 2; //This is the same as $x=$x+2. Therefore $x is now equal to 17
$x ++; //increment the value of $x by 1. $x is now equal to 18
 
$x --; //decrement $x by one
 
$x *= 2; // $x now equals 17 *2=34

And so on.

Constants

Constants are variables whose values cannot be changed. Constants are defined using the define statement

define( "MY_CONSTANT", "I love php" );
 
echo MY_CONSTANT; // displays I love php

The echo statement above is used to display the value of MY_CONSTANT. As you can see from the code example above $ signs are not used with constants. Although not a requirement, it is part of coding convention to use uppercase constant identifiers such as MY_CONSTANT above.

Arrays

Arrays allow you to store a series of values in successive memory locations and refer to them using a common variable. An array can be defined in multiple ways in PHP. Which way you choose to define an array depends on what is more convenient to you and your coding style. Lets see how an array is defined in PHP:

$my_array = array(); //define an empty array
 
$my_other_array = array( 1, 2, 4, 5 ); //define an array filled with values.

Each element of an array consists of two portions. The index or key and the value. The index is used to set and retrieve the value of an array element. By default PHP assigns integer indices starting from 0. For ex:

 
$my_other_array = array( 1, 2, 4, 5 );
 
echo $my_other_array[ 0 ]; //Will print 1
 
echo $my_other_array[ 3 ]; //will print 5
 
$my_other_array[ 2 ] = 9;  // will replace 4 with 9 in the array

PHP also supports associative arrays. An associative array can contain textual indices. For ex an address book array:

 
$my_array = array(
                "Ahmed Ibrahim" =>"No. 45, Gulistan-e-jauhar, Karachi",
                "Junaid Qasim" =>"Block 5/A Shon Circle Clifton"
            );

You would access the array in this fashion

echo $my_array[ "Ahmed Ibrahim" ]; //Will print No. 45, Gulistan-e-jauhar, Karachi
Multi dimensional arrays

PHP allows you to create multi dimensional arrays by creating sub arrays within an array:

$2d = array(
         array( 2, 4, 6 ),
         array( 1, 3, 5 )
      );
 
echo $2d[ 0 ][ 0 ]; //will print 2
 
echo $2d[ 1 ][ 2 ]; //will print 5

Finally it is possible to create an element without specifying the exact index. PHP will use the index one greater than the index of the last element of the array.

$pretty_array = array( 6, 4, 9 );
$pretty_array[] = 10;
echo $pretty_array[ 3 ]; //will print 10

One Response to “PHP Variables and constants”

  1. Michael says:

    Hello Abdussamad?
    Thanks alot for the tutorial, it has realy inspired me into having an intrest In PHP. I am an IT student in Nairobi Kenya and i believe i have found my new love “PHP”. I find it easy and interesting by the fact that it is similar to programming in ‘C’. I find ur site great n Il continually come to here hoping to find more tutorials on PHP.

    Posted on 30 Jul 2008 Reply

Leave a Reply

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