Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can raw PHP code go into normal webpage 2

Status
Not open for further replies.

ZOR

Technical User
Jan 30, 2002
2,963
GB
I keep reading comments that PHP code has to be compiled. I have been pasting exampled PHP code into a web page, is that why it does not work. I am publishing it on a hosted site before running it, having PHP, MySql etc on the server. Regards
 
PHP is, generally speaking, an interpreted language. There are compilers that can compile PHP scripts to something similar to Java p-code, but in most applications, PHP scripts are interpreted.

One simple PHP script that does useful things is:

Code:
<?php
phpinfo();
?>

What happens when you put the above into a script file on your server and point to it with your web browser?


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
PHP does not need to be compiled, it is interpreted by the server upon request. The thing to check for is the extension of the webpage. if it ends in htm or html, it will not work unless the server is configured to parse files with html extensions. Most servers with php only parse files with the php extension, like mypage.php.
 
Sorry about that sleipnir, I posted a second after you did .
 
Many thanks sleipnir, I pasted the code into a page, published it, and looked at the site on the browser and it was a blank page. Thanks also vacunita, the page name is Test.htm? the code placed in the page is:

<html>

<body>
<?php

$db = mysql_connect("localhost", "root", "password");
mysql_select_db("mydb",$db);
$result = mysql_query("SELECT * FROM employees",$db);
printf("First Name: %s<br>\n", mysql_result($result,0,"first"));

?>
</body>
</html>

The localhost,root,password are shown above clear for obvious reason. Many thanks both.
 
Aha, I changed the page name to end as .php and the phpinfo came into life. So you were correct, the servers looking for pages identified as PHP. Does that mean I cannot put any HTML code in there?, I will try. However both you folks deserve stars, as I have spent so much time trying to see something appear. Have a good weekend both, thanks again.
 
You can still put html code in there. Only items between <?php> tags will be parsed by php, the rest will just be forwarded on to the browser as if a regular file.

The only difference is that a file must in with .php to be parsed. This leads to a more efficient server (not every file needs to be passed through the php interpreter before being served) but it also makes it obvious to the user that you are using php to generate a dynamic page.
 
Thanks flubbard, that's good otherwise it would have started getting a bigger mess than it is already. Regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top