Jump to content
dalawh

Syntax for PHP files

Recommended Posts

Yes. EDIT: More precisely. you need to echo your html and begin the echo with a single quote and end it with a single quote and semi colon when your html is in a php block.

Edited by niche

Share this post


Link to post
Share on other sites
Yes. EDIT: More precisely. you need to echo your html and begin the echo with a single quote and end it with a single quote and semi colon when your html is in a php block.
What you are saying is that if I want to use html in a php file, I will need to echo all the html code inside a single quote? Also, does the echo print the code to screen or does it go to the server? I want to be more specific:If I had an html and php file, the html would require "<?php include 'header.php'; ?>" in order to get the php file on the html page, but does the php page itself require the "<?php ?>"? If I only had the php file, would I need the <?php ?>"?

Share this post


Link to post
Share on other sites

If you have a file called: page.php In that file you still need <?php ?> for the PHP code to be processed despite it being a .php file.

Share this post


Link to post
Share on other sites
If you have a file called: page.php In that file you still need <?php ?> for the PHP code to be processed despite it being a .php file.
What if it was a html page that had the php include, "<?php include 'header.php'; ?>", will that php file require the "<?php ?>"?

Share this post


Link to post
Share on other sites

If you're not processing any kind of PHP code in the file and just using it to include the header section of your website, then no, don't need. The file can be a .html file as well: header.html as long as no PHP code is being processed.

Share this post


Link to post
Share on other sites
Guest So Called

There seems to be a bit of incompleteness in this topic. When the PHP processor opens a .PHP file it begins in HTML mode and any HTML codes found will be sent to the browser. As the PHP processor scans it is searching for a <?php tag. When it encounters that tag it enters PHP execution mode and begins processing PHP rather than echoing HTML. That process continues until a ?> tag is found in which case it goes back to echoing as HTML. You can in fact have an entire PHP file with nothing inside but HTML and content. If it has no <?php and ?> tags it will behave just like an HTML file. When in PHP mode you can echo HTML within either single or double quotes. They behave somewhat differently but both will echo HTML.

Share this post


Link to post
Share on other sites
If you're not processing any kind of PHP code in the file and just using it to include the header section of your website, then no, don't need. The file can be a .html file as well: header.html as long as no PHP code is being processed.
Oh okay. Thanks.
There seems to be a bit of incompleteness in this topic. When the PHP processor opens a .PHP file it begins in HTML mode and any HTML codes found will be sent to the browser. As the PHP processor scans it is searching for a <?php tag. When it encounters that tag it enters PHP execution mode and begins processing PHP rather than echoing HTML. That process continues until a ?> tag is found in which case it goes back to echoing as HTML. You can in fact have an entire PHP file with nothing inside but HTML and content. If it has no <?php and ?> tags it will behave just like an HTML file. When in PHP mode you can echo HTML within either single or double quotes. They behave somewhat differently but both will echo HTML.
Thanks for explaining this further. Is it better to use php files that include both php and html or html files that include both php and html files?

Share this post


Link to post
Share on other sites
There seems to be a bit of incompleteness in this topic.
We were waiting for you to post a detailed explanation about the question at hand. :happy0046::good:

Share this post


Link to post
Share on other sites
Is it better to use php files that include both php and html or html files that include both php and html files?
If you need to have both PHP and HTML content in a file, the only way is from a PHP file (unless you configure your server otherwise, but... details, details...). To be more precise, if you need any PHP in a file to be executed, that file must be a PHP file (unless you con... you got the point).

Share this post


Link to post
Share on other sites
Guest So Called
We were waiting for you to post a detailed explanation about the question at hand. :happy0046::good:
My comment was based in frustration that IMO the topic seemed to be dancing around what I said without just saying it. I meant no disrespect.
Is it better to use php files that include both php and html or html files that include both php and html files?
Although answered above, here's a few more comments. It's not clear if you mean include as in "has it inside" or include as in "file include." There is no way I know of to include other files in HTML files. They're just flat files and the server sends them in full no matter what they are, even if it's not valid HTML. PHP files can contain both and when in PHP mode other files can be included.

Share this post


Link to post
Share on other sites
If you need to have both PHP and HTML content in a file, the only way is from a PHP file (unless you configure your server otherwise, but... details, details...). To be more precise, if you need any PHP in a file to be executed, that file must be a PHP file (unless you con... you got the point).
I know that, but is it better to have...PHP file that include php code and html codeorHTML file that include php code and html code After you answer the above, I assume it is always better to have the code in its respective file and then include it right? What I mean is if you have a html file, it is better to include a php file rather than have the php code in the html file.

Share this post


Link to post
Share on other sites

So Called, No no.. no disrespect taken. I actually think many of your posts are very informative and helpful. :)

Share this post


