PHP Operatörleri
Operatörler, değişkenler ve değerler üzerinde işlemler gerçekleştirmek için kullanılır.
PHP, operatörleri aşağıdaki gruplara ayırır:
PHP aritmetik operatörleri, toplama, çıkarma, çarpma vb. Gibi yaygın aritmetik işlemleri gerçekleştirmek için sayısal değerlerle kullanılır.
PHP Atama Operatörleri
PHP atama operatörleri, bir değişkene bir değer yazmak için sayısal değerlerle kullanılır.
PHP'deki temel atama operatörü "=" dir. Sol işlenenin sağdaki atama ifadesinin değerine ayarlandığı anlamına gelir.
PHP Karşılaştırma Operatörleri
PHP karşılaştırma operatörleri iki değeri (sayı veya dize) karşılaştırmak için kullanılır:
PHP Arttırma / Azaltma Operatörleri
PHP artış operatörleri bir değişkenin değerini artırmak için kullanılır.
PHP eksiltme operatörleri, bir değişkenin değerini azaltmak için kullanılır.
PHP Mantıksal Operatörleri
PHP mantıksal operatörleri koşullu ifadeleri birleştirmek için kullanılır.
PHP String Operatörleri
PHP'nin dizeler için özel olarak tasarlanmış iki operatörü vardır.
PHP Dizisi Operatörleri
PHP dizi operatörleri dizileri karşılaştırmak için kullanılır.
PHP Koşullu Atama Operatörleri
PHP koşullu atama operatörleri, koşullara bağlı olarak bir değer ayarlamak için kullanılır:
Operatörler, değişkenler ve değerler üzerinde işlemler gerçekleştirmek için kullanılır.
PHP, operatörleri aşağıdaki gruplara ayırır:
- Aritmetik operatörler
- Atama operatörleri
- Karşılaştırma operatörleri
- Artırma / Azaltma operatörleri
- Mantıksal operatörler
- Dize operatörleri
- Dizi operatörleri
- Koşullu atama operatörleri
PHP aritmetik operatörleri, toplama, çıkarma, çarpma vb. Gibi yaygın aritmetik işlemleri gerçekleştirmek için sayısal değerlerle kullanılır.
Operator | Name | Example | Result | |
---|---|---|---|---|
+ | Addition | $x + $y | Sum of $x and $y | <!DOCTYPE html> <html> <body> <?php $x = 10; $y = 6; echo $x + $y; ?> </body> </html> |
- | Subtraction | $x - $y | Difference of $x and $y | <!DOCTYPE html> <html> <body> <?php $x = 10; $y = 6; echo $x - $y; ?> </body> </html> |
* | Multiplication | $x * $y | Product of $x and $y | <!DOCTYPE html> <html> <body> <?php $x = 10; $y = 6; echo $x * $y; ?> </body> </html> |
/ | Division | $x / $y | Quotient of $x and $y | <!DOCTYPE html> <html> <body> <?php $x = 10; $y = 6; echo $x / $y; ?> </body> </html> |
% | Modulus | $x % $y | Remainder of $x divided by $y | <!DOCTYPE html> <html> <body> <?php $x = 10; $y = 6; echo $x % $y; ?> </body> </html> |
** | Exponentiation | $x ** $y | Result of raising $x to the $y'th power | <!DOCTYPE html> <html> <body> <?php $x = 10; $y = 3; echo $x ** $y; ?> </body> </html> |
PHP atama operatörleri, bir değişkene bir değer yazmak için sayısal değerlerle kullanılır.
PHP'deki temel atama operatörü "=" dir. Sol işlenenin sağdaki atama ifadesinin değerine ayarlandığı anlamına gelir.
Assignment | Same as... | Description | |
---|---|---|---|
x = y | x = y | The left operand gets set to the value of the expression on the right | <!DOCTYPE html> <html> <body> <?php $x = 10; echo $x; ?> </body> </html> |
x += y | x = x + y | Addition | <!DOCTYPE html> <html> <body> <?php $x = 20; $x += 100; echo $x; ?> </body> </html> |
x -= y | x = x - y | Subtraction | <!DOCTYPE html> <html> <body> <?php $x = 50; $x -= 30; echo $x; ?> </body> </html> |
x *= y | x = x * y | Multiplication | <!DOCTYPE html> <html> <body> <?php $x = 10; $y = 6; echo $x * $y; ?> </body> </html> |
x /= y | x = x / y | Division | <!DOCTYPE html> <html> <body> <?php $x = 10; $x /= 5; echo $x; ?> </body> </html> |
x %= y | x = x % y | Modulus | <!DOCTYPE html> <html> <body> <?php $x = 15; $x %= 4; echo $x; ?> </body> </html> |
PHP Karşılaştırma Operatörleri
PHP karşılaştırma operatörleri iki değeri (sayı veya dize) karşılaştırmak için kullanılır:
Operator | Name | Example | Result | |
---|---|---|---|---|
== | Equal | $x == $y | Returns true if $x is equal to $y | <!DOCTYPE html> <html> <body> <?php $x = 100; $y = "100"; var_dump($x == $y); // returns true because values are equal ?> </body> </html> |
=== | Identical | $x === $y | Returns true if $x is equal to $y, and they are of the same type | <!DOCTYPE html> <html> <body> <?php $x = 100; $y = "100"; var_dump($x === $y); // returns false because types are not equal ?> </body> </html> |
!= | Not equal | $x != $y | Returns true if $x is not equal to $y | <!DOCTYPE html> <html> <body> <?php $x = 100; $y = "100"; var_dump($x != $y); // returns false because values are equal ?> </body> </html> |
<> | Not equal | $x <> $y | Returns true if $x is not equal to $y | <!DOCTYPE html> <html> <body> <?php $x = 100; $y = "100"; var_dump($x <> $y); // returns false because values are equal ?> </body> </html> |
!== | Not identical | $x !== $y | Returns true if $x is not equal to $y, or they are not of the same type | <!DOCTYPE html> <html> <body> <?php $x = 100; $y = "100"; var_dump($x !== $y); // returns true because types are not equal ?> </body> </html> |
> | Greater than | $x > $y | Returns true if $x is greater than $y | <!DOCTYPE html> <html> <body> <?php $x = 100; $y = 50; var_dump($x > $y); // returns true because $x is greater than $y ?> </body> </html> |
< | Less than | $x < $y | Returns true if $x is less than $y | <!DOCTYPE html> <html> <body> <?php $x = 10; $y = 50; var_dump($x < $y); // returns true because $x is less than $y ?> </body> </html> |
>= | Greater than or equal to | $x >= $y | Returns true if $x is greater than or equal to $y | <!DOCTYPE html> <html> <body> <?php $x = 50; $y = 50; var_dump($x >= $y); // returns true because $x is greater than or equal to $y ?> </body> </html> |
<= | Less than or equal to | $x <= $y | Returns true if $x is less than or equal to $y | <!DOCTYPE html> <html> <body> <?php $x = 50; $y = 50; var_dump($x <= $y); // returns true because $x is less than or equal to $y ?> </body> </html> |
<=> | Spaceship | $x <=> $y | Returns an integer less than, equal to, or greater than zero, depending on if $x is less than, equal to, or greater than $y. Introduced in PHP 7. | <!DOCTYPE html> <html> <body> <?php $x = 5; $y = 10; echo ($x <=> $y); // returns -1 because $x is less than $y echo "<br>"; $x = 10; $y = 10; echo ($x <=> $y); // returns 0 because values are equal echo "<br>"; $x = 15; $y = 10; echo ($x <=> $y); // returns +1 because $x is greater than $y ?> </body> </html> |
PHP artış operatörleri bir değişkenin değerini artırmak için kullanılır.
PHP eksiltme operatörleri, bir değişkenin değerini azaltmak için kullanılır.
Operator | Name | Description | |
---|---|---|---|
++$x | Pre-increment | Increments $x by one, then returns $x | <!DOCTYPE html> <html> <body> <?php $x = 10; echo ++$x; ?> </body> </html> |
$x++ | Post-increment | Returns $x, then increments $x by one | <!DOCTYPE html> <html> <body> <?php $x = 10; echo $x++; ?> </body> </html> |
--$x | Pre-decrement | Decrements $x by one, then returns $x | <!DOCTYPE html> <html> <body> <?php $x = 10; echo --$x; ?> </body> </html> |
$x-- | Post-decrement | Returns $x, then decrements $x by one | <!DOCTYPE html> <html> <body> <?php $x = 10; echo $x--; ?> </body> </html> |
PHP mantıksal operatörleri koşullu ifadeleri birleştirmek için kullanılır.
Operator | Name | Example | Result | |
---|---|---|---|---|
and | And | $x and $y | True if both $x and $y are true | <!DOCTYPE html> <html> <body> <?php $x = 100; $y = 50; if ($x == 100 and $y == 50) { echo "Hello world!"; } ?> </body> </html> |
or | Or | $x or $y | True if either $x or $y is true | <!DOCTYPE html> <html> <body> <?php $x = 100; $y = 50; if ($x == 100 or $y == 80) { echo "Hello world!"; } ?> </body> </html> |
xor | Xor | $x xor $y | True if either $x or $y is true, but not both | <!DOCTYPE html> <html> <body> <?php $x = 100; $y = 50; if ($x == 100 xor $y == 80) { echo "Hello world!"; } ?> </body> </html> |
&& | And | $x && $y | True if both $x and $y are true | <!DOCTYPE html> <html> <body> <?php $x = 100; $y = 50; if ($x == 100 && $y == 50) { echo "Hello world!"; } ?> </body> </html> |
|| | Or | $x || $y | True if either $x or $y is true | <!DOCTYPE html> <html> <body> <?php $x = 100; $y = 50; if ($x == 100 || $y == 80) { echo "Hello world!"; } ?> </body> </html> |
! | Not | !$x | True if $x is not true | <!DOCTYPE html> <html> <body> <?php $x = 100; if ($x !== 90) { echo "Hello world!"; } ?> </body> </html> |
PHP'nin dizeler için özel olarak tasarlanmış iki operatörü vardır.
Operator | Name | Example | Result | |
---|---|---|---|---|
. | Concatenation | $txt1 . $txt2 | Concatenation of $txt1 and $txt2 | <!DOCTYPE html> <html> <body> <?php $txt1 = "Hello"; $txt2 = " world!"; echo $txt1 . $txt2; ?> </body> </html> |
.= | Concatenation assignment | $txt1 .= $txt2 | Appends $txt2 to $txt1 | <!DOCTYPE html> <html> <body> <?php $txt1 = "Hello"; $txt2 = " world!"; $txt1 .= $txt2; echo $txt1; ?> </body> </html> |
PHP dizi operatörleri dizileri karşılaştırmak için kullanılır.
Operator | Name | Example | Result | |
---|---|---|---|---|
+ | Union | $x + $y | Union of $x and $y | <!DOCTYPE html> <html> <body> <?php $x = array("a" => "red", "b" => "green"); $y = array("c" => "blue", "d" => "yellow"); print_r($x + $y); // union of $x and $y ?> </body> </html> |
== | Equality | $x == $y | Returns true if $x and $y have the same key/value pairs | <!DOCTYPE html> <html> <body> <?php $x = array("a" => "red", "b" => "green"); $y = array("c" => "blue", "d" => "yellow"); var_dump($x == $y); ?> </body> </html> |
=== | Identity | $x === $y | Returns true if $x and $y have the same key/value pairs in the same order and of the same types | <!DOCTYPE html> <html> <body> <?php $x = array("a" => "red", "b" => "green"); $y = array("c" => "blue", "d" => "yellow"); var_dump($x === $y); ?> </body> </html> |
!= | Inequality | $x != $y | Returns true if $x is not equal to $y | <!DOCTYPE html> <html> <body> <?php $x = array("a" => "red", "b" => "green"); $y = array("c" => "blue", "d" => "yellow"); var_dump($x != $y); ?> </body> </html> |
<> | Inequality | $x <> $y | Returns true if $x is not equal to $y | <!DOCTYPE html> <html> <body> <?php $x = array("a" => "red", "b" => "green"); $y = array("c" => "blue", "d" => "yellow"); var_dump($x <> $y); ?> </body> </html> |
!== | Non-identity | $x !== $y | Returns true if $x is not identical to $y | <!DOCTYPE html> <html> <body> <?php $x = array("a" => "red", "b" => "green"); $y = array("c" => "blue", "d" => "yellow"); var_dump($x !== $y); ?> </body> </html> |
PHP koşullu atama operatörleri, koşullara bağlı olarak bir değer ayarlamak için kullanılır:
Operator | Name | Example | Result | |
---|---|---|---|---|
?: | Ternary | $x = expr1 ? expr2 : expr3 | Returns the value of $x. The value of $x is expr2 if expr1 = TRUE. The value of $x is expr3 if expr1 = FALSE | <!DOCTYPE html> <html> <body> <?php // if empty($user) = TRUE, set $status = "anonymous" echo $status = (empty($user)) ? "anonymous" : "logged in"; echo("<br>"); $user = "John Doe"; // if empty($user) = FALSE, set $status = "logged in" echo $status = (empty($user)) ? "anonymous" : "logged in"; ?> </body> </html> |
?? | Null coalescing | $x = expr1 ?? expr2 | Returns the value of $x. The value of $x is expr1 if expr1 exists, and is not NULL. If expr1 does not exist, or is NULL, the value of $x is expr2. Introduced in PHP 7 | <!DOCTYPE html> <html> <body> <?php // variable $user is the value of $_GET['user'] // and 'anonymous' if it does not exist echo $user = $_GET["user"] ?? "anonymous"; echo("<br>"); // variable $color is "red" if $color does not exist or is null echo $color = $color ?? "red"; ?> </body> </html> |