Home > Backend Development > PHP Tutorial > PHP IF ELSE simplification/use of three-dimensional one-time formula_PHP tutorial

PHP IF ELSE simplification/use of three-dimensional one-time formula_PHP tutorial

WBOY
Release: 2016-07-21 15:25:16
Original
961 people have browsed it

Generally we will write like this:

Copy code The code is as follows:

if($_GET[' time']==null)
{
$time = time();
}
else
{
$time = $_GET['time'];
}
echo $time;
//If GET has the value of time, bring in the variable time, if not, bring the current time() time
?>

If Just a simple judgment, writing as above is too troublesome, and the efficiency is not high!
You can change it to use a three-dimensional linear expression:
Copy code The code is as follows:

$time = ($_GET['time']==null) ? (time()) : ($_GET['time' ]);
echo $time;
?>

Much simpler!
Briefly explain the meaning of ternary one-time expression
If the first bracket () If the judgment sentence is true, execute the content of the first bracket () after the question mark?. If it is not true, execute the content of the second bracket () after the question mark?
Copy code The code is as follows:

$a = 5; //Define variable a=5
$b = 3; //Define variable b=5
$c = ($a==$b) ? ("yes") : ("no");
//If a=b, c=yes; a is not equal to b, c=no
?>

There is also a simplification
Copy the code The code is as follows:

$bool = true;
if($bool)
{
setValueFun();
}

can be simplified to
Copy code The code is as follows:

$bool && setValueFun();

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324135.htmlTechArticleGenerally we will write like this: Copy the code The code is as follows: ? if($_GET['time']==null ) { $time = time(); } else { $time = $_GET['time']; } echo $time; //If GET has the value of time, bring in the variable...
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