php operatorLOGIN

php operator

PHP Operators

In this chapter we will discuss the application of different operators in PHP.

In PHP, the assignment operator = is used to assign values ​​to variables.

In PHP, the arithmetic operator + is used to add values ​​together.

PHP Arithmetic Operators

QQ截图20161008141737.png

The following examples demonstrate different results obtained by using different arithmetic operators:

Example

<?php  $x=10;  $y=6; echo ($x + $y); // 输出16 echo ($x - $y); // 输出4 echo ($x * $y); // 输出60 echo ($x / $y); // 输出1.6666666666667  echo ($x % $y); // 输出4  ?>
PHP7+ 版本新增整除运算符 intdiv(),使用实例:
<?php
var_dump(intdiv(10, 3));
?>

The above example will output:

int(3)

PHP assignment operator

In PHP, The basic assignment operator is "=". It means that the left operand is set to the value of the right-hand expression. That is, the value of "$x = 5" is 5.


QQ截图20161008141819.png

The following examples demonstrate different results obtained by using different assignment operators:

Examples

<?php 
 $x=10; 
 echo $x; // 输出10
 
 $y=20; 
 $y += 100;
 echo $y; // 输出120
 
 $z=50;
 $z -= 25;
 echo $z; // 输出25
 
 $i=5;
 $i *= 6;
 echo $i; // 输出30
 
 $j=10;
 $j /= 5;
 echo $j; // 输出2
 
 $k=15;
 $k %= 4;
 echo $k; // 输出3
 ?>

The following examples demonstrate different results obtained by using different string operators:

Examples

<?php
 $a = "Hello";
 $b = $a . " world!";
 echo $b; // 输出Hello world! 
 
 $x="Hello";
 $x .= " world!";
 echo $x; // 输出Hello world! 
 ?>

QQ截图20161008141846.png

The following examples demonstrate the use of increment/decrement operators Result obtained:

Example

<?php
 $x=10; 
 echo ++$x; // 输出11
 
 $y=10; 
 echo $y++; // 输出10
 
 $z=5;
 echo --$z; // 输出4
 
 $i=5;
 echo $i--; // 输出5
 ?>

PHP Comparison Operators

Comparison operators allow you to compare two values:

QQ截图20161008141904.png

The following examples demonstrate different results obtained using some comparison operators:

Examples

<?php
 $x=100; 
 $y="100";
 
 var_dump($x == $y);
 echo "<br>";
 var_dump($x === $y);
 echo "<br>";
 var_dump($x != $y);
 echo "<br>";
 var_dump($x !== $y);
 echo "<br>";
 
 $a=50;
 $b=90;
 
 var_dump($a > $b);
 echo "<br>";
 var_dump($a < $b);
 ?>

PHP Logical Operators

QQ截图20161008141927.png

QQ截图20161008141956.png

QQ截图20161008142019.png

The following examples demonstrate different results obtained using some array operators:

Example

<?php
 $x = array("a" => "red", "b" => "green"); 
 $y = array("c" => "blue", "d" => "yellow"); 
 $z = $x + $y; // $x 和 $y 数组合并
 var_dump($z);
 var_dump($x == $y);
 var_dump($x === $y);
 var_dump($x != $y);
 var_dump($x <> $y);
 var_dump($x !== $y);
 ?>

Ternary operator

Another conditional operator is the "?:" (or ternary) operator.

Syntax format

(expr1) ? (expr2) : (expr3)

When expr1 evaluates to TRUE, the value is expr2, when expr1 evaluates to FALSE The value is expr3.

Since PHP 5.3, the middle part of the ternary operator can be omitted. The expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE and expr3 otherwise.

Example

In the following example, it is judged that the $_GET request contains the user value. If so, $_GET['user'] is returned, otherwise nobody is returned:

<?php
$test = 'php中文网';
// 普通写法
$username = isset($test) ? $test : 'nobody';
echo $username, PHP_EOL;
 
// PHP 5.3+ 版本写法
$username = $test ?: 'nobody';
echo $username, PHP_EOL;
?>

