Variable

Definition


$variable;
$variable = 10;

Output


Scalar Types

$variable = 10;

var_dump($variable); //=> int(10)
print($variable);    //=> 10
echo($variable);     //=> 10
echo $variable;      //=> 10

Compound Types

$variable = [1, 2, 3];

var_dump($variable);
print_r($variable);
//=>
// Array
// (
//     [0] => 1
//     [1] => 2
//     [2] => 3
// )

print($variable); //=> Notice: Array to string conversion in php
echo($variable);  //=> Notice: Array to string conversion in php

Pattern


[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*

$variable;
$Variable;
$_variable;
$1variable;  //=> Parse error: syntax error, unexpected '1' (T_LNUMBER), expecting variable (T_VARIABLE)
$%variable;  //=> Parse error: syntax error, unexpected '%', expecting variable (T_VARIABLE)
$function;
$class;

Dinamic Type


$variable = 10;
echo $variable; //=> 10

$variable = 'fulano';
echo $variable; //=> fulano

Case Sensitive


$variable = 10;
$Variable = 'fulano';

echo $variable; //=> 10
echo $Variable; //=> fulano

Constant


define('VARIABLE', 10);
echo VARIABLE; //=> 10
const VARIABLE = 10;
echo VARIABLE; //=> 10

Variable variables


$param = 'name';
$$param = 'fulano';

echo $param;  //=> name
echo $$param; //=> fulano
echo $name;   //=> fulano

Variable from array


$params = ['name' => 'fulano', 'email' => 'fulano@email'];
print_r($params);
//=>
// Array
// (
//     [name] => fulano
//     [email] => fulano@email
// )

echo $params['name'];  //=> fulano
echo $params['email']; //=> fulano@email

Valid extract

$params = ['name' => 'fulano', 'email' => 'fulano@email'];
extract($params);
print_r(get_defined_vars());
//=>
// Array
// (
//   ...
//   [name] => fulano
//   [email] => fulano@email
//   ...
// )
echo $name;  //=> fulano
echo $email; //=> fulano@email

Invalid extract

$params = ['10' => 'fulano', '20' => 'fulano@email'];
extract($params);
print_r(get_defined_vars());
//=>
// Array
// (
//   ...
// )
echo $10; //=> PHP Parse error:  syntax error, unexpected '10' (T_LNUMBER), expecting variable (T_VARIABLE) or '{' or '$' in php

Predefined Variables


Variable Description
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


print_r($GLOBALS);
//=>
// Array
// (
//     [_GET] => Array
//         (
//         )
//     [_POST] => Array
//         (
//         )

// ...
// )

$x = 10;
print_r($GLOBALS);
//=>
// Array
// (
// [_GET] => Array
// ...
// [GLOBALS] => Array
// _RECURSION_
// [x] => 10
// )

Magic constants


__LINE__ __DIR__ __CLASS__ __METHOD__ ClassName::class
__FILE__ __FUNCTION__ __TRAIT__ __NAMESPACE__  


print_r(__DIR__); //=> /current_folder

Predefined Constants


PHP_VERSION PHP_OS_FAMILY DEFAULT_INCLUDE_PATH PHP_SYSCONFDIR E_CORE_WARNING
PHP_MAJOR_VERSION PHP_SAPI PEAR_INSTALL_DIR PHP_LOCALSTATEDIR E_COMPILE_ERROR
PHP_MINOR_VERSION PHP_EOL PEAR_EXTENSION_DIR PHP_CONFIG_FILE_PATH E_COMPILE_WARNING
PHP_RELEASE_VERSION PHP_INT_MAX PHP_EXTENSION_DIR PHP_CONFIG_FILE_SCAN_DIR E_USER_ERROR
PHP_VERSION_ID PHP_INT_MIN PHP_PREFIX PHP_SHLIB_SUFFIX E_USER_WARNING
PHP_EXTRA_VERSION PHP_INT_SIZE PHP_BINDIR E_ERROR E_USER_NOTICE
PHP_ZTS PHP_FLOAT_DIG PHP_BINARY E_WARNING E_RECOVERABLE_ERROR
PHP_DEBUG PHP_FLOAT_EPSILON PHP_MANDIR E_PARSE E_DEPRECATED
PHP_MAXPATHLEN PHP_FLOAT_MIN PHP_LIBDIR E_NOTICE E_USER_DEPRECATED
PHP_OS PHP_FLOAT_MAX PHP_DATADIR E_CORE_ERROR E_ALL