Anything wrong with this code, it works great, but I don't understand the forth line. Why is closing bracket all by itself? I am a fairly new to PHP and always Google for answers, but I cant figure this one out. Hopefully, I can help others someday. Thanks
<div class="errorbox">
<?php if(isset($error2)){?>
<strong class="error"><?php echo $error2;?></strong>
<?php } ?>
</div>
Nothing is wrong with it. You can break in and out of PHP, and that is what this code is doing. Sometimes it's easier to break out of a PHP block to write some HTML, then go back into PHP
It's ending the if statement created on line 2, but line 3 is outputting HTML so php is ended, only to begin on the next line to finish the open statement.
Write it like this you suddenly know:
<div class="errorbox">
<?php
if(isset($error2)) {
echo '<strong class="error">' . $error2 . '</strong>';
}
?>
</div>
Or like so:
<div class="errorbox">
<?php
if(isset($error2)) {
?>
<strong class="error"><?php echo $error2;?></strong>
<?php
}
?>
</div>
This is normal PHP templating. It is ouputting HTML. First bracket is opening, second bracket is closing
There are several ways of doing it:
The best way is as descibed in the question:
<div class="errorbox">
<?php if(isset($error2)){?>
<strong class="error"><?php echo $error2;?></strong>
<?php } ?>
</div>
Another way is by echoing:
echo "<div class="errorbox">";
<?php
if(isset($error2)){
echo "<strong class="error">". $error ."</strong>";
}
?>
echo "</div>";
Related
This is the code that's creates the error.
<div>
<?php while($row2 = mysqli_fetch_assoc($get_comment)){?>
<div class="comment">
<p><?php echo $row2['comment']?></p>
</div>
<?php}?>
</div>
Problem is here <?php}?>. You should use an space between php tags like <?php } ?>
For example if you write like <?phpecho "Hello";?> This is invalid. Because compiler will not found any starting tag like <?php Because it is <?phpecho. In your case it is <?php}
I am trying to have the title and date break to two lines but they are currently running in one line.
PHP novice here needs help!
<span class="kb-topic__list-article-title"><?php echo esc_html(get_the_title()); echo nl2br(the_date());?> </span></a>
All you need to do is echo a line break and not use the nl2br() function:
<span class="kb-topic__list-article-title">
<?php
echo esc_html(get_the_title());
echo '<br>';
echo (the_date());
?>
</span>
As long as you're jumping in and out of PHP and HTML, I would separate the dynamic content from the HTML layout. You can accomplish your goal in this fashion with something like the following:
<span class="kb-topic__list-article-title">
<div>
<?php echo esc_html(get_the_title()); ?>
</div>
<div>
<?php echo (the_date()); ?>
</div>
</span>
You can then style the HTML elements however you need to in order to achieve the exact layout you're going for. However the above code will get you the basic effect of putting the output of your two functions on separate lines.
You can also:
<span class="kb-topic__list-article-title">
<?php echo esc_html(get_the_title());?>
<br>
<?php echo (the_date());?>
</span>
have the brake not in php
Using this code:
<div class="venus"><span class="ricon">Established</span><span class="locko"><?php the_field('br_estd'); ?></span></div>
However, I need to wrap this within another PHP IF statement:
<?php $mista = get_field('br_presence'); if ( strval($mista) == 'Worldwide') {echo "PLACE CODE ABOVE HERE" ;} ?>
How could I syntax this?
PHP does have an alternative syntax format for control statements that work well within HTML, although it is generally recommended to use templates and keep logic out of your view as much as possible. That being said, you could do something like:
<?php if (strval(get_field('br_presence')) == 'Worldwide') : ?>
<div class="venus">
<span class="ricon">Established</span>
<span class="locko"><?php the_field('br_estd'); ?></span>
</div>
<?php endif; ?>
could be you are looking for
<?php
$mista = get_field('br_presence');
if ( strval($mista) == 'Worldwide') {
echo '<div class="venus">
<span class="ricon">Established</span>
<span class="locko"><?php the_field("br_estd"); ?></span>
</div>';
//echo "PLACE CODE ABOVE HERE" ;}
?>
Ever since i've used CakePHP, I asked myself about the deeper sense of the recommended syntax of CTP-files, which is basically a HTML-file with all PHP code bracketed with tags. I find this very hard to read and I should think that the context switches between HTML and PHP would add some performance penalty.
Wouldn't it be faster and clearer to collect all output in a string and echo it at the end?
But there is some deeper sense for sure, just that i don't see it..
To make myself clearer, here's an example:
CakePHP:
<?php if (!empty($file['User']['email'])): ?>
<div class="mailto"><?php echo $this->Html->link($file['User']); ?></div>
<?php endif; ?>
<?php if (!empty($file['Document']['comments'])): ?>
<div class="file-comment file-extra column grid_6">
<div class="content"><?php echo $file['Document']['comments']?></div>
</div>
<?php endif; ?>
My approach:
<?php
$out = '';
if (!empty($file['User']['email'])) {
$out .= '<div class="mailto">'.$this->Html->link($file['User']).'</div>';
}
if (!empty($file['Document']['comments'])) {
$out .= '<div class="file-comment file-extra column grid_6">'
.'<div class="content">'.$file['Document']['comments'].'</div>'
.'</div>';
}
echo $out;
?>
So my question is: What are the drawbacks to my approach compared to CakePHP's ?
First things first: writing your entire template as PHP, then echoing it is not a great idea. As a general rule of thumb, I avoid echoing HTML from PHP ever, if I can. there are many reasons, but the main one will be the lack of syntax highlighting in your IDE.
Anyway, code formatting is entirely down to personal preference, but if you're writing your templates like this:
<?php if (!empty($file['User']['email'])): ?>
<div class="mailto"><?php echo $this->Html->link($file['User']); ?></div>
<?php endif; ?>
<?php if (!empty($file['Document']['comments'])): ?>
<div class="file-comment file-extra column grid_6">
<div class="content"><?php echo $file['Document']['comments']?></div>
</div>
<?php endif; ?>
...it's no wonder you can't read them.
There are a few things you could try, to make your code clearer and easier to read. Again, these are down to your own personal preferences, and you could get into the habit of using some or all of them.
Format your HTML properly, with indentations for child elements.
Add white space between lines of code that are too busy, particularly between lines of PHP and lines of HTML.
Use short echo tags syntax (<?= instead of <?php echo).
Assign the more complex PHP values to variables so that your HTML is easier to read.
Remember to comment your code (HTML or PHP), particularly adding HTML comments so that you can easily see separate components of your template at a glance.
Example
<?php
$user = $file['User'];
$comments = $file['Document']['comments'];
?>
<!-- User -->
<?php if (!empty($user['email'])) : ?>
<div class="mailto"><?= $this->Html->link($user); ?></div>
<?php endif; ?>
<!-- File Comments -->
<?php if (!empty($comments)) : ?>
<div class="file-comment file-extra column grid_6">
<div class="content"><?= $comments; ?></div>
</div>
<?php endif; ?>
I'm trying to call an HTML/PHP content that it's inside my database using:
<?php echo $row_content['conteudo']; ?>
When the row is called the HTML appears correctly but the PHP doesn't.
I belieave it's cause of the echo inside the main echo.
<?php echo "
<h3>Hello</h3>
<?php do { ?>
<div class=\"indios\">
<a href=\"indio.php?id=<?php echo $row_indiosct['id']; ?>\">
<img src=\"galeria/indios/<?php echo $row_indiosct['foto']; ?>\" alt=\"<?php echo $row_indiosct['nome']; ?>\" />
<br /><?php echo $row_indiosct['nome']; ?></a></div>
<?php } while ($row_indiosct = mysql_fetch_assoc($indiosct)); ?> "
?>
The line one of this code is the same echo as the first code field, it's not repeating, it's there just for help and to understand where is the problem.
I already fixed some quotation marks but it gives an error in the line of the 1st echo.
That is some of the ugliest code I have ever seen...
<?php
echo '
<h3>Hello</h3>';
while ($row_indiosct = mysql_fetch_assoc($indiosct))
{
echo '
<div class="indios">
<a href="indio.php?id='.$row_indiosct['id'].'">
<img src="galeria/indios/'. $row_indiosct['foto'].'" alt="'.$row_indiosct['nome'].'" />
<br />'.$row_indiosct['nome'].'</a>
</div>';
}
?>
You could also use the HEREDOC syntax.
Don't do this. Multi-line echoes, especially when you've got embedded quotes, quickly become a pain. Use a HEREDOC instead.
<?php
echo <<<EOL
<h3>Hello</h3>
...
<div class"indios">
...
EOL;
and yes, the PHP inside your echo will NOT execute. PHP is not a "recursively executable" language. If you're outputting a string, any php code embedded in that string is not executed - it'll be treated as part of the output, e.g.
echo "<?php echo 'foo' ?>"
is NOT going to output just foo. You'll actually get as output
<?php echo 'foo' ?>
You have misunderstood how PHP works. PHP is processed by the server. When it encounters your script, it sees the following:
<?php echo "some long piece of text that you have told PHP not to look at" ?>
What is the reasoning behind trying to nest PHP calls inside strings?
evaluate code php in string using the function eval(): this post Execute PHP code in a string
<?php
$motto = 'Hello';
$str = '<h1>Welcome</h1><?php echo $motto?><br/>';
eval("?> $str <?php ");
http://codepad.org/ao2PPHN7
also if your need the code buffer output in a string also you can using the ob_start() method:
<?php ob_start(); ?>
<h3>Hello</h3>;
<?php
while ($row_indiosct = mysql_fetch_assoc($indiosct)){ ?>
<div class="indios">
<a href="indio.php?id='<?php echo $row_indiosct['id']'">
<img src="galeria/indios/'<?php echo $row_indiosct['foto'].'" alt="'.$row_indiosct['nome'].'" />
<br />'.$row_indiosct['nome'].'</a>
</div>';
<?php } ?>