Setting variable value on PHP ternary case -
i trying use ternary output 2 things on latter case.
the issue have setting text value variable in latter case, after incrementing error count.
i have tried few things, here's 2 attempts, these both fail on setting $errors_log value.
q. how can set variable value within output of ternary.
$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; }
i ternary below, , check if $errors_log not empty , if it's not, print out errors.
$errors_log[] = validate_username() === false ? null : 'username invalid'; if (!empty($errors_log)) { foreach($errors_log $error) { echo $error; } } function validate_username() { return true; } if it's needed have counter aswell, though recommend count on $errors_log array instead, this:
if (!validate_username()) { $errors_log[] = 'username invalid'; $errors_v++; }
Comments
Post a Comment