Variables



Prev. Page
Index Page
Next Page
  • In PHP, variables may contain numeric data, character string data, and arrays. For now, we'll only look at numeric and string variables.
  • PHP variables always start with a dollar sign (like Perl).
  • After the dollar sign, the next character must be either a letter or an underscore character (_). Technically the word "letter" here includes the letters a to z (and A to Z) plus the ASCII characters numbered 128 through 255. In practice, it's probably best to stick to the actual Latin alphabet.
  • After the dollar sign and leading character, you may include digits in variable names.
  • Variable names are case sensitive. It's best to get into the habbit of using a standard capitalization scheme. For me, that means variable names are usually lowercase with underscores between the sub-words. Other people prefer so-called StudleyCaps.
  • Values are assigned to variables using one of the assignment operators, the most common of which is = which assigns the value of the expression on the right to the variable on the left.
    $name    = 'Henry' ;		// Assigns the string "Henry" to
    				// the variable $name
    $name    = "$last, $first ;	// Assigns the value of $last,
    				// followed by a comma and space,
    				// followed by the value of $first
    $sallary = 10.50 * 2080 ;	// Assigns 21,840 to the variable
    				// $sallary
    $sallary = $rate * $hours ;	// Assigns the product of
    				// multiplying the value of $rate 
    				// by the value of $hours
    
  • PHP supplies many predefined variables to you:
    • $_SERVER - Variables set by the web server or otherwise directly related to the execution environment of the current script. ($HTTP_SERVER_VARS)
    • $_GET - Variables provided to the script via HTTP GET. ($HTTP_GET_VARS)
    • $_POST - Variables provided to the script via HTTP POST. ($HTTP_POST_VARS)
    • $_COOKIE - Variables provided to the script via HTTP cookies. ($HTTP_COOKIE_VARS)
    • $_SESSION - Variables which are currently registered to a script's session. ($HTTP_SESSION_VARS)
  • For the most part all PHP variables only have a single scope. This single scope spans included and required files as well. For example:
    <?php
    	$a = 1;
    	include "b.inc";
    ?>
    

    Here the $a variable will be available within the included b.inc script. However, within user-defined functions a local function scope is introduced. Any variable used inside a function is by default limited to the local function scope. For example:

    <?php
    	$a = 1; /* global scope */ 
    
    	function Test() { 
    		echo $a; /* reference to local scope variable */ 
    	} 
    
    	Test();
    ?>
    

    This script will not produce any output because the echo statement refers to a local version of the $a variable, and it has not been assigned a value within this scope. In PHP global variables must be declared global inside a function if they are going to be used in that function. An example:

    <?php
    $a = 1;
    $b = 2;
    
    function Sum() {
        global $a, $b;
        $b = $a + $b;
    } 
    
    Sum();
    echo $b;
    ?>
    
  • There are six functions in PHP for determaning a variable's type: gettype(), is_array(), is_float(), is_int(), is_object(), and is_string().


Prev. Page
Index Page
Next Page

Copyright © 2003
Henry H. Hartley
All Rights Reserved