Do i need to end php file with ?>?

anisgazig at gmail dot com

11 months ago

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

<?php
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(<?php ?>)
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
//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;
?>
</body>
</html>

If you want to just print single text or something ,you should use shorthand version .<?= $var ?>

But if you want to process something, you should use normal tag.
<?php
       
//$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
<html>
<head>
<body>
<?= $var ?>
</body>
</head>
</html>
but if you have multiple line, then use semicolon.
<?php
//line 1;
//line 2;
//line 3;
?>

As in C or Perl, PHP requires instructions to be terminated with a semicolon at the end of each statement. The closing tag of a block of PHP code automatically implies a semicolon; you do not need to have a semicolon terminating the last line of a PHP block. The closing tag for the block will include the immediately trailing newline if one is present.

Example #1 Example showing the closing tag encompassing the trailing newline

<?php echo "Some text"?>
No newline
<?= "But newline now" ?>

The above example will output:

Some textNo newline
But newline now

Examples of entering and exiting the PHP parser:

<?php
    
echo 'This is a test';
?>

<?php echo 'This is a test' ?>

<?php echo 'We omitted the last closing 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. It is also handy if you use output buffering, and would not like to see added unwanted whitespace at the end of the parts generated by the included files.

Krishna Srikanth

16 years ago

Do not mis interpret

<?php echo 'Ending tag excluded'; with<?php echo 'Ending tag excluded';
<
p>But html is still visible</p>The second one would give error. Exclude ?> if you no more html to write after the code.

M1001

3 months ago

You are also able to write more than one statement in one line, just separating with a semicolon, example:

<?php
echo "a"; echo "b"; echo "c";
#The output will be "abc" with no errors
?>

anisgazig at gmail dot com

2 years ago

At the end of each statement,we will use a semicolon to seperate each statement.Php closing tag autometically implies a semiclon.So last line of a statment do not require a statement.

<?php echo "first statement";
      echo
"second statement"
?>

Closing tag is optional and some cases omitting the closing tag is very helpful.
1.using include or require function
2.unwanted whitespace will not occur at the end of file
3.add header any time

<?php echo "omitting closing tag";

Is it necessary to close PHP tag?

It is recommended that a closing PHP tag shall be omitted in a file containing only PHP code so that occurrences of accidental whitespace or new lines being added after the PHP closing tag, which may start output buffering causing uncalled for effects can be avoided.

How do you end a PHP file?

The exit() function in PHP is an inbuilt function which is used to output a message and terminate the current script. The exit() function only terminates the execution of the script.

What should I save a PHP file as?

Save the File.
Choose Save from the File menu..
Enter your_file_name. php into the Save As field, being sure to include the . php extension..
Click the Save button..

What is the PHP extension file?

A file with the . php file extension is a plain-text file that contains the source code written in the PHP (it's a recursive acronym meaning PHP: Hypertext Preprocessor) programming language. PHP is often used to develop web applications that are processed by a PHP engine on the web server.