Exforsys.com
 
Home Tutorials PHP
 

PHP Tutorials - Operators

 
Category: PHP
Comments (0)

PHP Tutorials - Operators

In this PHP Tutorial you will learn about Operators - String concatenation operator, Comparison operators, Logical operators, Type casting, Combined assignment operator, Operators precedence and Operators precedence order.



String concatenation operator:

The string concatenation operator (.) concatenates two strings.


Example:


<?php
    $s1 = “String1”;
    $s2 = $s1 . “ and String2 are concatenated.”;
?>


Comparison operators:

Comparison operators are binary operators, they compare between two operands of any type, and return a Boolean value:


 == to check if two operands are equal.
 != to check if two operands are not equal.
 < to check if the left operand is less than the right operand.
 > to check if the left operand is greater than the right operand.
 <= to check if the left operand is less than or equals the right operand.
 >= to check if the left operand is greater than or equal the right operand.


If two operand are not of the same type, PHP will automatically convert one of them to the other one’s type, but this may cause problems, because if we try to compare 0 and ‘text’ for equality, PHP will convert ‘text’ to an integer, the conversion will return 0, so they will be equal, which is not correct, to avoid such a situation, PHP has two more comparison operators:


=== to check if the two values are equal and the type is the same


!== to check if the two operands are not equal or their types are not the same.


Logical operators:

Logical operators are binary operators, and they return a Boolean value, PHP logical operators are:


AND (&&) :


A


B


A AND B


False


False


False


False


True


False


True


False


False


True


True


True


OR (||):


A


B


A OR B


False


False


False


False


True


True


True


False


True


True


True


True


XOR:


A


B


A XOR B


False


False


False


False


True


True


True


False


True


True


True


False


NOT (!):


A


NOT(A)


False


True


True


False


Type casting:

Type casting operator forces the conversion of a data to another data type, type casting operators are:


(int): to cast to an integer, for example:
    $x = 10.5;
    $y = (int)$x; // $y = 10
(float) to cast to a float..
(string) to cast to a string.
(array) to cast to an array.
(object) to cast to an object.


Combined assignment operator:

The assignment operator can be combined with another operation, for example:


    $x = 10;
    $x+= 5; // $x = 15


The right hand operator is added to the left handed operator, and the result stored at the left hand operator.


Operators precedence:

Precedence defines the order of operators execution if there is more than one operator in the statement.
If two operators have the same precedence order, then execution is started from the left and ends at the right.


Operators precedence order:


[
! ~ ++ -- (int) (float) (string) (array) (object)
* / %
<< >>
< <= > >=
== != === !==
&
^
|
&&
||
? :
= += -= *= /= .= %= &= |= ^=
<<= >>=
and
xor
or


You can override precedence order rules by using parentheses, for example:


<?php
    $x = 3 * 5 + 7; // $x = 22
    $y = 3 * (5 + 7); // $y = 36
?>



Read Next: PHP Tutorials - Conditional Statements



 

 

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