[英]PHP - Placing an IF statement into a variable
I want to place an if statement into a variable in order to reduce the lines of code... for example I want to do this: 我想在变量中放置一个if语句以减少代码行...例如我想这样做:
$empty = if(empty($_POST['username']) && empty($_POST['password']))
if($empty){ // echo message;
}
elseif(!$empty){ ///echo message;
}
Can something like the above be produced? 可以生产类似上面的东西吗? Or am I making things too complex? 还是我把事情弄得太复杂了?
只需省略if:
$empty = empty($_POST['username']) && empty($_POST['password']);
You could use the ternary operator 您可以使用三元运算符
$message = ( empty($_POST['username']) && empty($_POST['password']) ) ? 'empty': 'not empty';
echo $message;
Try this 尝试这个
$empty = (empty($_POST['username']) && empty($_POST['password'])) ? 0 : 1;
if($empty){ // Not empty message here...;
}
else{ ///Empty message here....;
}
Use of ternary operator: 使用三元运算符:
echo empty($_POST['username']) && empty($_POST['password']) ? 'empty message' : 'Not empty message';
Rather than getting value of $empty
and printing it, you can directly use echo
and ternary operator. 您可以直接使用echo
和ternary运算符,而不是获取$empty
值并将其打印出来。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.