Operators



In general, we can call it symbolic characters used in a programming language. Each symbol that is an operator has a task in programming. Some characters have a representation as a string. For example, && / or. At the same time, operators generally construct the syntax of a programming language. You can think of it as punctuation marks used in a real human language. A code can be used for operations such as denoting the end (;) or a number increment (++), or subtracting from a number (-).

 

 

# Section Headings


# Operator Compilation Priority
# Transaction Priority Operator
# Arithmetic Operators
# Assignment Operator
# Comparison Operator

Comparison in Series

# Increment and Decrement Operators
# Logical Operators
# String Operators
# Merge Operators
# Object Control Operator
# Type Conversion Operators 
# Scope Resolution Operators

 

 

# Operator Compilation Priority


Operators have a process priority status. This is what happens in programming if there is a multiplier in mathematics and the division has priority over addition and subtraction. The transaction priority and transaction direction of the operators are listed below. The process priority is ordered from top to bottom.

Process priority direction Tour
clonenew nondirectional Cloning and Object Derivation
[] from left Sequence
++ , - nondirectional Increase and Decrease
( int ), ( float ), ( string ) right Change type
instanceof nondirectional Object ownership
! right Logical
* , / , % , + , - from left Arithmetic
. from left String concatenation
<< , >> from left bitwise
< , > , <= , > = , <> , == , ! = , === , ! == nondirectional Comparison
& , ^ , | from left bitwise
&& , || from left Logical
?? right Null comparison
? : from left 3 comparison
=, + =, - =, * =, / =,% =, & =, | =, ^ =, << =, >> =  right Assignment
and from left Logical
x from left Logical
or from left Logical

 

 

# Transaction Priority Operator


Despite the above information, the best way to prevent ambiguity about transaction precedence is to use parentheses (). As in mathematics, parentheses are used as transaction priority operators in programming languages.

() Specifies the operation priority.

For example, the use of parentheses is very convenient in the following example.


$proc = 5 * 2 - 5 % 3; # (5 * 2) - (5 % 3)

$proc = true ? false : true ? true : NULL; # (true ? false : true) ? true : NULL
?>

 

 

# Arithmetic Operators


Operators are used to perform mathematical operations.

$ x + $ y Sum of $ x and $ y
$ x - $ y Difference between $ x and $ y
$x * $y $x ile $y'nin çarpımı
$x / $y $x ile $y'nin bölümü
$x % $y $x`in $y`ye bölümünden kalanı.


$x = 10; 
$y = 5;

echo $x * ($y - 2);
30

 

 

# Atama Operatörü


Atama operatörünün (=) amacı bir değişkene bir nesnenin değerini atar. Atama operatörü ile bilinmesi gereken önemli bir nokta bu operatör karşılaştırma değil değer aktarma işlemi gerçekleştirir. Karşılaştırma için (==) operatörü kullanılır.

$a = $b $a'ya $b'nin değerini ata.
$a += $b $ a 'or $ a and $ b ' n assign the total. Actually, the constructed $ a = $ a + $ b process.
$ a - = $ b $ a 'or $ a and $ b ' n assign the difference. Actually, the constructed $ a = $ a - $ b process.
$ a * = $ b $ a 'or $ a and $ b ' n Assign product. Actually, the constructed $ a = $ a * $ b process.
$ a / = $ b $a'ya $a'nın $b'ye bölümü ata. Aslında yapılan $a = $a / $b işlemidir.


$a  = 10;
$b  = $a;
$c  = 5;
$c *= $b;

echo $a . ' - ' . $b . '-' . $c;
10 - 1050

 

 

# Karşılaştırma Operatörleri


Karşılaştırma operatörleri isminden de anlaşılacağı üzere iki değerin karşılaştırılması için kullanılır. Özellikle karar yapıları ile kullanılan bu operatörler kod akışına yön vermekte kullanılırlar.

$a == $b $a ile $b aynı değere sahipse true döner. Aksi halde false.
$a === $b Returns true if both $ a and $ b have the same value and the same type .
$ a ! = $ b Returns true if $ a and $ b do not have the same value .
$ a ! == $ b Returns true if $ a and $ b have the same value or the same type .
$ a < $ b $ a , $ b ' returns true if it is small .
$ a > $ b Returns true if $ a , $ b 'den is big .
$ a <= $ b Returns true if $ a , $ b is less than or equal to $ b .
$ a > = $ b Returns true if $ a , $ b 'den is greater than or equal to $ b ` .


