PHP Echo VS Open&Close Tag

Conditionally echo opening and closing HTML tags

Here you go... one if:

<?php
$count = getCount();
if($count > 0) {
echo "<ul>";
for($i = 0; $i < $count; $i++) {
// echo our items
echo "<li>" . getItem() ."</li>";
}
echo "</ul>";
}
?>

This also skips an unnecessary for loop, which you currently have in your code :)

Of course I jest a bit. Once you finally get past the point of wanting to write spaghetti code, then you can look into various templating engines.

Does the quantity of ?php and ? tags affect performance?

Does it have a performance impact? Yes. Simply because there is more text for the parser to parse. It must have an impact. And yes, it will have a measurable impact.

Does it have a meaningful impact? No. Not at all. You would be hard pressed to see any difference even if you had millions of them in your app. Yes, there will be more clock cycles used, but trivially...

The bigger thing is to note that the two pieces of code are not identical in general. PHP will strip a single new line after a closing ?>, but any other characters after ?> will render in the output. So trailing spaces or multiple newlines after ?> will be rendered directly.

So my suggestion is ignore the performance, write the correct and more readable code (the more semantically correct code). And ignore small performance differences...

Opening/closing tags & performance?

3 simple rules for you to get it right:

  • No syntax issue can affect performance. Data manipulation does.
  • Speak of performance only backed with results of profiling.
  • Premature optimization is the root of all evil

Performance issues are quite hard to understand. It is advised for the newbies not to take it into account. Because they are always impressed with trifle things and fail to see a real important things. Just because lack of experience.

Same for your question. Imagine you'll ever get some difference. Even big one, say, one method is 2 times faster. Oh my, 2 times! I choose it and optimized my app well, it will run 50% faster now!

Wrong. Not 50%. You'd never notice or even measure this speed increase. Because you optimized a part that take only 0,0001% of whole script runtime.

As for the big HTML tables, it take a long time for the browser to render it. Much more than you took to generate.

Profiling is a key word in the performance world. One can trash any performance related question with no doubts if there is no word "profiling" in it.
At the same time profiling is not a rocket science. It's just measuring of runtime of different parts of your script. Can be done with some profiler, like xdebug, or even manually, using microtime(1). And only after detecting the slowest part, may you start with tests.

Learn to profile before asking performance questions.
And learn not to ask performance questions if there is no real reasons for it.

Premature optimization is the root of all evil - D.Knuth.

PHP echo vs PHP short echo tags

First of all, <?= is not a short open tag, but a shorthand echo, which is the same as <?php echo. And it cannot be disabled. So, it's safe to use in the meaning it is always enabled.

Speaking of safety in terms of security, the output must be always encoded according the the output medium rules.

For example, when echoing data inside HTML, it must be html-encoded:

 <?= htmlspecialchars($function_here, ENT_QUOTES) ?>

Or, when echoing data inside JavasScript, it must be javascript encoded:

 <script>var=<?= json_encode($function_here) ?>

Or, when it's going to be both HTML and JS, then both encodings must be used:

<?php foreach($links as $label => $url): ?>
<br>
<form method="post">
<button class="my" onclick="<?=htmlspecialchars("window.open(".json_encode($url).")", ENT_QUOTES) ?>">
<?=htmlspecialchars($label, ENT_QUOTES) ?>
</button>
</form>
<?php endforeach ?>

Speaking of short open tags, there is only one, <?, and it's not always enabled (see the short_open_tag directive).

Actually, in the php.ini-production file provided with PHP 5.3.0, they are disabled by default:

$ grep 'short_open' php.ini-production
; short_open_tag
short_open_tag = Off

So, using them in an application you want to distribute might not be a good idea: your application will not work if they are not enabled.

<?php, on the other side, cannot be disabled -- so, it's safest to use this one, even if it is longer to write.

PHP - echo the opening and closing of tags from array

I'm honestly not sure if this is exactly what you're looking for. It would be great to have a full desired output... but I believe this is as close as it gets. It took me 3 hours so it better be it. It's a great question, very hard to accomplish.

I did print_r(htmlentities($base)), but you can simply do print_r($base) to see the formatted result. I did that because it was easier to check with the output you provided in the question.

Also, I modified your JSON because some tags specified there are non-existent. For example, I changed underline for u, italic for i, bold for b. Alternatives are em, strong... anyway, that's just a side-note.

<?php
$build = json_decode('["This is a test\nIncluding ",["bo","b"],["ld",["i","b"]],[", ","i"],["u",["u","i"]],["nderlined","u"],", ",["strike-through","strike"],", and ",["italic","i"],"\ntext:\n\n",["numbered lists",["u","strike","i","b"]],["\n",[]],"as well as",["\n",[]],["non ordered lists","http:\/\/test.com"],["\n",[]],"it works very well",["\n",[]],["try it","http:\/\/google.com"],"\n",["http:\/\/google.com",["b","http:\/\/google.com"]],"\n\n",["wow","b"],"\n",["lol","b"]]', true);
$used = [];
$base = '';
foreach($build as $data){
if(is_array($data)){
$text = array_shift($data);
$tags = $data[0];
if(!is_array($data[0])){
$tags = [$data[0]];
}
$elements = '';
$tagsToClose = array_diff($used, $tags);
$changes = true;
$i = 0;
foreach($tagsToClose as $tag){
while($changes){
$changes = false;
if($lastOpened != $tag){
$changes = true;
$elements .= '</'.$lastOpened.'>';
unset($used[$i++]);
$lastOpened = $used[$i];
}
}
$elements .= '</'.$tag.'>';
$key = array_search($tag, $used);
unset($used[$key]);
}
foreach($tags as $tag){
if(!in_array($tag, $used)){
$elements .= '<'.$tag.'>';
array_unshift($used, $tag);
$lastOpened = $tag;
}
}
$elements .= $text;
$data = $elements;
}
$base .= $data;
}
unset($used);
$base .= '</'.$lastOpened.'>';
print_r(htmlentities($base));
?>

EDIT

And here's the result I got, just in case you run into some trouble testing or to check with your results or whatever:

This is a test Including <b>bo<i>ld</i></b><i>, <u>u</u></i><u>nderlined, </u><strike>strike-through, and </strike><i>italic text: <u><strike><b>numbered lists</b></strike></u></i> as well as <http://test.com>non ordered lists</http://test.com> it works very well <http://google.com>try it <b>http://google.com </b></http://google.com><b>wow lol</b>

when i use php echo, why tag elements are closed automatically?

That has nothing to do with PHP. It's just that html doesn't want you to wrap h2-tags in p-tags. You should rather use divs, regardless of what you are trying to accomplish here.

How can I put an opening and a closing PHP tag in an echo?

You need to replace the < and > characters with their HTML entities:

<?php echo "<?php include('this.php'); ?>"; ?>

If you want to include the file, there's no need for the above, running the code below is more than sufficient:

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

You don't have to use two lots of PHP tags...

PHP - 'print/echo' displaying closing tags - or not outputting

You cannot put PHP code in an HTML file (unless you tell Apache to parse HTML files).

Rename the file to index.php

Do you have a webserver with PHP set-up?



Related Topics



Leave a reply



Submit