Skip to Main Content
PHP in a Nutshell
book

PHP in a Nutshell

by Paul Hudson
October 2005
Intermediate to advanced content levelIntermediate to advanced
372 pages
11h 35m
English
O'Reilly Media, Inc.
Content preview from PHP in a Nutshell

Some Operator Examples

Here are some examples of most of these operators in action:

    $somevar = 5 + 5; // 10
    $somevar = 5 - 5; // 0
    $somevar = 5 + 5 - (5 + 5); // 0
    $somevar = 5 * 5; // 25
    $somevar = 10 * 5 - 5; // 45
    $somevar = $somevar . "appended to end";
    $somevar = false;
    $somevar = !$somevar; // $somevar is now set to true
    $somevar = 5;
    $somevar++; // $somevar is now 6
    $somevar--; // $somevar is now 5 again
    ++$somevar; // $somevar is 6

The third line uses parentheses to control the order of operations. This is important, as the equation 5 + 5 - 5 + 5 can be taken in more than one way, such as 5 + (5 - 5) + 5, which is 10. There are some equations, such as the one on line five, where parentheses are not needed. There, 10 * 5 - 5 can only be taken to mean (10 * 5) - 5 because of the mathematical rules of precedence (rules of operations)—multiplication is considered higher in order (executed first) than subtraction.

Despite each operator having specific precedence, it is still best to use parentheses in order to make your meaning clear. Expressions inside parentheses are always evaluated first, and you can use any number of parentheses in order to get the expression correct.

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

PHP Cookbook

PHP Cookbook

Eric A. Mann
Programming PHP

Programming PHP

Rasmus Lerdorf, Kevin Tatroe
Learning PHP

Learning PHP

David Sklar

Publisher Resources

ISBN: 0596100671Errata Page