Jump to content
MakeWebGames

Simple Question


CHAMAVELI
 Share

Recommended Posts

Re: Simple Question

In all seriousness, you'd be wasting your time making that replacement.

I think people that are new to coding are very keen to make things as efficient as they can. But when you get to be an old and jaded coder like me, you'll be far less likely to feel the urge to implement such changes as a print to echo replacement because you'll have realized long ago that it would make virtually no difference and that your time would be better spent elsewhere doing some real optimization. ;) Or in the case of someone new to coding, time would be better spent writing new code or studying.

Link to comment
Share on other sites

  • 2 weeks later...

Re: Simple Question

print() and echo() aren't exactly the same, replacing them, in some cases(although rare) might result to an error.

One is that print() can be used within variable context, echo() can't.

 

$variable = print 'hello world';
$variable;

versus

$variable = echo 'hello world';
$variable;
Link to comment
Share on other sites

Re: Simple Question

pog-one's example is bad programming technique in most cases.

An example of when you might wanna put HTML text outside of php tags is when your document header is entirely static. In other words, it always stays the same.

Or perhaps your footer might be done this way.

Despite any performance differences of different techniques for outputting HTML in a php script, if you look at professionally coded scripts, you really don't see the use of start and end php tags for the sole purpose of outputting one small tid bit of php generated output.

For instance, phpBB appears to use some sort of parser that takes html files that are used for templating and swaps out certain things for dynamic content.

You might have something like:

display_pic.png

 

The phpBB script would swap out BASE_PHPBB_DIRECTORY for whatever that "constant" represented.

I know people won't take my word for it, but it is definitely true that use of start and end php tags is, in general, bad programming technique and you should stay away from it.

Link to comment
Share on other sites

Re: Simple Question

ok, so if i have a website with a header and footer included on every page and i have some dynamic content what would be the point of having it in 1 big echo tag?

<?php  include('header.php'); ?>




blah blah blah</p>


<?= $anyVariable ?></p>

<?php  include('footer.php'); ?>
Link to comment
Share on other sites

Re: Simple Question

So the header and footer would have the dynamic content, but the body would be static?

In that case, the start and end tags would be fine.

 

In contrast, if you had a form with four input elements, and each needed some dynamic content placed in it, I would NOT use start and end tags all through that form. I'd generate the entire form in the context of a heredoc statement.

And then, if the rest of the page where static, then by all means, place that content outside of the php tags. It's just like embedded javascript. It's extremely poor programming technique to have javascript embedded all over your document. It should all be condensed into an external js file and attached to the DOM dynamically. PHP can't be attached in the same way as javascript, so you have to have php tags in the HTML somewhere, so it's use should be minimized as much as possible.

One or two sets should be all you need. If it starts getting into four, five, six sets of php tags, you really should ask yourself, is there a cleaner way to write this code?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...