FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


<?php ?> Tags





polly-gone
Is it necessary to have
Code:
<?php ?>
on a page ending with *.php and that has absolutely no markup in it?

Thanks,

-Nick Smile Smile Smile
mathiaus
No, the tags are only required to actually parse php code.
the zephyrus
If you don't have them, it assumes you want the contents printed out rather than executed. You can include() a file with no php tags but it's probably not what you want.

From http://nl3.php.net/manual/en/function.include.php :
Quote:
When a file is included, parsing drops out of PHP mode and into HTML mode at the beginning of the target file, and resumes again at the end. For this reason, any code inside the target file which should be executed as PHP code must be enclosed within valid PHP start and end tags.
TomS
polly-gone wrote:
Is it necessary to have
Code:
<?php ?>
on a page ending with *.php and that has absolutely no markup in it?


I think you got it wrong, mathiaus.

If you use php code, you need the php-tags. Even for
Code:
<?php
echo 'hello world';
?>
mathiaus
TomS wrote:
polly-gone wrote:
Is it necessary to have
Code:
<?php ?>
on a page ending with *.php and that has absolutely no markup in it?


I think you got it wrong, mathiaus.

If you use php code, you need the php-tags. Even for
Code:
<?php
echo 'hello world';
?>


Me, wrong??? Never! Razz

Your code uses the php function echo to display hello. You could simply just type hello and rather than php parsing the echo function, it will just serve the text provided (as HTML).

Just because the file extension is php doesn't mean that php has to be used, plus the question mention that there was no markup!
Hogwarts
mathiaus wrote:
Me, wrong??? Never! Razz

Your code uses the php function echo to display hello. You could simply just type hello and rather than php parsing the echo function, it will just serve the text provided (as HTML).

Just because the file extension is php doesn't mean that php has to be used, plus the question mention that there was no markup!


Mathiaus is quite right here, TomS.

Of course, polly-gone, you would end up with a somewhat empty file if it had no markup in it and no PHP (unless you're including other forms of text as something else).

If you want PHP to execute, regardless of filetype, you will need those tags, and the PHP must be within them. This holds true even if there is no extension and you're using a hashbang + trying to execute the script via a command-line interface.
halilibo
these tags are only used to mark PHP codes.

you dont have to use them, if you havent written PHP codes.

For Example:



a.php
Code:

<?PHP
//write something here
echo "foo";
?>



b.php
Code:

<html>
<body>
foo
</body>
</html>


both usages are true. just use <?PHP ?> tags, only when you are writing PHP codes.
TomS
Hogwarts wrote:
Mathiaus is quite right here, TomS.


No, he's not Wink

The actual question was:

Is it necessary to have PHP tags on a page ending with *.php and that has absolutely no markup in it?

Markup is html. Php is not a markup language.

So the answer to this question is: Yes, you need php-Tags, even if the file-extension is .php and even if you're using only php-code and no html-code.
sonam
I don't know enought English and I cannot say who is right and who is wrong. But if I good understand question then you can use .php extension on plain html page (page without any type of php script - echo, include, session, etc.). I am always creating plain html pages with .php extension because I never know would I like to put some php in this pages after some times. The main reason for this practice is SEO and indexing in search engines.

Sonam
imagefree
polly-gone wrote:
Is it necessary to have
Code:
<?php ?>
on a page ending with *.php and that has absolutely no markup in it?

Thanks,

-Nick Smile Smile Smile


yes you can use .php for a file that has no markup. For example your javascript file's extension may be .php
The only thing you need in above case is to send appropriate headers or by any other possible way tell the browser that the requested file is js.
rvec
polly-gone wrote:
Is it necessary to have
Code:
<?php ?>
on a page ending with *.php and that has absolutely no markup in it?

Thanks,

-Nick Smile Smile Smile

No you can also only start with <? and don't close if short tags are allowed by your php settings Smile

actually not closing the php tags is a better idea most of the time. That way you won't accidentally have white lines giving you the infamous "headers already send" error.
TomS
Hm?

Header already sent is caused by rubbish before the Header() function of Php.
So there's now problem with that.

Code:
<?php
Header("Location: mypage.php"); //e.g
?>
<i>Some</i> <u>markup</u> <b>here</b>


While the code without closing the tag procudes an parsing error.

Code:
<?php
Header("Location: mypage.php"); //e.g

<i>Some</i> <u>markup</u> <b>here</b>


I've never come across the idea of not closing the Php-tags and I just don't see the point Shocked
rvec
index.php:
Code:
<?php
include('include.php');
session_start();
?>

include.php:
Code:
<?php
//some mysql stuff here, and basic functions that need to be included
?>


Notice how there is a whiteline in the include file, which will be seen as output and send to the browser. Because output is send to the browser, the headers will also be send, and because the headers are already send the "session_start" function will throw a "headers already send" error.

This would not have happened if the include file didn't have the php closing tag. Instead of sending the whiteline, the whiteline would be seen as part of the php and just ignored.

Now in this small part it is easy to detect, but think of a big file ending with "?> " (including the space after the tag), that would also throw the same error and would be a pia
TomS
Alright. In that case... I see your porint.
I usually write <?php ?>, then save the file and then insert my code in between the php-tags. It's unlikely for me to produce unwanted output that way.
Related topics
PHP Toturail
How To : Improve Your PHP Programming
Image in title bar
[PHP] Executing a local file
converting <TABLE> to CSV
Displaying Files...
targeting <div> tags with links
Validation problems with <form>
Question about a Fill out form.
header_already_sent!
<h1> and <strong> tags
<li> tag document type?
<?php session_start(); ?> on Every Page
Build Your Own PHP/MYSQL website
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.