arrays and if statements and accessing each index position

Viewed 62

I am not sure I understand if statements and how they access arrays as well as I would like. I have this array:

If I var_dump($count) I get:

array(1) { [0]=> object(stdClass)#2472 (1) { ["nmbr_tables"]=> string(1) "4" } } array(1) { [0]=> object(stdClass)#2445 (1) { ["nmbr_tables"]=> string(1) "0" } } array(1) { [0]=> object(stdClass)#2452 (1) { ["nmbr_tables"]=> string(1) "0" } }

So I write

if($count >= 1){echo "hello";} else {echo "no";}; 

my expected output would be:

hello
no
no

Since the first index position has the nmbr "4" and then the next two have the nmbr "0"

Instead what I am getting is:

hellohellohello

Seeming to indicate that it is only paying attention to the first index position.

So then I thought maybe:

   foreach($count  as $val){
   if($count >= 2){echo "hello";} else {echo "no";};
   };

No difference.

If I var_export($count) I get:

array ( 0 => (object) array( 'nmbr' => '4', ), )array ( 0 => (object) array( 'nmbr' => '0', ), )array ( 0 => (object) array( 'nmbr' => '0', ), )
2 Answers

Quick Solution

It looks as though your input array $count is in the format:

$count = [ (object) [ 'nmbr' => '0', ] ];

So your conditional should be formatted as:

echo ($count[0]->nmbr >= 1) ? "hello" : "no", PHP_EOL;

// You want the first [0] item in the subarray of `$count` and you then want to access the property "nmbr"

How it works

Ternary Logic

The echo in the above statement uses a shorthand if statement with the ternary operator. The general form of which is:

CONDITION ? TRUE : FALSE;

// You can use parentheses or not...
//   Whatever makes it easiest to read and understand;
//   all of these (and the one above) are the same

( CONDITION ) ? ( TRUE ) : ( FALSE );
( CONDITION ) ? ( TRUE ) : FALSE;
( CONDITION ) ? TRUE : FALSE;

and is equivalent to:

if(CONDITION){
    //Do something if TRUE
}
else{
   //Do something if FALSE
}

In this the ternary logic is evaluated and the result is returned to the echo which then outputs the string...

echo $count[0]->nmbr >= 1 ? "hello" : "no";

Note for each iteration of your larger loop $count looks like

$count = [ (object) [ 'nmbr' => '0', ] ];

// Therefore...

$count[0] == (object) [ 'nmbr' => '0', ];

// and finally...

$count[0]->nmbr == 0;

Running code on true

There are a few ways you can do this:

  1. Use a function as the true/false condition
    • Note: you can't use echo inside a ternary
  2. Change back to a standard if

So, for example you could do either of the below:

if($count[0]->nmbr >= 1){
    // Code to carry out if true
    echo "This is true!";
}
else{
    // Code to carry out if false
    echo "false logic :(";
}

OR...

function countIsMoreThanOne() : void
{
    // Code to carry out if true
    echo "This is true!";
}

$count[0]->nmbr >= 1 ? countIsMoreThanOne() : "false logic :(";

OR...

$count[0]->nmbr >= 1 ? print("This is true!") : "false logic :(";

Try this code:

foreach($count as $val){

    if($val >= 2){
        echo "hello";
    }
    else{
        echo "no";
    }

}
Related