Variables
In this PHP Tutorial you will learn about Variables, different types of Variables like Identifiers, Variables, Statements and Constants.
Variables
In this PHP Tutorial you will learn about Variables, different types of Variables like Identifiers, Variables, Statements and Constants.
Identifiers:
Identifier is the name of the data, identifiers also can be used as a label for a set of commands.
There are rules for identifiers naming, rules are:
• The first character has to be a letter or an underscore (_);
• The following characters can be any combination of, letters, digits, and undersore.
Example:
The following are valid identifiers:
identifier
_identifier
__identifier
_another_identifier
another_identifier
Variables:
A variable is a data container, it can contain data of any of the types stated before.
A variable can contain only one value at a time.
A variable is an identifier preceded by a dollar sign ($).
Variable names are case sensitive, for example, $var is different from $Var.
Variables can be embedded directly in strings, for example, “Value \$var is embedded here”
If there is no space between the variable name and the rest of the string, variable can be embedded like in the following example:
<?php
$var = 100;
echo “There are {$var}000 books in the college library”;
?>
Statements:
A statement is a command that is executed, for example the echo function causes an expression to be sent to the standard output.
Statements has to be ended with a semicolon.
Constants:
A constant is a data container like variable, but the data value of the constant doesn’t change.
To create a constant use the construct define().
Example:
<?php
define(“MY_CONSTANT”, 100);
echo MY_CONSTANT;
?>
define() takes two arguments, the first is a string, which is the constant name, and the another is the constant value.
There is a little trick about constants, they can be defined with any name, but they can’t be used unless they follow the identifier naming rules.