Check if a value exists but print another

Asked

Viewed 164 times

2

I have the following code:

<span class="end"><h1><?php echo $this->seo_tags->h1spam; ?></h1><h2><?php echo $this->seo_tags->spam; ?></h2></span>
<span class="end"><h3><?php echo $this->seo_tags->meta_description ?></h3><span><?php echo $this->seo_tags->meta_description; ?></span></span>

What I need to do is: if the $this->seo_tags->spam (in the first span) does not exist, it prints only the value of the second span.

I tried so:

<?php if($this->seo_tags->spam != '') {
        echo '<span class="end"><h1><?php echo $this->seo_tags->h1spam; ?></h1><h2><?php echo $this->seo_tags->spam; ?></h2></span>';
      }else {
        echo '<span class="end"><h3><?php echo $this->seo_tags->meta_description ?></h3><span><?php echo $this->seo_tags->meta_description; ?></span></span>';
     }
?>

But instead of printing the values from $this->seo_tags->meta_description, he prints only: seo_tags->meta_description; ?>. (Both in the h3how much in the span).

What am I doing wrong? How can I do this?

2 answers

5


What’s wrong with your code is that you’re trying to put a PHP tag inside a echo which is already a PHP print command.

<?php if(!$this->seo_tags->spam) {
        echo '<span class="end"><h1>' . $this->seo_tags->h1spam; . '</h1><h2>' . $this->seo_tags->spam . '</h2></span>';
      }else {
        echo '<span class="end"><h3>' . $this->seo_tags->meta_description . '</h3><span>' . $this->seo_tags->meta_description . '</span></span>';
     }
?>

When you are already writing a PHP code that is contained within the tag <?php you write in a string again this same tag, will not be interpreted as dynamic language (there is less that you use eval but this is dangerous and unnecessary to your problem).

So you can use . to concatenate a string in PHP, just as I demonstrated in the example code of its correction.

Weakly typed conditional in PHP:

As for the expression in its conditional, PHP is a weakly typed language which allows you to make comparisons between different types of variables.

To check if a variable is empty you can deny it !$variavel that is, '' or false or null or 0 all will return true in this expression. You can also use empty($variavel) if it is a string or empty array it will return true.

Alternate form:

To solve the same case alternatively, you can also use PHP to stop interpreting your code between commands, this would show your HTML in some WYSIWYG editors if you are using a.

<?php if(!$this->seo_tags->spam) { ?>
        <span class="end"><h1><?php $this->seo_tags->h1spam; ?></h1><h2><?php $this->seo_tags->spam; ?></h2></span>
<?php } else { ?>
        <span class="end"><h3><?php $this->seo_tags->meta_description; ?></h3><span><?php $this->seo_tags->meta_description; ?></span></span>
<?php } ?>

This code results in the same thing as the previous one, but instead of the HTML being inside a echo PHP, you interrupt the PHP processor between the conditional blocks.

Curiosity about short-tag in PHP

When using code between blocks as in the previous example there is the possibility to use short-tags (it should be active in your PHP.INI, use the phpinfo() to know if it is active), consists of using only <?=$variavel?> notice that this way it gets shorter writing:

<?php if(!$this->seo_tags->spam) { ?>
        <span class="end"><h1><?= $this->seo_tags->h1spam ?></h1><h2><?= $this->seo_tags->spam ?></h2></span>
<?php } else { ?>
        <span class="end"><h3><?= $this->seo_tags->meta_description ?></h3><span><?= $this->seo_tags->meta_description ?></span></span>
<?php } ?>
Single and double quotes are different

In PHP single and double quotes are different, if you want to better understand how they work, look at this question: Difference between single and double quotes in PHP

  • Thank you very much, it worked! And thanks for the explanation, it got me several questions, I’m beginner and I still have these mistakes "idiots". : p hehe

  • 1

    @GWER is a pleasure to help, I was also a beginner one day friend, but it is by bringing the doubts to the surface that we learn.

3

I’m not even a programmer PHP, but I think you should do so (concatenating HTML with the dynamic values of PHP):

<?php if($this->seo_tags->spam != '') {
        echo '<span class="end"><h1>'.$this->seo_tags->h1spam.'</h1><h2>'.$this->seo_tags->spam.'</h2></span>';
      }else {
        echo '<span class="end"><h3>'.$this->seo_tags->meta_description .'</h3><span>'.$this->seo_tags->meta_description.'</span></span>';
     }
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.