View Full Version : Bypassing the Echo
marain
11-25-2012, 02:43 PM
Both of the following excerpts that I have come across appear to work. But I cannot find in my PHP book any indication that the second excerpt is acceptable. I invite comments on the second excerpt: Is it efficient? Is it preferable? Is there any downside to using it?
Best,
A.
<?php
if ( $here === 'qualifications' ) {
echo "Our widgets are better than anyone else's.
<br />
<br />
Call us."
}
?>
<?php
if ( $here === 'qualifications' ) {
?>Our widgets are better than anyone else's.
<br />
<br />
Call us.
<?php
}
?>
djr33
11-25-2012, 02:52 PM
You're thinking about it backwards-- that's NOT part of PHP-- specifically because PHP is only within the <?php ?> tags. That's just normal HTML.
In other words, that's completely acceptable and useful.
What's very interesting about this is that although that's technically outside of the PHP, it still does actually operate within the "if" structure of the PHP-- only if the "IF" is active, then that HTML is displayed. This of course works with other things like loops, functions, etc., in just the same way.
So I suppose it is "part of PHP" in some vague sense, but at the same time it's incredibly convenient because it allows you to use "real" HTML without worrying about escaping the quotes or anything-- just type the HTML as you normally would, but you can still use it within an "if" or other PHP structure.
This is useful for templating and layout in general-- it's best to separate your PHP and HTML as much as possible, and this is one good way to do it. It's much preferable to multi-line strings with lots of complex HTML.
(Also, just for convenience, it'll work in your syntax-highlightling editor and highlight the HTML rather than leaving it as a mono-color string.)
As for efficiency, it's more efficient-- you are processing plain HTML (DIRECTLY AS OUTPUT), without ever making it a string to process. Realistically it won't make much of a noticable difference (unless you have a huge site, and still might not be obvious even then), but it won't hurt anything certainly.
And just for the record, this is important/helpful to know as well:
<?php
if ( $here === 'qualifications' ) {
?>Our widgets are better than anyone else's.
<br />
<?php echo 'Some text'; ?>
<?php echo $myvar; ?>
<br />
Call us.
<?php
}
?> As you can see, you can still use PHP inside that, and it will also be within the "if" statement.
marain
11-25-2012, 03:26 PM
Parenthetical addendum: In reviewing what I posted originally, I note that, in attempting to simplify the problem, I forgot the semicolons. I assume that in the first example, we need a semicolon after call us." In the second example, I assume the semicolon would immediately follow the second <?php.
A.
djr33
11-25-2012, 04:12 PM
You're right-- there's a missing semi-colon in the first example. That's required.
But the second example doesn't have any kind of "echo" statement or anything like that. Remember, it's HTML, not PHP. So not only do you not need it-- but it's not allowed. There's nothing to "end" there with a semi-colon. It's not a line of code. It's just HTML, which happens to be within the context of an "if" statement.
Does that help?
marain
11-25-2012, 04:15 PM
Indeed it does. Thanks again.
A.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.