<< PHP Syntax | PHP Tutorials | PHP Comments >>

PHP Variables

Well, if you've haven't taken Algebra, or done any other programming before variables are basically what their name suggests. A variable can contain data, that can vary in values. You can store things like integers, strings, booleans (true or false) and so on and so forth. Variables are always set using this simple code:

$variable = value;
$another_variable = another_value;

Choosing a Variable Name

Choosing a variable name is not as hard as you think (well you probably don't think it's hard, I just thought it would be cool to say that). There are some rules though, that must be followed or the code just won't work. First of all, let's look at a little piece of text I took from the PHP.net website:

"A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores."

Yup. That about sums it up for ya. But then again, there is a little more that isn't really a rule, it's more or less something that just should be done. First of all, variables should be all lowercase. They don't have to be, but it just looks nicer. Second, if there are more than one words in a variable name, use an underscore ( $two_words ) to separate them. If this isn't possible, capitalize the second word ( $twoWords ). Also note that PHP variables are CaSe SeNsItIvE. That means that things like $variable and $varIABLE are seen as two different variables.

Different Data Types

Unlike most other programming languages, PHP does not require that you initialize your variables. This basically means you don't have to define a big list of all your variables before you get started. As well the PHP engine automatically chooses the best data type it should be (int, bool, double, etc.). Here is an example of some variables being set:

$integer = 1;
$string = 'Hello';
$boolean = false;

These are all valid variables. The PHP engine will decide the first one to be an integer (due to lack of quotes, and it's an integer), the second a string (because of the quotes) and the last boolean (because of no quotes, and it's false or true).

Predefined Variables

Ahhh the horror of having to remember which variables are already defined by the PHP engine! Just wait until you start writing functions and have to remember the function names :S Anyway, enough with functions we are talking about variables... yes! Predefined variables exist for the better-ment of the PHP community. Some predefined variables are not in use anymore, but may still be in existance on some servers. They are the ones that are in the HTTP_*_VARS format. Here is a list for you:

  • $GLOBALS - contains every variable in the global scope
  • $_SERVER, $HTTP_SERVER_VARS - containts server information
  • $_GET, $HTTP_GET_VARS - variables provided to the script using a URL query string
  • $_POST, $HTTP_POST_VARS - variables provided by the HTTP POST method
  • $_COOKIE, $HTTP_COOKIE_VARS - contains all cookie information via HTTP cookies
  • $_FILES, $HTTP_POST_FILES - contains all data from file uploads
  • $_ENV, $HTTP_ENV_VARS - contains all the variables available from the environment
  • *$_REQUEST, $HTTP_REQUEST_VARS - contains $_POST, $_GET and $_COOKIE all in one
  • $_SESSION, $HTTP_SESSION_VARS - contains session information
  • $php_errormsg - contains last PHP error message
  • $HTTP_RAW_POST_DATA - contains raw POST data
  • $http_response_header - contains header information

* $_REQUEST cannot be trusted, and I suggest you never use it - ever.

That is a list of the predefined variables. I'm not going to explain them anymore right now as they will be explained later when I ... well explain superglobals and all that good stuff. Before moving on, there will be parts about variables throughout this tutorial, as variables are found in virtually every PHP script written.

<< PHP Syntax | PHP Tutorials | PHP Comments >>