Execution Operator and Operator Precedence in PHP
PHP Operators with Security Considerations - Part 7
Foreword: In this part of the series, I talk about execution operator and operator precedence in PHP.
By: Chrysanthus Date Published: 17 Oct 2018
Introduction
The Execution Operator
The execution operator is the pair of backticks, that is, ``. PHP uses this to execute operating system commands. The command is typed within the backticks. The return result of the command can be held in a PHP variable. Try the following code for the operating system command, dir, that returns the listing of the current directory.
<?php
$ret = `dir`;
echo $ret;
?>
The result may not have the format you like.
Operator Precedence
The expression,
2 + 5 * 3
is either evaluated as
(2 + 5) * 3
or
2 + (5 * 3)
That is, you either do the addition first or the multiplication (*) first. If you do the addition first you have 21; if you do the multiplication first, you have 17. PHP actually does the multiplication first and the answer is always 21. This is called, precedence: what operation is done first, what is done second, what is done third, and so on. When you are not sure of the sequence of operations in an expression, you can use brackets to force a sequence.
There is more to precedence: Consider the following:
2 + 5
You must have given the answer as 7. However, the computer does not work like that. The computer either evaluates the addition (operation) beginning from 5 to 2 or beginning from 2 to 5. With some operators, the answers are different from what you expect. Right-to-left evaluation is called right associativity. Left-to-right evaluation is called left associativity.
The following table lists the operators in order of precedence, with the highest-precedence at the top (descending downward). Operators on the same line have equal precedence, in which case associativity decides the order of evaluation.
Operator Precedence
Associativity Operators
non-associative clone, new
left [
non-associative ++, --
right ~, -, (int), (float), (string), (array), (object), (bool), @
non-associative instanceof
right !
left *, /, %
left +, -, .
left <<, >>
non-associative < ,<=, >, >=, <>
non-associative ==, !=, ===, !==
left &
left ^
left |
left &&
left ||
left ? :
right =, +=, -=, *=, /=, .=, %=, &=, |=, ^=, <<=, >>=, =>
left and
left xor
left or
left ,
That is it for this part of the series.
Chrys
Related Links
Basics of PHP with Security ConsiderationsWhite Space in PHP
PHP Data Types with Security Considerations
PHP Variables with Security Considerations
PHP Operators with Security Considerations
PHP Control Structures with Security Considerations
PHP String with Security Considerations
PHP Arrays with Security Considerations
PHP Functions with Security Considerations
PHP Return Statement
Exception Handling in PHP
Variable Scope in PHP
Constant in PHP
PHP Classes and Objects
Reference in PHP
PHP Regular Expressions with Security Considerations
Date and Time in PHP with Security Considerations
Files and Directories with Security Considerations in PHP
Writing a PHP Command Line Tool
PHP Core Number Basics and Testing
Validating Input in PHP
PHP Eval Function and Security Risks
PHP Multi-Dimensional Array with Security Consideration
Mathematics Functions for Everybody in PHP
PHP Cheat Sheet and Prevention Explained
More Related Links