Welcome to our free PHP tutorial. This tutorial is based on Webucator's Introduction to PHP Training course.

Contact Us or call 1-877-932-8228


PHP variables begin with a dollar sign ($) as shown below.


$varName = "Value";

Variable Types

Variable Type Explanation
Integer whole number
Double real number
String string of characters
Boolean true or false
Array list of items
Object instance of a class

Variable Names (Identifiers)

Variable, function and class names are all identifiers and all follow the rules below, with the exception that function names are not case sensitive.

  • consist of letters, digits, underscores and dollar signs
  • cannot begin with a digit
  • are case sensitive

Type Strength

PHP is weakly typed, meaning that variables do not need to be assigned a type (e.g, Integer) at the time they are declared. Rather, the type of a PHP variable is determined by the value the variable holds and the way in which it is used.

Hello Variables!

Here is the "Hello World!" script again, but this time we use a variable.

Code Sample:

	$greeting = 'Hello World!';
<meta charset="UTF-8">
<title><?php echo $greeting; ?></title>
	echo $greeting;

This time the string "Hello World!" is stored in the $greeting variable, which is output in the title and body of the page with an echo command.

Variable Scope

A variable's scope determines the locations from which the variable can be accessed. PHP variables are either superglobal, global, or local.

Variable Scope Explanation
superglobal Superglobal variables are predefined arrays, including $_POST and $_GET. They are accessible from anywhere on the page.
global Global variables are visible throughout the script in which they are declared. However, they are not visible within functions in the script unless they are re-declared within the function as global variables.
function Variables in the function scope are called local variables. Local variables are local to the function in which they are declared.


Again, superglobal variables are predefined arrays, including $_POST and $_GET and are accessible from anywhere on the page. The complete list of superglobals is shown below.

  • $_GET - variables passed into a page on the query string.
  • $_POST - variables passed into a page through a form using the post method.
  • $_SERVER - server environment variables (e.g, $_SERVER['HTTP_REFERER'] returns the URL of the referring page).
  • $_COOKIE - cookie variables.
  • $_FILES - variables containing information about uploaded files.
  • $_ENV - PHP environment variables (e.g, $_ENV['HTTP_HOST'] returns the name of the host server.
    Which environment variables are available depends on the specific server setup and configuration.
  • $_REQUEST - variables passed into a page through forms, the query string and cookies.
  • $_SESSION - session variables.

The elements within superglobal variables can be accessed in three different ways, which the authors of PHP and MySQL Web Development refer to as short style, medium style, and long style.

PHP & MySQL Web Development, Third Edition.

Style Syntax (using $_GET) Notes
Short $varname
  • Convenient, but it makes it difficult to distinguish superglobal variables from other variables in the code.
  • Requires register_globals config setting to be on.
Medium $_GET['varname']
  • Recommended approach.
  • Happy medium between convenience and clarity.
  • Not available before v. 4.1.
Long $HTTP_GET_VARS['varname']
  • Inconvenient to type.
  • Deprecated, but still supported in current versions.
  • Can be disabled via the register_long_arrays directive in the php.ini file.

Many of these superglobals will be covered later in the course.


Constants are like variables except that, once assigned a value, they cannot be changed. Constants are created using the define() function and by convention (but not by rule) are in all uppercase letters. Constants can be accessed from anywhere on the page.



Variable-Testing and Manipulation Functions

For a complete list of variable functions see

PHP provides built-in functions for checking if a variable exists, checking if a variable holds a value, and removing a variable.

To output the results of these functions to a browser, use the var_dump() function (e.g. var_dump(isset($a));).

Function Explanation Example
isset() Checks to see if a variable exists. Returns true or false. isset($a)
unset() Removes a variable from memory. unset($a)
empty() Checks to see if a variable contains a non-empty, non-false value. empty($a)

This tutorial is based on Webucator's Introduction to PHP Training Course. We also offer many other PHP Training courses. Sign up today to get help from a live instructor.