



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Data types
The type of a variable is decided at runtime by PHP depending on the context in which that variable is used. PHP supports the following data types:
Integers
The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value (that's 32 bits signed). 2147483647 = 2^31 – 1. PHP does not support unsigned integers. It is important to note that, unlike C++ or Java, if you specify a number beyond the bounds of the integer type, it will be interpreted as a float instead. If an operation results in a number beyond the bounds of the integer type, a float will be returned instead. Also, there is no integer division operator in PHP. 1/2 yields the float
0..
Strings
PHP strings are created using single quote or double quote. They can also be created by <<< which is called heredoc. One should provide an identifier after <<< , then the string, and then the same identifier to close the quotation. The closing identifier must begin in the first column of the line.
Variable
Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'.
Note: For our purposes here, a letter is a-z, A-Z, and any ASCII characters from 127 through 255 (0x7f-0xff). Therefore, $Inzi½ is a legal variable name. It is not necessary to initialize variables in PHP. Un-initialized variables have a default value of their type - FALSE, zero, empty string or an empty array.
String conversion to numbers
When a string is evaluated as a numeric value, the resulting value and type are determined as follows.
The string will evaluate as a float if it contains any of the characters '.', 'e', or 'E'. Otherwise, it will evaluate as an integer. The value is given by the initial portion of the string. If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero).
Here is an example:
Using strings in numeric expressions provides flexibility but it should be obvious that it also is a source to numerous programming errors and mistakes. This kind of error is also very hard to debug and detect.
Arrays
An array in PHP is an ordered map. An array can be created by the array() construct. It takes a certain number of comma-separated key => value pairs. key may be an integer or string. The following example illustrates this concept:
"bar", 12 => true); echo $arr["foo"]; // bar echo $arr[12]; // 1 ?>If no key is specified in the assignment, then the maximum of the existing integer indices is taken, and the new key will be that maximum value + 1. If the current maximum is negative then the next key created will be zero. If no integer indices exist yet, the key will be 0 (zero). Note that the maximum integer key used for this need not currently exist in the array. It simply must have existed in the array at some time since the last time the array was re-indexed. If the specified key already has a value assigned to it, that value will be overwritten. These things are demonstrated in the following example:
1, 12 => 2); $arr[] = 56; // This is the same as $arr[13] = 56; // at this point of the script $arr["x"] = 42; // This adds a new element to // the array with key "x“ unset($arr[13]); // This removes the element from the arrayConstants
A constant is an identifier (name) for a simple value that cannot change during the execution of the script. A constant is case-sensitive by default. By convention, constant identifiers are always uppercase. The name of a constant follows the same rules as any label in PHP. A valid constant name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thusly: [a-zA-Z\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*_.
Logical operators
PHP supports the following logical operators.
and, or, xor, !, &&, ||
The reason for the two different variations of "and" and "or" operators is that they operate at different precedence. This is demonstrated with the help of the following example:
$a and $b or $c // ($a and $b) or $c $a && $b || $c $a and $b || $c // $a and ($b || $c)
String operators
There are two string operators. The first is the concatenation operator ('.'), which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator ('.='), which appends the argument on the right side to the argument on the left side.
Array Operators
PHP provides a number of operators to manipulate arrays. These are:
Note the difference between == and ===. The former checks if the same key-value pairs are present in the two arrays and the order does not matter whereas the latter requires that in order to be identical the pairs have to be in the same order.
The + operator appends the right handed array to the left handed, whereas duplicated keys are NOT overwritten. This concept is elaborated as below:
$a = array("a" => "apple", "b" => "banana"); $b = array("a" => "pear", "b" => “date", "c" => “mango"); $c = $a + $b; // Union of $a and $b
This will result in an array c with the following values:
["a"]=> "apple" ["b"]=> "banana" ["c"]=> “mango"
Control Statements
PHP supports the following control statements:
Most control structures work in a manner similar to C. The differences are highlighted in the following paragraphs.
If statements