Page 97 - Open Soource Technologies 304.indd
P. 97
Open Source Technologies
Notes You’ll get the hang of it pretty quickly.
Non-numeric strings cannot be decremented.
6.1.3.9 The Cast Operators
PHP provides a C-like way to force a type conversion of a value by using the cast operators.
The operand appears on the right side of the cast operator, and its result is the converted type
according to the following table.
Operator Changes Type To
(int), (integer) Integer
(float), (real), (double) Floating point String
(string) String
(bool), (boolean) Boolean
(array) Array
(object) Object
The casting operators change the type of a value and not the type of a variable. For example:
$str = “5”;
$num = (int) $str;
This results in $num being assigned the integer value of $str (5), but $str remains of type string.
6.1.3.10 The Silence Operator
The operator @ silences error messages during the evaluation process of an expression. It is
discussed in more detail in unit 7.
6.1.3.11 The One and Only Ternary Operator
One of the most elegant operators is the ?: (question mark) operator. Its format
Is truth_expr ? expr1 : expr2
The operator evaluates truth_expr and checks whether it is true. If it is, the value of the expression
evaluates to the value of expr1 (expr2 is not evaluated). If it is false, the value of the expression
evaluates to the value of expr2
(expr1 is not evaluated).
For example, the following code snippet checks whether $a is set (using isset()) and displays a
message accordingly:
$a = 99;
$message = isset($a) ? ‘$a is set’ : ‘$a is not set’;
print $message;
This example prints the following:
$a is set
92 LOVELY PROFESSIONAL UNIVERSITY