The main way to store information in the middle of a PHP program is by using a PHP Variables.Here are the most important things to know about variables in PHP.
- All variables in PHP are denoted with a leading dollar sign ($).
- The value of a variable is the value of its most recent assignment.
- Variables are assigned with the = operator, with the variable on the left-hand side and the expression to be evaluated on the right.
- Variables can, but do not need, to be declared before assignment.
- Php variable do not have intrinsic types – a variable does not know in advance whether it will be used to store a number or a string of characters.
- Variables used before they are assigned have default values.
- PHP does a good job of automatically converting types from one to another when necessary.
- Its variables are Perl-like.
PHP has a total of eight data types which we use to construct our variables −
The first five are simple types, and the next two (arrays and objects) are compound – the compound types can package up other arbitrary values of arbitrary type, whereas the simple types cannot.
They like 3.14159 or 49.1. By default, doubles print with the minimum number of decimal places needed. For example, the code −
<?php $many = 2.2888800; $many_2 = 2.2111200; $few = $many + $many_2; print("$many + $many_2 = $few <br>"); ?>
They have only two possible values either true or false. PHP provides a couple of constants especially for use as Booleans: TRUE and FALSE, which can be used like so −
if (TRUE) print("This will always print<br>"); else print("This will never print<br>");
NULL is a special type that only has one value: NULL. To give a variable the NULL value, simply assign it like this −
$my_var = NULL;
The special constant NULL is capitalized by convention, but actually it is case insensitive; you could just as well have typed −
$my_var = null;
A variable that has been assigned NULL has the following properties −
i. It evaluates to FALSE in a Boolean context.
ii. It returns FALSE when tested with IsSet() function.
They are sequences of characters, like “PHP supports string operations”. Following are valid examples of string:
$string_1 = "This is a string in double quotes"; $string_2 = 'This is a somewhat longer, singly quoted string'; $string_39 = "This string has thirty-nine characters"; $string_0 = ""; // a string with zero characters
Singly quoted strings are treated almost literally, whereas doubly quoted strings replace variables with their values as well as specially interpreting certain character sequences.
<?php $variable = "name"; $literally = 'My $variable will not print!'; print($literally); print "<br>"; $literally = "My $variable will print!"; print($literally); ?>
There are no artificial limits on string length – within the bounds of available memory, you ought to be able to make arbitrarily long strings.
Strings that are delimited by double quotes (as in “this”) are preprocessed in both the following two ways by PHP −
i. Certain character sequences beginning with backslash (\) are replaced with special characters
ii. Variable names (starting with $) are replaced with string representations of their values.
The escape-sequence replacements are −
- \n is replaced by the newline character
- \r is replaced by the carriage-return character
- \t is replaced by the tab character
- \$ is replaced by the dollar sign itself ($)
- \” is replaced by a single double-quote (“)
- \\ is replaced by a single backslash (\)
You can assign multiple lines to a single string variable using here document.
<?php $channel =<<<_XML_ <channel> <title>What's For Dinner</title> <link>http://menu.example.com/ </link> <description>Choose what to eat tonight.</description> </channel> _XML_; echo <<<END This uses the "here document" syntax to output multiple lines with variable interpolation. Note that the here document terminator must appear on a line with just a semicolon. no extra whitespace! <br /> END; print $channel; ?>