0

I am trying to use ternary output to do 2 things on the latter case.

The issue I have is setting a text value to the variable in the latter case, after incrementing the error count.

I have tried a few things, here's two attempts, but these both fail on setting the $errors_log value.

Q. How can i set a variable value within an 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;
}
skolind
  • 1,724
  • 6
  • 28
  • 51
Derple
  • 865
  • 11
  • 28
  • 2
    Your code must be __readable and understandable__ Currently it is not. – u_mulder Aug 15 '17 at 12:17
  • Care to elaborate? @u_mulder – Derple Aug 15 '17 at 12:20
  • 2
    @u_mulder is right. This is readable: `if (!validate_username()) { ... }`. Your code is harder to read, which requires extra time looking at it. Stick to these two rules, and your team mates will respect you: DRY KISS = Don't Repeat Yourself, Keep It Simple & Stupid. – Andrejs Cainikovs Aug 15 '17 at 12:29
  • @AndrejsCainikovs ha! i feel styoopid. Thanks. making it more difficult than it needed to be. i went with `if (validate_username()){ $v++; $errors_log='username invalid';}` and works as expected. regards >. – Derple Aug 15 '17 at 12:36

3 Answers3

1

I would do my ternary like below, and then check if $errors_log is not empty and if it's not, print out the errors.

$errors_log[] = validate_username() === false ? null : 'username invalid';

if (!empty($errors_log)) {
    foreach($errors_log as $error) {
        echo $error;
    }
}

function validate_username() {
    return true;
}

If it's needed to have a counter aswell, even though I really recommend you count on the $errors_log array instead, you could do something like this:

if (!validate_username()) {
    $errors_log[] = 'username invalid';
    $errors_v++;
}
skolind
  • 1,724
  • 6
  • 28
  • 51
  • @mickmackusa Hm, might be, it seemed to work fine though. Maybe I've coded too much JS, where this is possible. Ty! :) – skolind Aug 15 '17 at 13:28
  • My point is, because you are storing a null element in your shorthand conditional, foreach() will echo that null value with no visible impact on the page. – mickmackusa Aug 15 '17 at 13:36
  • @mickmackusa True, but OP asked for a ternary according to his code. – skolind Aug 16 '17 at 07:54
1

You are mixing "longhand" and "shorthand" conditional syntax.

Your ambiguous function name coupled with its return value is confusing/unintuitive. I recommend renaming your function or reversing the boolean it returns.

Always endeavor to use DRY and DAMP coding practices.

The approach in the second half of your code looks better than the first.
If you are going to generate an array of errors, don't bother with incrementing a counter, just count the array when you wish.

I don't see any need to fancy up your code with shorthand conditionals.

Code: (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";
}

Output:

array (
  0 => 'username invalid',
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
0

You can't perform two operations in a ternary case. You could just check if $errors_log is empty and if it is increment $errors_v inside the if statement like this:

if (validate_username() == false ? null : $errors_log='username invalid');

if (!empty($errors_log)) {
   $errors_v++;
   echo $errors_log;
}

function validate_username() {
    return true;
}
DrRoach
  • 1,320
  • 9
  • 16
  • its the '$errors_log='username invalid')' that seems not to be working – Derple Aug 15 '17 at 12:21
  • Copied and pasted my code into http://writephponline.com and output is `username invalid`. Are you getting any errors? – DrRoach Aug 15 '17 at 12:23
  • i require two things to happen on the latter case. set the variable value, but also increment the error count. i confirm it works without the count, but this then breaks other parts of requirement as the count is needed. – Derple Aug 15 '17 at 12:30
  • 1
    @Derple You can't set multiple values in a ternary. Then you should make a normal `if...` statement. But you shouldn't really make the count - you should just "count" on the `$errors_log` array. – skolind Aug 15 '17 at 12:31
  • yeh, i saw that, i been thinking too inside the box. i found also using `if (validate_username()){ $v++; $errors_log='username invalid';}` works great too. thanks for the help >. – Derple Aug 15 '17 at 12:35
  • 1
    @Derple You need a `!` if you want it to make errors on a failed check :) `if(!validate_username()) ...` – skolind Aug 15 '17 at 12:36
  • 1
    Yeah it does, just choose whichever you prefer. There's never just one right answer. – DrRoach Aug 15 '17 at 12:36