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

Basic tutorial for getting started with php - php operators

WBOY
Release: 2016-07-25 09:00:17
Original
1026 people have browsed it
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
?>
Copy after login

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
?>
Copy after login

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
?>
Copy after login

Details: and && both represent logical AND, the difference is mainly in the priority: and priority and< = <&& or < = < || Example 4, < = <&& or < = < || 例4,

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

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!



source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template