Not empty is not working with object in php 8.1

Not empty is not working with object
my object have value but code is not consider
if(!empty($data->title)){
echo “Yes”;
}else{
echo “No”;
}

result is “No” why?
suggest me what library is need to be install.

Hey @Soft_Solution,

it 's a bit difficult to answer since we know nothing about $data. Obviously $data should be an object in your code. Can you dump the content of $data so that we can see what type it is and which properties it got?

Just use the following code or copy the output from XDebug if your IDE supports it.

echo "<pre>";
var_dump($data);
echo "</pre>";

I’m already do this but this is not working.

This is not working

This is not a meaningful description of what is happening or what exactly is not working. What exactly is not working?

Here are a few examples how empty() works out with different values.

$data = stdClass;
$data->title = 'bla';

// is true because the title property is not empty
assert(empty($data->title) === false);

$data->title = null;

// true since the title property is null
assert(empty($data->title) === true);

$data->title = '';

// true since the title property is an empty string
assert(empty($data->title) === true);

$data->title = false;

// true because the title property is false
assert(empty($data->title) === true)

Please answer the following questions:

  • How is your code looking?
  • What type of is your $data object?
3 Likes

Thanks this is working