PHP Operators

Submitted by: 
Language: 
Visitors have accessed this post 8778 times.

In every programming language, operators play an important role. It can be use in comparison statements like IF statement, loops statement like WHILE loops, assigning a value to a variable and a lot more.

Comparison Operators

Operator Description Example
>= is greater than or equal to 4>=9 returns false
<= is less than or equal to 5<=7 returns true
is less than 2<7 returns true
is greater than 3>9 returns false
<>  is not equal 2<>3 returns true
!= is not equal 4!=8 returns true
== is equal to 2==1 returns false

Example of comparison operators in IF statement:

  1. <?php
  2. If (4>=9){
  3. //the statement here will not be executed
  4. }
  5. ?>

Arithmetic Operators

Operator Description Example Result
* Multiplication x=2
x*3
6
/ Division 9/3
10/2
3
5
+ Addition x=3
x+4
7
- Subtraction x=1
5-x
4
-- Decrement x=4
x--
x=3
++ Increment x=7
x++
x=8
% Modulus (division remainder) 5%2
10%8
10%2
1
2
0

Example of arithmetic operators in mathematics:

  1. <?php
  2. $x = 2;
  3. $y = $x + 3;
  4.  
  5. echo $y;
  6. ?>

Logical Operators

Operator Description Example
&& and x=8
y=2
(x < 10 && y > 1) returns true
! not x=7
y=2
!(x==y) returns true
|| or x=8
y=2
(x==8 || y==2) returns false

Example of logical operators is IF statement:

  1. <?php
  2. $x=8
  3. $y=2
  4.  
  5. If ($x==8 || $y==2){
  6. //the statement here will be executed
  7. }
  8. ?>

Assignment Operators

Operator Example Is The Same As
+= a+=b a=a+b
*= a*=b a=a*b
= a=b a=b
.= a.=b a=a.b
-= a-=b a=a-b
%= a%=b a=a%b
/= a/=b a=a/b

Example of assignment operators in a variable x and y:

  1. <?php
  2. $x = 2;
  3. $y = $x + 3;
  4. ?>

Comments

Hello sir,...can i have a copy of all your tutorials in php?
[email protected]

tnx..

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.