2

I want to echo variables if either of them aren't empty.

I have written the following PHP but they aren't being shown when the variables aren't empty:

if(!empty($this->element->videos||$this->element->productdownloads)) {
    echo $this->element->videos; 
    echo $this->element->productdownloads; 
}
5
  • Just chain the conditions? !empty($this->element->videos) || !empty($this->element->productdownloads) Commented Feb 2, 2017 at 17:10
  • From the docs "Prior to PHP 5.5, empty() only supports variables; anything else will result in a parse error." You have a test inside the function and error reporting (perhaps the error logs) would show you this. Commented Feb 2, 2017 at 17:12
  • Do you want to echo both variables? Or just either one? Commented Feb 2, 2017 at 17:14
  • The order of precedence says that it does $this->element->videos || $this->element->productdownloads first. This evaluates to a boolean value no matter what those variables actually contain. If one of them has some value, this will probably evaluate to true. And doing !empty() on a boolean true will return true. But as Jay Blanchard pointed out, empty() only works on variables so you probably have an error. Commented Feb 2, 2017 at 17:19
  • (cont.) You probably meant to do !empty() on each variable separately. So if(!empty($this->element->videos) || !empty($this->element->productdownloads)). But that will try to echo both values even if just one of them has something. So you probably want to use AND && instead of OR || Commented Feb 2, 2017 at 17:21

5 Answers 5

8

When checking with the OR operator (||) the code will execute if one or none of them is empty. But you echo both variables even if one of them is empty.

What I think you want to do is use the AND operator(&&). This way, the code will only execute if none of the variables are empty.

<?php   if(!empty($this->element->videos) && !empty($this->element->productdownloads)) {
    echo $this->element->videos; 
    echo $this->element->productdownloads; }
?>

if you still want to show videos, even if productdownloads is empty (and vice versa), you could do a seperate if for each of them, like this:

if(!empty($this->element->videos){
    echo $this->element->videos; 
}
if(!empty($this->element->productdownloads){
    echo $this->element->productdownloads; 
}

edit: minor grammatical fixes

Sign up to request clarification or add additional context in comments.

Comments

6
    <?php   
      if(!empty($this->element->videos) || !empty($this->element->productdownloads)) {
        echo $this->element->videos; 
        echo $this->element->productdownloads; 
      }
    ?>

Comments

2
if(!$this->element->videos || !$this->element->productdownloads) 
{
    echo $this->element->videos; 
    echo $this->element->productdownloads; 
}

You dont need to use empty by default php checks for empty when you use ! sign with if condition

Comments

1

Try

if(!($this->element->videos && $this->element->productdownloads)){
    echo $this->element->videos; 
    echo $this->element->productdownloads;  
}

Comments

0

Well, I have had quite number of issues like that.

Try to remove empty space from the two variables before comparing them. Sometimes empty spaces comes before the variable and that hinders comparison.

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.