TUTSCODE PHP 007: OPERATOR IN PHP

CAKE PHP

Posted on 2015-07-08 08:45:06


1. Operators Arithmetic Arithmetic is the simplest form of calculation, subtraction, multiplication, and division in arithmetic. There are also divisions take account balance (%). Used to retrieve the remainder of the first unit operation.

Operator Name For example Result
+ Czech $ X + $ y The total of $ x and $ y
- Minus $ X - $ y Effect of $ x and $ y
* Staff $ X * $ y Integration of $ x and $ y
/ Natural divide $ X / $ y The result of the natural divide of $ x to $ y
% Divide surplus $ X% $ y The result of the division of $ x to obtain balance $ y

For example:

1
2
3
4
5
6
7
8
9
<? Php
$x=10;
$ Y = 6;
echo ( $ x + $ y ); // result 16
echo ( $ x - $ y ); // result 4
echo ( $ x * $ y ); // result 60
echo ( $ x / $ y ); // result 1.6666666666667
echo ( $ x % $ y ); // result 4
?>

2. The assignment operator In PHP, the assignment operator is used to assign a value to a variable. The most basic usage is to use the "=". For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<? Php
$x=10;
echo $ x ; // result 10
$ Y = 20;
$ Y + = 100;
echo $ y ; // result 120
$ Z = 50;
$ Z - = 25;
echo $z; // outputs 25
$i=5;
$i *= 6;
echo $ i ; // result 30
$j=10;
$j /= 5;
echo $ j ; // result 2
$k=15;
$k %= 4;
echo $ k ; // result 3
?>

Some expressions assigned:

Expression Equivalent expression Description
x = y x = y Assign y for x
x + = y x = x + y Plus y to the variable x
x - = y x = x - y Set x = x – y
x * = y x = x * y Set x = x * y
x / = y x = x / y Set x = x/y
x% = y x = x% y Set x = x%y

3. Operators string manipulation In this section we refer only to a single operator to connect two strings together. It is the operator ".". For example:

1
2
3
4
5
6
7
$str1 = "Hello";
$str2 = "Tutscode!";
$str = $str1." ".$str2;
echo $str;

The result is printed to the screen: Hello Tutscode !. $ Str transplant chain by connecting three consecutive string $ str1, "", $ str2. This operator also allows us to assign a shortened form:

1
2
3
4
5
$str = "Hello";
$str .= " world!";
echo $str;

Print results screen: Hello world !. In the example above, $ str. = "World" equals $ str = $ str. "World"; 4. Operator rises and falls In PHP 4 is often used to increase or decrease as operators: ++ $ x, $ x ++, - $ x, $ x-.Where $ x is a variable.

 Operator Name Description
++$x Pre-increment Up $ x add 1 or surrender value of $ x
$x++ Post-increment Returns the value of $ x, then rose $ x more 1
–$x Pre-decrement X goes down $ 1, then return the value of $ x
$x– Post-decrement Returns the value of $ x, then x goes down $ 1

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
<? Php
$x=10;
echo ++ $ x ; // result 11
$ Y = 10;
echo $ y ++; // result 10
$ Z = 5;
echo - $ z ; // result 4
$i=5;
echo $ i -; // result 5
?>

5. Comparison operators Comparison operators are often used for the conditional expression. For example:

1
if($x == $y) echo "the same";

Here are the common operators used in PHP:

 Operator Name  For example Result
== Equal $ X == $ y True if $ x by $ y
=== Identical $ X === $ y True if $ x by $ y both style and value
! = Not equal $ X! = $ Y True if $ x other $ y
< Not equal $x <> $y True if $ x is not equal to $ y
! == Not identical $ X! == $ Y True if $ x is not equal to $ y in value or in kind
> Greater than $ X> $ y True if $ x is greater than $ y
< Less than $ X <$ y True if $ x is less than $ y
>= Greater than or equal to $ X> = $ y True if $ x is greater than or equal to $ y
<= Less than or equal to $ X <= $ y True if $ x less than or equal to $ y

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<? Php
$x=100;
$ Y = "100" ;
var_dump($x == $y); //Result "true"
echo "<br>";
var_dump($x === $y); //Result  "false"
echo "<br>";
var_dump($x != $y);//Result  "false"
echo "<br>";
var_dump($x !== $y);//Result  "true"
echo "<br>";
$a=50;
$b=90;
var_dump($a > $b);//Result  "false"
echo "<br>";
var_dump($a < $b);//Result  "true"
?>

6. logical operators Like other languages, PHP provides us some logical operators follows:

 Operator  Name For example Result
and And $x and $y True if both $ x and $ y are true
or Or $x or $y True if either $ x or $ y true
xor Xor $x xor $y True if either $ x or $ y true, or neither true
&& And $x && $y True if both $ x and $ y are true
|| Or || $ X $ y True either $ x or $ y true
! Not !$x True if $ x is not true