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 PHP code that is contained within the <?php
tag you write in a string again this same tag, it will not be interpreted as a dynamic language (unless you use eval
but this is dangerous and unnecessary for your problem).
Then you can use .
to concatenate a string in PHP, as I demonstrated in the example code of your fix.
Poorly typed conditional in PHP:
As for the expression on your conditional, PHP is a poorly 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 an empty string or array this will return true .
Alternative 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 one.
<?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
of 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 of using short-tags (it must be active in your PHP.INI, use phpinfo()
to know if it is active), is to use only <?=$variavel?>
notice that this way the writing is shorter:
<?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 quotation marks are different
In PHP single and double quotes are different, if you'd like to better understand how they work, look at this question: Difference between single and double quotation marks in PHP