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<?
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.
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.
PHP Related Interview Questions
|PHP Interview Questions||MySQL Interview Questions|
|PHP+MySQL Interview Questions||Drupal Interview Questions|
|MYSQL DBA Interview Questions||PHP5 Interview Questions|
|WordPress Interview Questions||Joomla Interview Questions|
|CakePHP Interview Questions||CodeIgniter Interview Questions|
|PHP7 Interview Questions|
An Introduction To Php
Variables, Operators, And Expressions
Classes And Objects
I/o And Disk Access
Time, Date, And Configuration Functions
Parsing And String Evaluation
Sorting Searching And Random Numbers
Integration With Html
Efficiency And Debugging
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.