PHP PHP Variables

PHP Variables

php variables variables in php

Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive, that means $myVar, $myvar are two different variables.

Rules for PHP variables:

  • PHP variables starts with the $ sign, followed by the name of the variable
  • PHP variable name must start with a letter or the underscore character
  • Any PHP variable name can not start with a number
  • Name of  any PHP variable can have only alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • PHP variables’ names are case-sensitive ($age and $AGE are two different variables)

Example:

PHP Predefined variables

There are few variables which are already defined in PHP and can be accessed anywhere (in any scope) in PHP and cannot be declared by user. These are-

Superglobals — Superglobals are built-in variables that are always available in all scopes

$GLOBALS — References all variables available in global scope

$_SERVER — Server and execution environment information

$_GET — HTTP GET variables

$_POST — HTTP POST variables

$_FILES — HTTP File Upload variables

$_REQUEST — HTTP Request variables

$_SESSION — Session variables

$_ENV — Environment variables

$_COOKIE — HTTP Cookies

$php_errormsg — The previous error message

$HTTP_RAW_POST_DATA — Raw POST data

$http_response_header — HTTP response headers

$argc — The number of arguments passed to script

$argv — Array of arguments passed to script

*Use of these variables will be shown in upcoming tutorials.

PHP Variables from External Sources

Talking about PHP variables from external sources include data (variables) fro –

  • HTML forms (by GET or POST):

When a form is submitted it carries data from external sources which is stored in variables in PHP.

Example #1: HTML form with user name and Password fields

Example #2: External data from this form stored in php Variables

 HTTP Cookies

PHP supports HTTP cookies for storing data in the remote browser and so tracking or identifying return users can be done very easily. You can set cookies using the setcookie() function and retrieve data using $_COOKIE[“cookie_name”] variable.

Example #1 set and get cookie data

 

Leave a Reply