在 PHP 三元情况下设置变量值
Setting variable value on PHP ternary case
我正在尝试使用三元输出在后一种情况下做两件事。
我遇到的问题是在增加错误计数后,在后一种情况下为变量设置一个文本值。
我已经尝试了一些方法,这里有两次尝试,但都无法设置 $errors_log
值。
问。如何在三元输出中设置变量值。
$errors_v=0;
if (validate_username() == false ? null : $errors_v++ && $errors_log='username invalid');
if ($errors_v != 0) {
echo $errors_log;
}
function validate_username() {
return true;
}
$errors_v=0;
$errors_log[];
if (validate_username() == false ? null : $errors_v++ && $errors_log[]='username invalid');
if ($errors_v != 0) {
var_dump($errors_log);
}
function validate_username() {
return true;
}
您不能在三元情况下执行两个操作。您可以只检查 $errors_log
是否为 empty
以及它是否在 if
语句中递增 $errors_v
,如下所示:
if (validate_username() == false ? null : $errors_log='username invalid');
if (!empty($errors_log)) {
$errors_v++;
echo $errors_log;
}
function validate_username() {
return true;
}
我会像下面那样做我的三进制,然后检查 $errors_log
是否不为空,如果不是,打印出错误。
$errors_log[] = validate_username() === false ? null : 'username invalid';
if (!empty($errors_log)) {
foreach($errors_log as $error) {
echo $error;
}
}
function validate_username() {
return true;
}
如果它还需要有一个计数器,尽管我真的建议您改用 $errors_log
数组,您可以这样做:
if (!validate_username()) {
$errors_log[] = 'username invalid';
$errors_v++;
}
您正在混合使用 "longhand" 和 "shorthand" 条件语法。
您含糊不清的函数名称及其 return 值是 confusing/unintuitive。我建议重命名您的函数或反转布尔值 returns.
始终努力使用 DRY and DAMP 编码实践。
代码后半部分的方法看起来比第一部分好。
如果要生成错误数组,请不要为递增计数器而烦恼,只需在以下时间对数组进行计数即可你愿意。
我认为没有必要用 shorthand 条件来修饰你的代码。
代码:(Demo)
function bad_username(){ // new meaningful function name
return true;
}
$errors_log=[]; // declare the variable as an array
if(bad_username()){
$errors_log[]='username invalid'; // push the value
}
if(sizeof($errors_log)){ // count elements in array, if 1 or more display them
var_export($errors_log);
}else{
echo "No Error";
}
输出:
array (
0 => 'username invalid',
)
我正在尝试使用三元输出在后一种情况下做两件事。
我遇到的问题是在增加错误计数后,在后一种情况下为变量设置一个文本值。
我已经尝试了一些方法,这里有两次尝试,但都无法设置 $errors_log
值。
问。如何在三元输出中设置变量值。
$errors_v=0;
if (validate_username() == false ? null : $errors_v++ && $errors_log='username invalid');
if ($errors_v != 0) {
echo $errors_log;
}
function validate_username() {
return true;
}
$errors_v=0;
$errors_log[];
if (validate_username() == false ? null : $errors_v++ && $errors_log[]='username invalid');
if ($errors_v != 0) {
var_dump($errors_log);
}
function validate_username() {
return true;
}
您不能在三元情况下执行两个操作。您可以只检查 $errors_log
是否为 empty
以及它是否在 if
语句中递增 $errors_v
,如下所示:
if (validate_username() == false ? null : $errors_log='username invalid');
if (!empty($errors_log)) {
$errors_v++;
echo $errors_log;
}
function validate_username() {
return true;
}
我会像下面那样做我的三进制,然后检查 $errors_log
是否不为空,如果不是,打印出错误。
$errors_log[] = validate_username() === false ? null : 'username invalid';
if (!empty($errors_log)) {
foreach($errors_log as $error) {
echo $error;
}
}
function validate_username() {
return true;
}
如果它还需要有一个计数器,尽管我真的建议您改用 $errors_log
数组,您可以这样做:
if (!validate_username()) {
$errors_log[] = 'username invalid';
$errors_v++;
}
您正在混合使用 "longhand" 和 "shorthand" 条件语法。
您含糊不清的函数名称及其 return 值是 confusing/unintuitive。我建议重命名您的函数或反转布尔值 returns.
始终努力使用 DRY and DAMP 编码实践。
代码后半部分的方法看起来比第一部分好。
如果要生成错误数组,请不要为递增计数器而烦恼,只需在以下时间对数组进行计数即可你愿意。
我认为没有必要用 shorthand 条件来修饰你的代码。
代码:(Demo)
function bad_username(){ // new meaningful function name
return true;
}
$errors_log=[]; // declare the variable as an array
if(bad_username()){
$errors_log[]='username invalid'; // push the value
}
if(sizeof($errors_log)){ // count elements in array, if 1 or more display them
var_export($errors_log);
}else{
echo "No Error";
}
输出:
array (
0 => 'username invalid',
)