Thursday, March 21, 2013

Understanding Operators in PHP - Part 5

Hi all, as we are trying to understand the operators concepts. This is the last article in the Operators series. In our PHP Developer Journey tutorials, let's continue with our Understanding Operators part 5. If you are unaware of the initial parts then please go through the below links.

Assignment operators, Arithmetic operators, Array operators - Part 1

String operators, Logical Operators - Part 2

Bitwise Operators - Part 3

Comparison Operators - Part 4

Error Control Operators:

PHP language is very handy when dealing with errors. Sometimes its necessary for the developer not to display the error messages to the end user which actually occurs when something goes wrong with the code. This can be accomplished in PHP with the help of an operator called "Error Suppression" Operator (@).

How to use Error Control Operator (@):

We can prevent the errors by not showing to the end user by putting in front of the operation. Like wise, for example, Divide-By-Zero is an infinity number, if you output then it will print some error. With the @ operator, however, we can prevent the error from being printed out, but not from occurring it. Let's see a simple example.

Demo Without using Error Control Operator in the expression:

$a = 1 / 0;

echo $a;

echo "hi";

From the above code, the following output will come if you execute at your localhost. See the below picture.
Demo With using Error Control Operator in the expression:

@$a = 1 / 0;

echo $a;

echo "hi";

Piece Of Advice for Error Control Operators:

This operator can be very dangerous because it prevents PHP from notifying you that something has gone wrong. You should, therefore, use it only whenever you want to prevent errors from propagating to a default handler because you have a specialized code segment that you want to take care of the problem. Generally speaking, it’s a bad idea to use this approach simply as a way to “silence” the PHP interpreter, as there are better ways to do so (for example, through error logging) without compromising its error reporting capabilities. 

Note that not all types of errors can be caught and suppressed using the @ operator. Because PHP first parses your script into an intermediate language that makes execution faster and then executes it, it won’t be capable of knowing that you have requested error suppression until the parsing phase is over and the execution phase begins. As a result, syntax errors that take place during the parsing phase will always result in an error being outputted, unless you have changed your php.ini settings in a way that prevents all errors from being outputted independently from your use of the @ operator.

Execution Operators:

PHP supports one execution operator: backticks (``). Note that these are not single-quotes! PHP will attempt to execute the contents of the backticks as a shell command; the output will be returned (i.e., it won't simply be dumped to output; it can be assigned to a variable). Use of the backtick operator is identical to shell_exec().

As i haven't worked with this operator in my PHP's life span. So i just grabbed this content from php.net manual.

$output = `ls -al`;

echo "
$output
";
Type casting: 

As we know PHP is a loosely typed language and assigns types to variables depending what is assigned to it. Even though PHP handles data types automatically most of the time, you can still force it to convert a particular datum to a specific type by using a typecasting operator. There are two ways to cast a variable in PHP as a specific type: using the settype() function, or using (int) (bool) (float) etc. 

Using the settype() function you can do this: These are 

settype($foo, "array");

settype($foo, "bool");

settype($foo, "boolean");

settype($foo, "float");

settype($foo, "int");

settype($foo, "integer");

settype($foo, "null");

settype($foo, "object");

settype($foo, "string");


Using (int) etc you can do this instead:

  • (int) to cast a value to its integer equivalent 
  • (float) to cast a value to its floating-point equivalent 
  • (string) to cast a value to its string equivalent 
  • (array) to force the conversion of the operand to an array if it is not one already 
  • (object) to force the conversion of the operand to an object if it is not one already

$foo = (array)$foo;

$foo = (boolean)$foo;

$foo = (double)$foo;

$foo = (float)$foo;

$foo = (int)$foo;

$foo = (integer)$foo;

$foo = (object)$foo;

$foo = (real)$foo;

$foo = (string)$foo;

Note: php.net manual says "PHP does not require (or support) explicit type definition in variable declaration; a variable's type is determined by the context in which the variable is used."

Operator Precedence: 

Operator precedence refers to the order in which operators execute within an expression. For example, one of the basic rules of arithmetic is that multiplications and divisions are executed before additions and subtractions. With a large number of types of operations available, things get a bit more complicated, but are by no means complex.

Operator Precedence and associvity
For Example,

$foo = 5 * 10 - 1; // output: 49

Should $foo be 49 or 45? If you cannot see why there are two possibilities, it might help to break them up using brackets, like this:

$foo = (5 * 10) - 1; output: 49
$foo = 5 * (10 - 1); output: 45

Case 1:

$foo = (5 * 10) - 1

In case 1, five is multiplied by ten then one is subtracted from the result.

Case 2:

$foo = 5 * (10 - 1);

In case 2 ten has one subtracted from it, making nine, then that result is multiplied by five. If there is any ambiguity in your expressions, PHP will resolve them according to its internal set of rules about operator precedence, that is, which operators are calculated first.

Ok that's all about operators. Keep coming for more stuff..

Did You Enjoy this Article ?

If yes, Then enter your email below to get

more such great articles in your inbox

For FREE !

No comments:

Post a Comment

Thank you for reading and commenting...