Closing and opening the php tag [duplicate]

Asked
Active3 hr before
Viewed126 times

5 Answers

openingclosing
90%

Closing and re-opening a tag is exactly the same speed as if you were echoing the html from within a single <?php ?> block.,Yes the more files you include, the longer it will take to load a page, especially if they have to be read from disk each time and you are not utilising various caching mechanisms., Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers , By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Does opening and closing php tags (<? php code ?>) multiple times increases page load time?

< ? php code ? >
load more v
88%

Example #1 PHP Opening and Closing Tags, PHP includes a short echo tag <?= which is a short-hand to the more verbose <?php echo. , As short tags can be disabled it is recommended to only use the normal tags (<?php ?> and <?= ?>) to maximise compatibility. , When PHP parses a file, it looks for opening and closing tags, which are <?php and ?> which tell PHP to start and stop interpreting the code between them. Parsing in this manner allows PHP to be embedded in all sorts of different documents, as everything outside of a pair of opening and closing tags is ignored by the PHP parser.

PHP tags

When PHP parses a file, it looks for opening and closing tags, which are <?php and ?> which tell PHP to start and stop interpreting the code between them. Parsing in this manner allows PHP to be embedded in all sorts of different documents, as everything outside of a pair of opening and closing tags is ignored by the PHP parser.

< ? php

PHP tags

When PHP parses a file, it looks for opening and closing tags, which are <?php and ?> which tell PHP to start and stop interpreting the code between them. Parsing in this manner allows PHP to be embedded in all sorts of different documents, as everything outside of a pair of opening and closing tags is ignored by the PHP parser.

? >
load more v
72%

Possible Duplicate: why in some scripts they omit the closing php tag ?> ,As explained in the Code Igniter style guide, it seems that PHP closing tags are optional. It even say you should avoid it for several reasons.,Update: Looking at the zend language scanner src, it would seem that my "hunch" was correct: the T_CLOSE_TAG token would appear to possibly contain a newline char. What's more, It'd also seem that a closing semi-colon for the last statement in a script that contains a closing tag is optional...,One reason people avoid the closing ?> tag is avoid "the headers already sent error" due to line breaks or other invisible characters after the ?> tag.

You can simply use preg_match_all function of the PHP along with following regex

~[id](.* ? )[/id]~

like as

$my_string = "I am with a [id]123[/id] and [id]456[/id]";
preg_match_all("~[id](.*?)[/id]~", $my_string, $m);
print_r($m[1]);
load more v
65%

Well, omitting the closing tag is just one solution for avoiding blanks and other characters at the end of file. For example any char which is accidentally added behind the closing tag would trigger an error when trying to modify header info later.,In some scripts I see that they omit writing a closing tag ?> for the script. Why is it and should I do this as well?,They do it to avoid risking to have whitespaces after the closing tag which may stop headers to work.,Removing the closing tag is kind of "good practice" referring to many coding guidelines.

In some scripts I see that they omit writing a closing tag ?> for the script. Why is it and should I do this as well?

? >
load more v
75%

To still get the benefit of using references in foreach loops without running the risk of these kinds of problems, call unset() on the variable, immediately after the foreach loop, to remove the reference; e.g.:,After going through the first foreach loop, $array remains unchanged but, as explained above, $value is left as a dangling reference to the last element in $array (since that foreach loop accessed $value by reference).,Not sure how to use foreach loops in PHP? Using references in foreach loops can be useful if you want to operate on each element in the array that you are iterating over. For example:,So the main point here is that the empty() method should be used with care as it can lend itself to confusing – or even potentially misleading – results, if one is not careful.

Not sure how to use foreach loops in PHP? Using references in foreach loops can be useful if you want to operate on each element in the array that you are iterating over. For example:

$arr = array(1, 2, 3, 4);
foreach($arr as & $value) {
   $value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)
load more v

Other "opening-closing" queries related to "Closing and opening the php tag [duplicate]"