Home  >  Article  >  Backend Development  >  求教一个PHP赋值判断的有关问题

求教一个PHP赋值判断的有关问题

WBOY
WBOYOriginal
2016-06-13 10:16:13802browse

求教一个PHP赋值判断的问题
刚刚接触PHP,在一个MYSQL的类当中看到这么一句话
$condition = $condition ? 'where' .$condition :NULL
不是太明白什么意思,请教大家能否解释一下。
我个人是把他理解成:如果'where'.$condition为空,则把'where'.$condition赋值给$condition,反之,则把$condition=$condition,不懂是对还是错,还望大神前来指教!!

------解决方案--------------------
靠!楼上错了!

如果'where'.$condition为空,则把'where'.$condition赋值给$condition,反之,则把$condition=$condition

=>

如果$condition不为空,则把'where'.$condition赋值给$condition,反之,则把$condition=NULL
------解决方案--------------------
这是拼合一个SQL语句。
例如 如果用户没有设置匹配条件,那么就是
SELECT * FROM tablename;
如果有设置条件,即$condition不为空的话
SELECT * FROM tablename WHERE $condition;
------解决方案--------------------
$a ? $b : $c 
php 的三元运算符。
如果$a 为 true 表达式的值为$b,如果为 false 则表达式值为$c.
------解决方案--------------------

PHP code
$condition = $condition ? 'where' .$condition :NULL//等同于if($condition){  $condition = 'where' .$condition;}else{  $condition = NULL;}<div class="clear">
                 
              
              
        
            </div>
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