Zor Çocuk

Moderatör
Ticaret - 100%
Katılım
3 Yıl 9 Ay 1 Gün
Mesajlar
881
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:

  • 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
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.

OperatorNameExampleResult
+Addition$x + $ySum of $x and $y<!DOCTYPE html>
<html>
<body>

<?php
$x = 10;
$y = 6;

echo $x + $y;
?>

</body>

</html>

-Subtraction$x - $yDifference of $x and $y<!DOCTYPE html>
<html>
<body>

<?php
$x = 10;
$y = 6;

echo $x - $y;
?>

</body>

</html>
*Multiplication$x * $yProduct of $x and $y<!DOCTYPE html>
<html>
<body>

<?php
$x = 10;
$y = 6;

echo $x * $y;
?>

</body>
</html>

/Division$x / $yQuotient of $x and $y<!DOCTYPE html>
<html>
<body>

<?php
$x = 10;
$y = 6;

echo $x / $y;
?>

</body>

</html>

%Modulus$x % $yRemainder of $x divided by $y<!DOCTYPE html>
<html>
<body>

<?php
$x = 10;
$y = 6;

echo $x % $y;
?>

</body>

</html>

**Exponentiation$x ** $yResult 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
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.

AssignmentSame as...Description
x = yx = yThe 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 += yx = x + yAddition<!DOCTYPE html>
<html>
<body>

<?php
$x = 20;
$x += 100;

echo $x;
?>

</body>

</html>

x -= yx = x - ySubtraction<!DOCTYPE html>
<html>
<body>

<?php
$x = 50;
$x -= 30;

echo $x;
?>

</body>

</html>

x *= yx = x * yMultiplication<!DOCTYPE html>
<html>
<body>

<?php
$x = 10;
$y = 6;

echo $x * $y;
?>

</body>

</html>

x /= yx = x / yDivision<!DOCTYPE html>
<html>
<body>

<?php
$x = 10;
$x /= 5;

echo $x;
?>

</body>

</html>

x %= yx = x % yModulus<!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:

OperatorNameExampleResult
==Equal$x == $yReturns 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 === $yReturns 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 != $yReturns 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 <> $yReturns 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 !== $yReturns 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 > $yReturns 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 < $yReturns 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 >= $yReturns 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 <= $yReturns 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 <=> $yReturns 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 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.

OperatorNameDescription
++$xPre-incrementIncrements $x by one, then returns $x<!DOCTYPE html>
<html>
<body>

<?php
$x = 10;
echo ++$x;
?>

</body>

</html>

$x++Post-incrementReturns $x, then increments $x by one<!DOCTYPE html>
<html>
<body>

<?php
$x = 10;
echo $x++;
?>

</body>

</html>

--$xPre-decrementDecrements $x by one, then returns $x<!DOCTYPE html>
<html>
<body>

<?php
$x = 10;
echo --$x;
?>

</body>

</html>

$x--Post-decrementReturns $x, then decrements $x by one<!DOCTYPE html>
<html>
<body>

<?php
$x = 10;
echo $x--;
?>

</body>

</html>

PHP Mantıksal Operatörleri
PHP mantıksal operatörleri koşullu ifadeleri birleştirmek için kullanılır.

OperatorNameExampleResult
andAnd$x and $yTrue 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>

orOr$x or $yTrue 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>

xorXor$x xor $yTrue 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 && $yTrue 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 || $yTrue 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!$xTrue if $x is not true<!DOCTYPE html>
<html>
<body>

<?php
$x = 100;

if ($x !== 90) {
echo "Hello world!";
}
?>

</body>

</html>

PHP String Operatörleri
PHP'nin dizeler için özel olarak tasarlanmış iki operatörü vardır.

OperatorNameExampleResult
.Concatenation$txt1 . $txt2Concatenation of $txt1 and $txt2<!DOCTYPE html>
<html>
<body>

<?php
$txt1 = "Hello";
$txt2 = " world!";
echo $txt1 . $txt2;
?>

</body>

</html>

.=Concatenation assignment$txt1 .= $txt2Appends $txt2 to $txt1<!DOCTYPE html>
<html>
<body>

<?php
$txt1 = "Hello";
$txt2 = " world!";
$txt1 .= $txt2;
echo $txt1;
?>

</body>

</html>

PHP Dizisi Operatörleri
PHP dizi operatörleri dizileri karşılaştırmak için kullanılır.

OperatorNameExampleResult
+Union$x + $yUnion 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 == $yReturns 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 === $yReturns 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 != $yReturns 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 <> $yReturns 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 !== $yReturns 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
PHP koşullu atama operatörleri, koşullara bağlı olarak bir değer ayarlamak için kullanılır:

OperatorNameExampleResult
?:Ternary$x = expr1 ? expr2 : expr3Returns 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 ?? expr2Returns 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>

 

Bu konuyu görüntüleyen kullanıcılar

Üst Alt