Jump to content

Does a PHP script require "?>"


liderbug
Go to solution Solved by Psycho,

Recommended Posts

I've just downloaded a 3d party app and I'm finding that most (98%) of the [files].php have a "<?php" but not a "?>" in them.  I created a tst.php...HW and it seems to not care.  An improvement? Mox-Nix? Poor form? Or the same coder the loves 0xEEE text on a 0xFFF background because it's sooooo cool.

 

Head @ 42 deg /////

 

Thanks

cal

 

 

Link to comment
Share on other sites

  • Solution

No, it is not required. There are good reasons to leave it off - such as the dreaded "Headers already sent error". As you may know, some commands will fail if any content has already been generated for display in the browser - such as a header() command. If you include multiple files into a script that *may* use a header() command later you cannot have any of those include files generate output if you will later invoke the header() function.

 

If you do use "?>" then you cannot have any characters following the closing tag. Else, it will be considered output for the browser. By leaving the "?>" off, then all of the content of the file is considered PHP code.

Edited by Psycho
Link to comment
Share on other sites

From the manual

http://php.net/manual/en/language.basic-syntax.phptags.php

 

 

If a file is pure PHP code, it is preferable to omit the PHP closing tag at the end of the file. This prevents accidental whitespace or new lines being added after the PHP closing tag, which may cause unwanted effects because PHP will start output buffering when there is no intention from the programmer to send any output at that point in the script.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.