Maison >développement back-end >tutoriel php >Portée variable et variable | Principes fondamentaux de PHP
The rules when you create variables in PHP:
Valid variables:
$name = "Gunawan"; //valid $Name = "Gunawan"; //valid $_name = "Gunawan; //valid
Not valid variables:
$4name = "Gunawan"; //not valid $user-name = "Gunawan"; //not valid $this = "Gunawan"; //not valid
PHP has 3 variable scopes:
$name = "Gunawan"; function get_name() { echo $name; // not valid } get_name();
To access a global variable within a function you must declare a global variable with the keyword 'global' within a function.
$name = "Gunawan"; function get_name() { global $name; echo $name; // valid } get_name();
The second way to access global variables is to use a global array.
$name = "Gunawan"; function get_name() { echo $GLOBALS['name']; // valid } get_name();
function test() { static $number = 0; echo $number; $number++; }
Download my repository php fundamental from my github.
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!