4

I have recently seen code samples of php which looks something like this:

<?php
   //some code here
   //some more code

Is it possible to write a php code without closing tags? thanks in advance.

Sreekumar R
  • 573
  • 6
  • 24
  • If its the end of the file, yes. In fact, if it's a pure PHP file, you should leave it out, because it might cause whitespaces between files, which could cause issues with certain functions (lke `header()` for example). – Qirel May 06 '17 at 10:37
  • It's recommended not to use a closing tag at the end of a file, because even a newline character after the closing tag will be injected into the browser output, which can break a page in some cases – Mark Baker May 06 '17 at 10:38

2 Answers2

3

This is from PHP Manual

"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."

unclexo
  • 3,691
  • 2
  • 18
  • 26
0

Yeah. In fact it's recommended to do that. IDE softwares like, PhpStorm, for example, are marking it as warning recommending to you to remove the PHP closing tag.

Explanation:

  • It's unnecessary.

  • It's easy to accidentally add significant whitespace after the ?>, which will be output to the client, which in turn can lead to obscure 'headers already sent' errors (this happens when an included file contains whitespace, and you try to set a header after including that file).

I think this question was answered before here:

Why would one omit the close tag?

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

PHP end tag "?>"

Community
  • 1
  • 1
OscarAkaElvis
  • 5,384
  • 4
  • 27
  • 51