Do i need to close

Possible Duplicate:
Why do some scripts omit the closing PHP tag, '?>'?

I've seen in a PHP framework (I can't remember which) that they didn't close the php tag (?>) at the bottom of the pages.

Why is that and should I do it too?

asked Apr 18, 2011 at 10:46

Do i need to close

1

If it's a PHP file that contains no HTML, then don't close the tag.

This stops you from accidentally adding whitespace at the end of the file, therefore invoking browser output, and by extension headers, etc, which can cause a world of pain.

answered Apr 18, 2011 at 10:49

Stephen MelroseStephen Melrose

4,6825 gold badges27 silver badges41 bronze badges

0

The framework you saw is most probably Zend Framework. From the code style section of their manual:

For files that contain only PHP code, the closing tag ("?>") is never permitted. It is not required by PHP, and omitting it´ prevents the accidental injection of trailing white space into the response.

answered Apr 18, 2011 at 10:50

TreffynnonTreffynnon

21k6 gold badges62 silver badges97 bronze badges

1

Basically it means a PHP file won't have trailing whitespace.

If you include a file with trailing whitespace and try to set header() or cookies or stuff like that, then the trailing whitespace will cause a problem.

Do i need to close

answered Apr 18, 2011 at 10:49

Do i need to close

bumperboxbumperbox

10k6 gold badges42 silver badges65 bronze badges

I personally prefer not to do it as it caused some unexpected troubles in the past. If you have white space(s) after closing tag, it might cause some troubles (like outputting this white space to browser, which is unpleasant if you are parsing XMLs) which are quite hard to debug.

answered Apr 18, 2011 at 10:51

Ondrej SlintákOndrej Slinták

30.7k20 gold badges92 silver badges125 bronze badges

You don't have to, as long as it's also the end of that script.

Adding ?> to the end of a file doesn't serve any purpose, it just adds a few bytes to the filesize.

answered Apr 18, 2011 at 10:49

Adam HopkinsonAdam Hopkinson

27.7k7 gold badges65 silver badges95 bronze badges

anisgazig at gmail dot com

10 months ago

If you want your file to be interpreted as php then your file must start and end with and ?> and everything outside of that is ignored by the php parser.

php code..//parsed
php code..//parsed
?>
hellow..//normal test but ignred by php parser

Three types of tag are available in php
1.normal tag()
2.short echo tag()
3.short tag()

short tag are bydefault available but can be disabled by short_open_tag = Off and also disabled bydefault if php will  built with --disabe--short--tags()

As short tag can be disabled so only use the normal and short echo tag.

If your file only have php code then  do not use closing tag.
//php code;
//php code;
//php code;
but if you are embedding php with html then enclose php code with opening and closing tag.
<
html>
<
head>
head>
<
body>
php
//php code;
//php code;
//php code;
?>

If you want to just print single text or something ,you should use shorthand version .

But if you want to process something, you should use normal tag.
        //$var = 3;
        //$var2 = 2;
        //$var3 = $var+$var2;
        //if($var3){//result}
?>

If you embedded php with html and single line, do not need to use semicolon







but if you have multiple line, then use semicolon.
//line 1;
//line 2;
//line 3;
?>

Do I have to close PHP tag?

Note: The closing tag of a PHP block at the end of a file is optional, and in some cases omitting it is helpful when using include or require, so unwanted whitespace will not occur at the end of files, and you will still be able to add headers to the response later.

What is the purpose of tags?

The tags tell the web server to treat everything inside the tags as PHP code to run. The code is very simple. It uses an in-build PHP function "echo" to display the text "Hello World ..." in the web page.

What is the correct way to end PHP?

Note: PHP statements end with a semicolon ( ; ).

Do I need to end PHP file with ?>?

It's entirely optional but including it provides the opportunity to slip whitespace into the output by accident. If you do that in a file that you include or require before you try to output headers then you'll break your code.