Reputation: 2493
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;
}
Upvotes: 2
Views: 7701
Reputation: 717
Try
if(!($this->element->videos && $this->element->productdownloads)){
echo $this->element->videos;
echo $this->element->productdownloads;
}
Upvotes: 1
Reputation: 11
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.
Upvotes: 0
Reputation: 176
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
Upvotes: 2
Reputation: 5766
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
Upvotes: 8
Reputation: 1446
<?php
if(!empty($this->element->videos) || !empty($this->element->productdownloads)) {
echo $this->element->videos;
echo $this->element->productdownloads;
}
?>
Upvotes: 6