Steve Kim
Steve Kim

Reputation: 5591

Php if empty variable

So, I have following variables:

<?php
$multi_images = image($image, $rh_post_id);
?>

Then, I want to show something like this only if the variable is not empty. I am not sure what the best way to do it.

<?php if ( empty($multi_images())) { ?>                         
    no images
<?php }else{ ?>
    there is an image
<?php } ?>  

Is this correct?

Thanks!

Upvotes: 0

Views: 48

Answers (2)

user2182349
user2182349

Reputation: 9782

I'd code it like so, with the assumption that $multi_images is an array of images.

<?php if ( isset($multi_images) && is_array($multi_images) && count($multi_images) > 0) : ?>                         
    no images
<?php else : ?>
    there is an image
<?php endif ?> 

Upvotes: 2

Daryl Gill
Daryl Gill

Reputation: 5524

Are you calling a variable or a function?

 $multi_images; 

or

multi_images();

Each one is different. If you want to validate a response from a function you'll use:

if (empty(image($image, $rh_post_id))){  ... }

on a variable:

if (empty($image)){ ... }

to check that the response is not empty. You'll want to use the exclamation mark ! so it'll be:

if (!empty($var)){ .. }

which will translate to

if variable is not empty

Upvotes: 1

Related Questions