What is PHP Variables?
A variable is a place where you can store things, such as number, a date or some text. You can put the text or number into variables so you can retrieve them later or so you can manipulate them. Variables are called variables because the value that they can vary.
Variable Naming Conventions and Best practices
Variables in PHP are started with dollar($) sign followed by the name of the variable. The Variable name is case-sensitive, meaning that PHP would treat $name and $Name as two different variables.
PHP Variables Naming Rules
- Name must begin with a $ dollar sign, for example, $name
- Name can comprise letters, numbers, and underscore characters, but not spaces – for example, $subtotal_1
- The first character after the $ dollar sign must be a letter or an underscore character – it cannot be a number.
Note: that variable names in PHP are case-sensitive so $name, $Name, and $NAME are three separate individual variables.
PHP variables are loosely typed meaning they can contain data of any type, unlike “strongly typed” variables in some languages where the data type must be specified when the variables are created. So, PHP variables may happily contain an integer, or a floating-point number, or a string of text characters, or a Boolean value of True or FALSE, or an object, or a NULL empty value.
Do not confuse the purpose of double and single quotes. Remember that PHP only makes variables substitutions for mixed strings enclosed with double quotes.
A variable is created in PHP script simply by stating its name. The variable can then be assigned an initial value (initialized) by using the = assignment operator to state its value. This statement, and all others in PHP, must end with a semi-colon like this:
1 | $first_name="John"; |
The value contained within the variable can then be displayed by referencing it using the variable like this :
1 | echo $first_name; |
0 comments:
Post a Comment