Learn PHP Operators with example
Operators are mathematical, string, comparison, and logical
commands such as minus , plus, divide, times. PHP operators are very similar to
plain arithmetic for eg.
echo 10+5;
this will output 15
Various arithmetic operators in PHP are as follows
Operator
|
Description
|
Example
|
+
|
Addition
|
$m+1
|
-
|
Subtraction
|
$m-1
|
*
|
Multiplication
|
$m*5
|
/
|
Division
|
$m/2
|
++
|
Increment
|
++$m
|
--
|
Decrement
|
--$m
|
%
|
Modulus
|
$m%2
|
PHP Assignment operator
Assignment operators are used to assign values to variables.
Operator
|
Example
|
Equivalent to
|
=
|
$m=5
|
$m=5
|
+=
|
$m +=2
|
$m= $m+2
|
-=
|
$m -=2
|
$m=$m-2
|
*=
|
$m*=2
|
$m=$m*2
|
/=
|
$m/=2
|
$m=$m/2
|
.=
|
$m.=$h
|
$m=$m.$h
|
%=
|
$m%=2
|
$m=$m%2
|
PHP Comparison operator
Comparison operator are used inside a construct such as an
if statement where you need to compare two items.
Operator
|
Description
|
Example
|
==
|
Is equal to
|
$m==2
|
!=
|
Is not equal to
|
$m!=3
|
>
|
Is greater than
|
$m>3
|
<
|
Is less than
|
$m<25
|
>=
|
Is greater than or equal to
|
$m>=25
|
<=
|
Is less than equal to
|
$m<=5
|
PHP Logical Operator
Logical operator can also be input to another logical
operator. If something has a true or false value, it can be used as input to a
logical operator. A logical operator takes two true or false inputs and
produces a true or false result.
&& is interchangeable with “ and ”
|| is interchangeable
with “ or “
Example
If($time>12 && $time<13) dosleep();
Mysql_select_db($database) or die(“unable to select
database”);
$ingredent = $ammonia xor $bleach;
PHP Variable assignment
Variable = value;
Variable=variable;
$m +=5;
$y -=5;
PHP Variable incrementing and decrementing
++$m;
--$y;
If(++$m==5)echo$m;
If($m-- ==1) echo $m;
0 comments :
Post a Comment