|
|
PHP VariablesVariables are containers to store data. We can add or change or delete data from the variable and use them in our script. Here are some basic rules to create variables.
Declaring variables in PHP
Variable should start with $
$var1='testing'; // correct
var1='testing'; // Wrong - error
_var1='testing'; // wrong -error
5var='testing'; // wrong -error
Variable should contain numbers, alphabet and underscore
$var1='testing'; // correct
$va!3='testing'; // wrong -error
$var^4='testing';// wrong -error
After $ we can't use number it must be alphabet or underscore
$_var1='testing'; // correct
$1var='testing'; // wrong error
$var1='testing'; // correct
Variables are case sensitive
No need to declare variable type before using in PHP
Scope of Variable
The variables declared as global are available throughout the script and variables declared inside functions is know as local variable and are available inside the function only. Read more on scope of the variables in our PHP functions.
All variables are used within the page or within the script present in a page. As you are reading this page some variables are used to display the content of the page. These variables are lost as soon as this page execution is completed at server and downloaded to your browser. So how to pass the data or retain the data across the pages ? We will read that in our next section passing the variables from one page to other.
| |
| |
|
|
|
|
|