Exforsys.com
 
Home Tutorials PHP
 

PHP Tutorials - Operators (Part I)

 
Category: PHP
Comments (0)

PHP Tutorials - Operators (Part I)

In this PHP Tutorial you will learn 1st Part of Operators - The assignment operator, The arithmetic operators, Bitwise operators - AND (&), OR (|), XOR (^), NOT (~) and Error control operator



The assignment operator:

It’s used to assign a value to a variable, for example, to assign the value 100 to the variable $var:
$var = 100;


The variable comes at the left of the operator, and the value at the right, the value can be another variable, and in this case the variable at the left will be assigned to the value of the variable at the right, for example:


$var1 = $var2


You can change the value of $var1, or $var2 at anytime without affecting the other variable.


Example


<?php
$var1 = 10;
$var2 = 15;
$var1 = $var2; // $var1 = 15
$var2 = 20;   // $var1 = 15, $var2 =20
$var1 = &$var2;    // $var1 = $var2 = 20
$var2 = 25    // $var1 = $var2 = 25
?> 


The arithmetic operators:

There are five binary arithmetic operators in PHP:


  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
  • Modulus (%)

The preceding arithmetic operators have two operands, any of the operands can be a value or a variable, all these arithmetic operators must not be a problem to you, only the modulus may be new to you, modulus is the remainder of the division, for example:


$a = 7 % 3;


This makes the value of $a = 1
 
There are also two unary arithmetic operations:


  • Increment by one (++)
  • Decrement by 1 (--)

Each of the unary operations can be used in two different ways, the first way is as a prefix, for example, ++$x and --$x, and the second way is as a postfix, for example, $x++ and $x—


The difference between the prefix and the postfix is that for prefix the value is incremented or decremented first, then its value is returned, but for postfix, the value is returned first, then the variable value will be returned, then it will be incremented or decremented, for example:


$x = 5;
$y = ++$x;    // $y = 6 


But:


$x = 5;
$z = $x++;    // $z = 5


Bitwise operators:

Bitwise operators manipulate variables at bit level, they are a binary operators, and its operands have to be integer:


AND (&):

A


B


A & B


0


0


0


0


1


0


1


0


0


1


1


1


• OR (|):

A


B


A | B


0


0


0


0


1


1


1


0


1


1


1


1


• XOR (^):

A


B


A | B


0


0


0


0


1


1


1


0


1


1


1


0



• NOT (~):

A


~A


0


1


0


0



Left shift (<<) and right shift (>>): Shifts the bits of the left operand left or right by the number of positions specified by the right.


Error control operator:

When PHP faces an error in the code, it outputs a verbose error message to help in finding the cause of the error an fixing it.


Sometimes the error is wanted to be not reported, even if it occurs.


There is an error control operator (@) to prevent error reporting.



@ can be used as the following:


<?php
@$x = 100 / 0;
?>


Dividing by zero should return an error, but when @ is used, no error will be reported.
Syntax errors are not caught by using @, so they will still be reported even with using @.



Read Next: PHP Tutorials - Operators



 

 

Comments



Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2009 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape