PHP variables begin with a dollar sign (
$) as shown below.
$varName = "Value";
|String||string of characters|
|Boolean||true or false|
|Array||list of items|
|Object||instance of a class|
Variable, function and class names are all identifiers and all follow the rules below, with the exception that function names are not case sensitive.
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.
Here is the "Hello World!" script again, but this time we use a variable.
<?php $greeting = 'Hello World!'; ?> <!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title><?php echo $greeting; ?></title> </head> <body> <?php echo $greeting; ?> </body> </html>
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
A variable's scope determines the locations from which the variable can be accessed. PHP variables are either superglobal, global, or local.
|superglobal||Superglobal variables are predefined arrays, including
|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
$_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.
$_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.
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.
For a complete list of variable functions see http://www.php.net/manual/en/ref.var.php.
PHP provides built-in functions for checking if a variable exists, checking if a variable holds a value, and removing a variable.
||Checks to see if a variable exists. Returns true or false.||
||Removes a variable from memory.||
||Checks to see if a variable contains a non-empty, non-false value.||