如果这回应这个 - 如果变量没有回声什么

I have added a script to my website which shows a message when the product filter does not show up any results. The code I've included is this:

<?php
if(empty($htmlFilter->rows)){
  echo '<p>Sorry, no results found - try a different search selection</p>';
}
?>

This works, but the problem I'm having is that the message also shows up on the pages where the filter is not existent. I need to write a condition for when the filter does not exists on the page.

Can anyone help please?

Quick update: When I add var_dump($htmlFilter) it come sup with NULL

Is the variable not right?

Normally you would expect an undefined variable error, but it silently just evaluates to true even though the variable is not set, which is a bad thing because not everyone is aware of this.

As a workaround you can additionally check if the variable is set before checking if it's value is empty.

if (isset($htmlFilter) && empty($htmlFilter->rows)) {
    echo '<p>Sorry, no results found - try a different search selection</p>';
}

You could use the isset function like this if I got what you wrote right.

<?php
if(empty($htmlFilter->rows) && isset($htmlFilter)){
  echo '<p>Sorry, no results found - try a different search selection</p>';
}
?>

EDIT:

This code:

<?php
echo is_null($algo);
echo '<br>';
echo isset($algo);
echo '<br>';
echo empty($algo);
?>

returns this:

1

1

As you can see, the variable $algo is not initialized nor declared.

Could you try...

<?php
if( isset($htmlFilter->rows) && empty($htmlFilter->rows) ) {
  echo '<p>Sorry, no results found - try a different search selection</p>';
}
?>

If $htmlFilter->rows is supposed to be an array, you can do this:

<?php
if(isset($htmlFilter->rows) && count($htmlFilter->rows) === 0){
  echo '<p>Sorry, no results found - try a different search selection</p>';
}
?>

I've finally fixed this - I used the following code:

if(empty($this->rows) && !empty($htmlFilter)){
echo '<p>Sorry, no results found - try a different search selection</p>'

It was also not put in the correct line :-/

All sorted now though - thanks for your help guys! :-)