Operator

This time, let's learn about operators.
Operators seem hard, but they are arithmetic.
Addition is +
Subtraction is-
Multiplication
Division is /
rest of it %
How to use is as follows.

<?php
    echo "Operator usage example<br />";

    $a = 8;
    $b = 5;

    $value = $a + $b;
    echo "$a + $b = $value <br>";

    $value = $a - $b;
    echo "$a - $b = $value <br>";

    $value = $a * $b;
    echo "$a * $b = $value <br>";

    $value = $a / $b;
    echo "$a / $b = $value <br>";

    $value = $a % $b;
    echo "$a % $b = $value <br>";
?>
php image

Increment operator, decrement operator
There is something called ++,- Using these, ++ increases 1, and-decreases 1.
Increment(++)
$a ++: Returns the value of the variable $a first, and increments it by 1.
$++ a: First, increment 1 and return the variable $a.
decrease(--)
$a--: return the value of variable $a first, decrement by 1
$-a: Decrease 1 first and return $a
Let's understand the increase and decrease operator!
What is the value of $b = $a ++ when $a = 5?
$a ++ returns the value first and increments it by 1. Therefore, it returns its own value of 5. Then $b becomes 5. Then increase it by 1. Therefore, the value of a is 6.
$A = 6 and $b = 5.
So what is $b = ++ $a this time when $a = 5?
a is 5, but ++ $a, so increase 1 first. Then return. Therefore b is 6 since a = 6 and then returned.
If you understand by the source

<?php
    echo "Calculate result of operation of b = a++ when a = 5 <br />";
    echo " ============================================== ";

    $a = 5;
    $b = $a++;

    echo "\$a=$a... output 1 increased to the variable \$a<br />";
    echo "\$b=$b... The value is assigned to \$b before the value of variable \$a is increased by one  <br />";

    echo "When \$a = 5, the result of the calculation of \$b = ++ \$a the values ​​of \$a and \$b? <br />";

    $a = 5 ;
    $b = ++$a;

    echo "\$a=$a... Output variable \$a increased by 1 <br />";
    echo "\$b=$b... The value of the variable \$a is first incremented by 1 and assigned to \$b <br />";
?>

The result is shown below.

php image

Assignment operator

When you declare $var = 123, you should understand that 123 is assigned to var, rather than that var and 123 are equal.
The assignment operator uses the = sign. Then $a = $a + 5 can be reduced to $a + = 5 using the compound operator.
Also use. = To assign a string.

operator

example

Same example

Result

meaning

+=

$a = 10
$a += 5

$a = 10
$a = $a + 5

15

Assign result of $a = 10 + 5

-=

$a = 10
$a -= 5

$a = 10
$a = $a - 5

5

Assign result of $a = 10 - 5

*=

$a = 2
$a *= 5

$a = 2
$a = $a * 5

10

Assign result of $a = 2 * 5

/=

$a = 35
$a /= 5

$a = 35
$a = $a / 5

7

Assign result of $a = 35 / 5

%=

$a = 7
$a %= 5

$a = 7
$a = $a % 5

2

Assign result of $a = 7 % 5

.=

$a = "mickey"
$a .= "mouse"
echo $a

$a = "mickey"
$a = $a."mouse"
echo $a

mickey mouse

Assign result of $a = $a."mouse"

Take advantage of renter operators

<?php
    echo "Use of assignment operator  <br />";

    $a = 3;
    $a += 5;

    echo "\$a = 3<br />";
    echo "\$a += 5<br /> result : $a <br />";

    $a = 7;
    $a %= 5;
    echo "\$a = 7<br />";
    echo "\$a %= 5<br /> result... <b>$a</b><br />";

    $a = "mickey";
    $a .= "mouse";
    echo "\$a = \"mickey\" <br />";
    echo "\$a .= \"mouse\" <br />";
    echo "result..-->  $a <br />";
?>

The result is as follows. In the source, \ "<-means".

php image

Comparison operator

Comparison operators are often used in conditional expressions such as control and loop statements.
Compares two conditions and executes the command if it is true and does not execute the command if it is false.

operator

example

meaning

==

$a == $b

true if $a and $b have the same value

!=

$a != $b

true if the values of $a and $b are different

<>

$a <> $b

true if the values of $a and $b are different

>

$a > $b

true if $ais greater than the value of $b

<

$a < $b

true if $a is less than $b

>=

$a >= $b

true if $a is greater than or equal to the value of $b

<=

$a <= $b

true if $a is less than or equal to the value of $b

Logical operator

Logical operators are operators that return true and false. You can combine multiple comparison operators.

operator

example

meaning

and

$a and $b

true if both $a and $b are true

or

$a or $b

true if any of $a and $b is true

xor

$a xor $b

true if only one of $a and $b is true

!

!$a

true if $a is false

&&

$a && $b

true if both $a and $b are true

||

$a || $b

true if any of $a and $b is true

As shown in the table above, and and && are the same
The same is true for or and ||.

bitwise operators

Bitwise operators perform operations in the form of binary numbers combined with 0s and 1s..

operator

example

meaning

&

$a & $b

Logical product between unit bits of $a and $b

|

$a | $b

OR between $a and $b unit bits

^

$a ^ $b

xor between the unit bits of $a and $b

~

~$a

Negative unit bits of $a(0 to 1, 1 to 0)

<<

$a << 3

Shift bit string of $a left by 3 bits

>>

$a >> 2

Shift bit string of $a right by 2 bits

If you are not an engineering student, you may be able to say what bit string movement means.
If 5 is represented in binary, it is 0101. 1 2 4 8 16 32 64 128 256 512 1024 2048

0 1 0 1

8 4 2 1

The number above is binary and the value below is what each digit represents.
That is, 1 in 101 is marked with 4 and 1 digits, so that means 5.
To see each other's conversion method, you can easily see how in Google.
So what is bit shift?
If it is 2 bit right shift in binary 0101 of 5, it is 0001 because it is 2 bit right shift. The shifted 2 bits will disappear and the new place will be filled with zeros. Therefore the value is 0001 and the value is 1.
Back to the course
Looking at the use of bit operators as a source ...

<?php
    echo "Using bitwise operators when \ $a = 5 and \ $b = 8 <br />";


    $a = 5;
    $b = 8;

    // 5 is 0101 in binary
    // 8 is 1000 in binary

    $c = $a & $b;
    $d = $a | $b;
    $e = $a << 3;
    $f = $b >> 2;

    echo "\$a & \$b = $c";
    echo ".... 0101 * 1000 = <b>0000</b> <br /><br />";

    echo "\$a | \$b = $d";
    echo ".... 0101 + 1000 = 1101 <br /><br />";

    echo "\$a << 3 = $e <br />";
    echo ".... Move 3 bits to the left and then 101000 <br /><br />";

    echo "\$b >> 2 = $f <br />";
    echo ".... 1000 Two bits to the right and then 0010 <br />";
?>

And the result is as follows.

php image

비트연산자에 대해서 잘 모르시는 분들은 좀 이해가 안가시는게 있을실것 같습니다.

If you are not familiar with bit operators, you may not understand.
a is 5 and b is 8
The binary number of each number is 0101,1000.
This is why the answer is 0 when you multiply with &.
In &, each digit must overlap with 1 to be 1.
In other words
5 = 0101
8 = 1000
If you look at it, it's going to be 0 because there's no single digit that overlaps with 1