What are Expressions? PHP

Expressions are combinations of identifiers and operators. In most cases, they are the familiar formulas you learned about in high school algebra. They are executed from left to right; some operators are processed before others, and you can use parentheses to force an operation to occur before the rest of the expression. But since the expression may be a mix of different data types, you must be aware of how types are converted.

Using Assignment Operators

<?
// this assignment
$Count = $Count + 1;
// is the same as this assignment
$Count += 1;
?>

Two general rules are at work when an expression is evaluated. First, some operators work only on certain data types. Second, if the operation is on a mix of an integer and a double, the integer will be converted to a double.

Most operators work on numbers. If you attempt to add a string, the string will be converted to a number. The contents of the string will determine whether it becomes an integer or a double. PHP will make a good attempt at converting your string to a number. It will strip leading whitespace and it will strip off all characters after a string of digits. It will even read doubles with an exponent. But if PHP can't decide on a reasonable numerical value, it will treat your string as zero.

String/Number Conversion

<?
//1 + 1 == 2
print((1 + "1") . "<BR> ");
//1 + 2 == 3
print((1 + " 2") . "<BR> ");
//1 + 3 == 4
print((1 + "3extra stuff") . "<BR> ");
//1 + 4500000 == 4500001
print((1 + "4.5e6") . "<BR> ");
//1 + 0 == 1
print((1 + "a7") . "<BR> ");
?>

is a good test of how PHP will convert strings to numbers. All the commands will produce a number from the string, except the last. Since the string in the last line begins with a letter, PHP gives up and treats it as zero. Notice that after the addition the script uses a concatenation operator. This causes the integer created inside the parentheses to be converted to a string for the purposes of printing. The concatenation operator forces both sides to be treated as strings.

demonstrates the use of parentheses to force the order in which the expression is evaluated. The first line evaluates to 17, the second to 35. In addition to evaluation from left to right, operators execute in a specific precedence. For example, multiplication is resolved before addition.

A programming language must order all its operators, but in practice it is difficult for the programmer to keep it all straight. The best policy is to use parentheses to explicitly force the precedence you want on complex expressions. lists the operators in order of precedence. Operators on the same line are of equal precedence, therefore falling back to left-to-right precedence.

Using Parentheses

<?
print ((3 + 2 * 7) . "<BR> ");
print (((3 + 2) * 7) . "<BR> ");
?>

Precedence-of-OperatorsPrecedence-of-Operators


Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

PHP Topics