Home  >  Article  >  Backend Development  >  Basic tutorial for getting started with php - php operators

Basic tutorial for getting started with php - php operators

WBOY
WBOYOriginal
2016-07-25 09:00:17990browse
This section introduces PHP operators to everyone, and it is recommended that everyone has a firm grasp of them.

1. Arithmetic operators: +, -, *, /, %. 2. Increment/decrement operators: such as $a++,$a--,++$a,--$a. Example 1,

<?php
$a=10;
$b=5;
$c=$a++; //先赋值,后自增。$c=$a,$a=$a+1
$d=$b--; //先赋值,后自减。$d=$b,$b=$a-1
echo '$a='.$a."||".'$c='.$c.'<br/>'; //$a=11,$c=10
echo '$b='.$b."||".'$d='.$d.'<br/>'; //$b=4,$d=5
//by bbs.it-home.org
?>

Example 2,

<?php
$a=10;
$b=5;
$c=++$a; //先自增,后赋值。$a=$a+1,$c=$a
$d=--$b; //先自减,后赋值。$b=$a-1, $d=$b
echo '$a='.$a."||".'$c='.$c.'<br/>'; //$a=11,$c=11
echo '$b='.$b."||".'$d='.$d.'<br/>'; //$b=4,$d=4
//by bbs.it-home.org
?>

3. Comparison operator: php comparison operator 4. Logical operators: php 比较运算符 Example 3,

<?php
$a=10;$b=7;
if($a++>8 || $b++>7){ //$a++>8为真,$b++>7这个就不执行了
echo 'OK!';
}
echo 'a='.$a.'b='.$b; // 输出OK,a=11,b=7

//改变一下

$a=10;$b=7;
if($a++>10 && $b++>7){ //$a++>8为假,$b++>7这个就不执行了
echo 'OK!';
}
echo 'a='.$a.'b='.$b; // a=11,b=7
?>

Details: and && both represent logical AND, the difference is mainly in the priority: and priority and

<?php
$a=false || true; //&& > = > and ;先比较false || true,再赋值
$b=false or true; //|| > = > or  ;先赋值$b=false,再比较,所以结果是false
var_dump($a,$b); //bool(true) bool(false)
?>

Thank you for paying attention to the PHP introductory tutorials. This series of PHP basic tutorials will help PHP newbies quickly master the PHP programming language. Programmer's Home will continue to launch PHP-related tutorials for everyone, and I wish you all the best in your learning and progress!



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