Link to post
Share on other sites
I know that, but is it better to have...PHP file that include php code and html codeorHTML file that include php code and html code After you answer the above, I assume it is always better to have the code in its respective file and then include it right? What I mean is if you have a html file, it is better to include a php file rather than have the php code in the html file.
How can you even talk about "better" if only one of the options works to begin with?If there's any PHP executed in the file (and this also means if the PHP in question contains only a one or more "include" statements), this file must be a PHP file. Period.There's no other way. An "HTML file that include php code and html code" will simply not have the PHP code execute.If you don't have any PHP code in a file, that file may be PHP or an HTML file.

Share this post


Link to post
Share on other sites
I know that, but is it better to have...PHP file that include php code and html codeorHTML file that include php code and html code After you answer the above, I assume it is always better to have the code in its respective file and then include it right? What I mean is if you have a html file, it is better to include a php file rather than have the php code in the html file.
The normal approach for web applications is to have a PHP program with no HTML in it which loads the HTML from other files.

Share this post


Link to post
Share on other sites
How can you even talk about "better" if only one of the options works to begin with? If there's any PHP executed in the file (and this also means if the PHP in question contains only a one or more "include" statements), this file must be a PHP file. Period. There's no other way. An "HTML file that include php code and html code" will simply not have the PHP code execute. If you don't have any PHP code in a file, that file may be PHP or an HTML file.
If I misunderstood what you wrote, please correct me. I am going to have to disagree with you, a HTML file can execute PHP code without itself, being a PHP file. It just has to include the "<?php ?>" to indicate that it is PHP. When you said:
There's no other way. An "HTML file that include php code and html code" will simply not have the PHP code execute.
You are completely wrong... an HTML file that include php and html code will execute... http://www.w3schools.com/php/php_syntax.asp That was pretty basic.
The normal approach for web applications is to have a PHP program with no HTML in it which loads the HTML from other files.
I am not making a web application. I am creating a site and I notice some sites are all php files rather than html files, so I was just curious, which was better to use.

Share this post


Link to post
Share on other sites
Guest So Called
So Called, No no.. no disrespect taken. I actually think many of your posts are very informative and helpful. :)
Okay, thanks Don. I'm wary of coming off as being preachy or know-it-all, and don't want anybody to think that of me. I find it all too easy to type something the wrong way and have people misinterpret what I meant. Better to err on the side of the safe.
I know that, but is it better to have...PHP file that include php code and html codeorHTML file that include php code and html code After you answer the above, I assume it is always better to have the code in its respective file and then include it right? What I mean is if you have a html file, it is better to include a php file rather than have the php code in the html file.
No, it's not that at all. And you missed what's already been said--three times now. You can't include anything in an HTML file. It is dead! Whatever is in it, that's what the server sends. If you want active scripting you have to use a language like PHP. It is often the case that you mix PHP and HTML. With more complicated content the content is stored on a database like MySQL and the PHP accesses the database and puts together the HTML page and sends it to the browser. It would be interesting what others have to say about including external HTML content within PHP files. I think having a mix of PHP and HTML files is kind of yucky except for when very little PHP is used on the site, like the contact form I mentioned above. If you look at very advanced content delivery systems I think you'll find that all of them store content in an external database, not in HTML files.

Share this post


Link to post
Share on other sites

If you plan on having any PHP at all, you must give the file a ".php" extension.

Share this post


Link to post
Share on other sites
Guest So Called
You are completely wrong... an HTML file that include php and html code will execute... http://www.w3schools.../php_syntax.asp That was pretty basic.
Sorry, you lose this time. An ordinary HTML file (one ending in .HTM or .HTML) is not executable. It's what we call a flat file. Whatever is inside just gets sent with no server side processing. You should read your link.(It is possible to reconfigure the server to do that, perhaps .htaccess if you're using Apache, but that is not the normal case and is probably rarely done.)

Share this post


Link to post
Share on other sites
If you plan on having any PHP at all, you must give the file a ".php" extension.
Note: That's what I have in mind when I say "PHP file" - a file that has a ".php" extension. At least in a typical context, that's what this means.So with that in mind...
You are completely wrong... an HTML file that include php and html code will execute... http://www.w3schools.com/php/php_syntax.asp That was pretty basic.
... try it! Try to name a file with ".html" extension, have that file contain some PHP code (like "<?php phpinfo(); ?>" for example), place it into a server that supports PHP, and just open it up in your browser, and see if any of the PHP executes. Rename that file to ".php" and see if the results are different.
  • Like 1

Share this post


Link to post
Share on other sites
If you plan on having any PHP at all, you must give the file a ".php" extension.
This is exactly what I needed. Thanks for explaining it. I must of missed that part somewhere.

Share this post


Link to post
Share on other sites

you should have name your file as .php if you want server to interpret as php. php parser parse file which file type is set to executable. by default server is set to parse .php,.php5 as a php file. but you can add any other file name or .html,.htm to treat as a php file. when a file type is set to executable by php engine it parse codes inside php tag. outside anything of php tag send as it is as plain text. php does not know it sends html or other thing. though it sends a content-type header. by default which is text/html or html type which let your browser know the text should be treated as html.

Share this post


Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...

×
×
  • Create New...