$a = 5;
$b = 10;

echo $a < $b
true


$a = '10';
$b = 10;

echo $a == $b;
echo $a === $b;
true
false
Series Comparisons

It's like the variants in the series.



$a = ['a' => 1, 'b'];
$b = ['a' => '1', 'b'];

echo $a == $b;
echo $a === $b;
true
false

 

 

# Increase and Decrease Operators


They are used to increment or decrement the value of a variable.

$ a ++ After incrementing $ a , increment it by 1 .
$ a - Decrement 1 after returning the value of $ a .
++ $ a Returns the value of $ a after incrementing by 1 .
- $ a $ a 's value to 1 return after the decrease.


$a = 5;

echo $a++;
echo $a;
5
6


$a = 5;

echo ++$a;
echo $a;
6
6

 

 

# Logical Operators


They are used to make comparisons in the logical kind. At least two conditions are evaluated and used to direct code flow according to the correctness or inaccuracy condition of these conditions.

$ a and $ b , $ a && $ b And $ a  and  $ b  both return true .
$ a or $ b , $ a || $ b Or Returns false  if $ a  or  $ b is true .
$ a xor $ b Privileged Or Returns true  if $ a  or  $ b is true , false otherwise.
! $ a Not Returns true  if $ a is not true .


$a = 5;
$b = 10;

echo $a === 5 && $b === 10;
echo $a < 10 || $b > 10;
echo $a === 5 xor $b === 10;
echo ($a > 10 || $b >= 10) && $a < $b;
true
true
false
true

 

 

# String Operators


Used to specify sequences. Arrays are usually quoted strings that are used to produce output.

$ a = "any expression" Used to display sequences. 
$ a = 'any expression' Used to display sequences.

There is a small difference between single quotes and double quotes. Variables written in double-quoted expressions are perceived as variables. Variables written in single quotes are perceived as ordinary expressions.



$a = 10;

echo "Sayı: $a";
echo 'Sayı: $a';
Issue: 10
Issue: $ a

Note that in the second output, the variable assignment is perceived as an ordinary character. 

 

 

# Merge Operator


The join operator is usually used for arrays. In addition, joining can be done in the knees.

string in . Used to combine 2 or more strings. It is like the concat () function.
Arrays + Used to combine 2 or more sequences. It looks like array_merge () .


$a = 'Hello';
$b' World';
$c = $a . $b;

echo $c;
Hello World


$a = ['a', 'b' => 1];
$b = ['a', 'c', 'd' => 2];
$c = $a + $b;

var_dump($c);
[ 'a' , 'b' => 1 , 'c' , 'd' => 2]

 

 

# Object Control Operator


The object control checks whether the object of interest of the control operator belongs to a specified class.

$ object instanceof ExampleClass Is the $ object variable an instance of the ExampleClass class?


$a = new MySQLi;

echo $a instanceof MySQLi;
true

 

 

# Type Conversion Operators


Type conversion operators are used to change the type of a variable.

( int ) $ a The variable $ a returns the number of integers.
( string ) $ a The $ a variable converts the string to string.
( bool ) $ a The variable $ a translates to the logical type.
( array ) $ a The variable $ a translates to the appropriate array.
( object ) $ a The $ a variable turns the object into an object.


$a = ['a' => 1, 'b' => 2];

echo $a['a'];

$a = (object) $a;

echo $a->a;
1
1

 

 

# Scope Resolution Operators


Scope resolution operators are used specifically to access sub-objects belonging to a class or object.

:: The static method is used to access attributes and class constants.
-> Used to access sub-objects or classes methods and attributes for objects.
=> This operator is used as a delimiter in relational sequences. And only the sequence containing the key value pair is specific.


echo ExampleClass::$property;

$a = new XML;

echo $a->getVersion();

$a = (object) ['a' => 1];

echo $a->a; # 1

$a = ['key' => 'Value'];

echo $a['key']; # Value