Home  >  Article  >  Backend Development  >  How to use PHP basic operators_PHP tutorial

How to use PHP basic operators_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:11:24815browse

1. Arithmetic operators: +, -, *, /, %.

2. Increment/decrement operators: such as $a++,$a--,++$a,--$a.

For example:

$a=10;
$b=5;
$c=$a++; //Assign value first, then increment. $c=$a,$a=$a+1
$d=$b--; //Assign value first, then decrement. $d=$b,$b=$a-1
echo '$a='.$a."||".'$c='.$c.'
'; //$a=11,$c=10
echo '$b='.$b."||".'$d='.$d.'
'; / /$b=4,$d=5
?>

$a=10;
$b=5;
$c=++$a; //Increment first, then assign value. $a=$a+1, $c=$a
$d=--$b; //Decrease first, then assign value. $b=$a-1, $d=$b
echo '$a='.$a."||".'$c='.$c.'
'; //$a=11,$c=11
echo '$b='.$b."||".'$d='.$d.'
'; / /$b=4,$d=4
?>

3. Comparison operator: reference document

4. Logical operators:

For example:

$a=10;$b=7;
if($a++>8 || $b++>7){ //$a++>8 is true , $b++>7 will not be executed
echo 'OK!';
}
echo 'a='.$a.'b='.$b; // Output OK, a=11,b=7

Change it

$a=10;$b=7;
if($a++>10 && $b++>7){ //$a++ >8 is false, $b++>7 This will not be executed
echo 'OK!';
}
echo 'a='.$a.'b=' .$b; // a=11,b=7

Details: and && both represent logical AND, what is their difference?

Mainly reflected in the priority

and the priority of
and< = <&&
or < = < ||
For example:

$a=false || true; //&& > = > and ;Compare false || true first, then assign value
$b=false or true; //|| > = > or ;Assign $b=false first and then compare, so the result is false
var_dump($a,$b); //bool(true) bool( false)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326905.htmlTechArticle1. Arithmetic operators: +, -, *, /, %. 2. Increment/decrement operators: such as $a++,$a--,++$a,--$a. Such as: ?php $a=10; $b=5; $c=$a++; //First Assigned a value, it will be incremented later. $c=$a,$a=$a+1 $d=$b--;...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn