But the order of operations is not fully determined by this: in 2 * 2 + 4 * 5 both multiplications must be performed before the addition, but the grouping does not say anything about the order in which the two multiplications are performed. For example, in 2 + 4 * 5, the grouping implied by precedence means that the multiplication of 4 and 5 must be performed before the addition of 2 and 20, simply because the result of that multiplication is required as one of the operands of the addition. So the expression yields 6 - 2 = 4, rather than 9 - 1 = 8.įor simple operators that evaluate all their operands and then combine the values in some way, precedence and associativity (and parentheses) imply some ordering requirements on those combining operations. It is as if the expression were written (9 - 3) - 2, not 9 - (3 - 2).
For example, in 9 - 3 - 2, subtraction is left associative, so 9 - 3 is grouped together as the left-hand operand of the second subtraction, rather than 3 - 2 being grouped together as the right-hand operand of the first subtraction. Operator associativity defines what happens if a sequence of the same operators is used one after another: usually that they will be grouped at the left or the right. So the expression yields 2 + 20 = 22, rather than 6 * 5 = 30. It is as if the expression were written 2 + (4 * 5), not (2 + 4) * 5. For example, in 2 + 4 * 5, the multiplication has higher precedence, so 4 * 5 is grouped together as the right-hand operand of the addition, rather than 2 + 4 being grouped together as the left-hand operand of the multiplication. Operator precedence means some operators group more tightly than others. Operator precedence and associativity work in Perl more or less like they do in mathematics. There are a few exceptions though: x can be either string repetition or list repetition, depending on the type of the left operand, and &, |, ^ and ~ can be either string or numeric bit operations. For example $x = $y compares two numbers for equality, and $x eq $y compares two strings.
It also means that Perl has two versions of some operators, one for numeric and one for string comparison. This is in contrast to many other dynamic languages, where the operation is determined by the type of the first argument. For example $x + $y is always a numeric addition, and if $x or $y do not contain numbers, an attempt is made to convert them to numbers first. In Perl, the operator determines what operation is performed, independent of the type of the operands. Perlop - Perl operators and precedence #DESCRIPTION Gory details of parsing quoted constructs.Keep in mind that is_int() operates in signed fashion, not unsigned, and is limited to the word size of the environment php is running in.
Getting Started Introduction A simple tutorial Language Reference Basic syntax Types Variables Constants Expressions Operators Control Structures Functions Classes and Objects Namespaces Enumerations Errors Exceptions Fibers Generators Attributes References Explained Predefined Variables Predefined Exceptions Predefined Interfaces and Classes Context options and parameters Supported Protocols and Wrappers Security Introduction General considerations Installed as CGI binary Installed as an Apache module Session Security Filesystem Security Database Security Error Reporting User Submitted Data Hiding PHP Keeping Current Features HTTP authentication with PHP Cookies Sessions Dealing with XForms Handling file uploads Using remote files Connection handling Persistent Database Connections Command line usage Garbage Collection DTrace Dynamic Tracing Function Reference Affecting PHP's Behaviour Audio Formats Manipulation Authentication Services Command Line Specific Extensions Compression and Archive Extensions Cryptography Extensions Database Extensions Date and Time Related Extensions File System Related Extensions Human Language and Character Encoding Support Image Processing and Generation Mail Related Extensions Mathematical Extensions Non-Text MIME Output Process Control Extensions Other Basic Extensions Other Services Search Engine Extensions Server Specific Extensions Session Extensions Text Processing Variable and Type Related Extensions Web Services Windows Only Extensions XML Manipulation GUI Extensions Keyboard Shortcuts ? This help j Next menu item k Previous menu item g p Previous man page g n Next man page G Scroll to bottom g g Scroll to top g h Goto homepage g s Goto search