php Chinese Net

Note: PHP_EOL is a newline character and is compatible with larger platforms.

In the PHP7+ version, there is an additional NULL merge operator. The example is as follows:

<?php

// If $_GET['user'] does not exist, 'nobody' is returned. Otherwise, return the value of $_GET['user']

$username = $_GET['user'] ?? 'nobody';

// Similar ternary operator

$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';

?>

Combined comparison operator (PHP7+)

PHP7+ supports combined comparison operators. Examples are as follows:

<?php
// 整型
echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1
 
// 浮点型
echo 1.5 <=> 1.5; // 0
echo 1.5 <=> 2.5; // -1
echo 2.5 <=> 1.5; // 1
 
// 字符串
echo "a" <=> "a"; // 0
echo "a" <=> "b"; // -1
echo "b" <=> "a"; // 1
?>

PHP’s operator symbols

Operator Operator Operator

1                                                                                                                                                                        . ##Ternary operator 1 ? : 3

Arithmetic operation symbols + - * / % ++ --

Assignment operation symbols - += -= *= /= %=

Comparison operator symbols > < == >= <= === != !==

Logical operation symbols && || !

Bit operators & | ^ ~ >> <<

Other primitive operators ? : @ -> =>

PHP’s arithmetic operator

+ The + sign in php only performs operations

- Nothing special / The divisor cannot be 0

% (modulo) Decimals are not moduloed. PHP automatically converts decimals into integers for modulo. Negative numbers determine the first number

+ + (Auto-increment)

$a=5

$a++ Use it first and then add

++$a Add it first and then use it

<?php
     $a=5;
     //5         7
     $b=$a++  +  ++$a;
     echo $b;
     echo '<br>';
     echo $a;
 ?>

-- (Self-decreasing)

$a=5

$a-- Use it first and then add it

--$a Add it first and then use it

<?php
     $a=5;
    
     $b=$a--  -  --$a; 
     echo $b;   //2   echo '<br>';
     echo $a;//3>

PHP’s assignment operator

The basic assignment operator is "=". At first you may think it is "equal to", but it is not. It actually means assigning the value of the expression on the right to the operand on the left.

The value of the assignment operation expression is the assigned value. That is, the value of "$a = 3" is 3. This allows you to do some tricks: <?php

$a = ($b = 4) + 5; // $a now becomes 9, and $b becomes 4.

PHP comparison operators


1. The result of comparison is a boolean value, which is used in statements such as if and while

4>3 true==

===Congruent, not only the content must be the same, but the types of the two operands must also be equal.

<?PHP
         $a = 7.00;
         $b= 7;
    var_dump($a===$b);
 
?>

can see Make a difference.

<?PHP
         $a = 7.00;
         $b= 7;
    var_dump($a==$b);
 
?>

PHP’s logical operator

Features: Short circuit

&& and

|| or

Example:$a = 1; After that, other expressions will no longer be executed, so in this statement it has been judged that the return value of $a is "true", and the assignment to $c will no longer be executed. This will form a short circuit phenomenon, so the subsequent output statements will not Output content.

$a==2 && $c=100; //Logical AND operator (&&). The logical AND operation method is that the expressions on both sides of the operator are "true" before the execution of other operations continues. statement directly returns a "false" value, so this statement will not execute the assignment of $c.       

echo 'The value of $c:'.$c;


PHP’s ternary operator

Operator? Operator: Operator

condition? Statement: Statement

Condition? Expression: Expression

一! Two: three

If the condition of "one" is true, point to "two", otherwise execute "three"

$a =8;

$sun = false ? $a +5+6:"00000000";

echo $sun;

Next Section
<?php $x=10; echo $x; // 输出10 $y=20; $y += 100; echo $y; // 输出120 $z=50; $z -= 25; echo $z; // 输出25 $i=5; $i *= 6; echo $i; // 输出30 $j=10; $j /= 5; echo $j; // 输出2 $k=15; $k %= 4; echo $k; // 输出3 ?>
submitReset Code
ChapterCourseware