0

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!

4
  • why dont you use echo instead of opening and clogin php tags.. or set the valur of variable in loop first and than display the value of variable Commented Oct 16, 2015 at 1:12
  • Are you calling a variable or a function? Commented Oct 16, 2015 at 1:15
  • yeah, i had few divs and didn't want to change all. Commented Oct 16, 2015 at 1:15
  • and i am calling a variable, like a image source (So, it will like numbers) Commented Oct 16, 2015 at 1:15

2 Answers 2

2

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 ?> 
Sign up to request clarification or add additional context in comments.

1 Comment

Make sense! =) Thanks
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.