当php数组不是时,它被视为null

I have passed an array called $items to my view in order to print it. Take a look:

<?php echo "<br>First print: " . $items;    

if ($items != null): 
    echo "<br>Second print: " . $items; 
php endif;?>

The final output is:

First print: Array

As you can see, $items is not null, however, the second message is never printed. Is there something wrong with my server?

Edit:

I can evaluate $items with empty function, but I need to evalute it with null comparator because this variable can be null.

Edit 2:

The $items can take be take the following values when is passed to the view:

  • null: $items = null;
  • array: $items = array(); or $items = array('a', 'b');

If you want to compare with null it is better using !== instead !=

This way you tell PHP to use strict comparision and both sides should be identical (type and value)

Else you can get wrong results if the array is empty (for example)

More for php comparisions here

If PHP array has no values in it, i.e count($array) is 0, it gives true when compared with null.

Make sure you have atleast one value in your array.