anotherphpnoob Posted August 3, 2010 Share Posted August 3, 2010 I am trying to use some php code inside of an html document and it does not work. I have a script.php file that executes perfectly when I try and use it. Here is the code I am currently using: <?php echo "<title>"; echo "the Homepage!"; echo "</title>"; ?> Any suggestions would be great. Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted August 3, 2010 Share Posted August 3, 2010 I am trying to use some php code inside of an html document and it does not work. I have a script.php file that executes perfectly when I try and use it. Here is the code I am currently using: <?php echo "<title>"; echo "the Homepage!"; echo "</title>"; ?> Any suggestions would be great. If the file doesn't have a .php extension then most webservers will not parse it for PHP. You can configure this, but it is normally not done by default. Quote Link to comment Share on other sites More sharing options...
DWilliams Posted August 3, 2010 Share Posted August 3, 2010 Do you have Apache (or substitute whatever web server you're using if not apache) and PHP properly configured on the machine you're trying to execute? What happens when you view script.php in your web browser? Does it give any sort of error or does it just output the PHP code? There's nothing wrong with your code syntax wise (although it's a bit redundant, you don't need 3 echos), so assuming apache and PHP are configured correctly and the script is in one of the folders apache serves up it should be working fine. EDIT: Actually the above post made me realize I might be reading the OP incorrectly. If "script.php" and the document the code you posted are in different files then yes, any file with PHP to be executed needs to have a .php extension as he said. It's actually possible to make PHP execute code in files with other extensions but it goes against the "standard" and I would not recommend doing this unless you A. are the administrator of the server you plan to run this on and B. do not intent to distribute code to other users. Quote Link to comment Share on other sites More sharing options...
monkeytooth Posted August 3, 2010 Share Posted August 3, 2010 php is php, html is html. If the extension of the file you are working with is .html .htm or well anything other than .php then your php code wont work. Try renaming the extension of the file from what it is to .php I only say this cause <?php echo "<title>"; echo "the Homepage!"; echo "</title>"; ?> I really see no problem with and it should work.. But seeing as your working with a "Title" tag it could also be all about placement within the code as well. If your already working with a php file but placing the above code below the "body" tag then it also wont work with what I am guessing you want to do.. Title tags have to go between the head tags. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> your content here.. </body> </html> Also you really dont need 3 lines of php code to echo out the title. if can easily be done up like.. $mytitle = "the Homepage!"; <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title><?php echo $mytitle; ?></title> </head> <body> your content here.. </body> </html> Well its all food for thought, wish you the best. Quote Link to comment Share on other sites More sharing options...
anotherphpnoob Posted August 3, 2010 Author Share Posted August 3, 2010 yes im using apache, and when i execute script.php everything happens exactly as it is supposed to. When i try and view script.php it executes the code. I doubt i have the apache server set up right thought. Any links / websites i should be checking out? Also i tried <?php .... ?> in other parts of my web page and it didn't work, so i don't think it is necessarily because im working with title tags. I hope that i dont have to rename the file .php cause rewriting that page to write everything with php doesn't sound fun. Quote Link to comment Share on other sites More sharing options...
anotherphpnoob Posted August 3, 2010 Author Share Posted August 3, 2010 A. are the administrator of the server you plan to run this on and B. do not intent to distribute code to other users. I am the admin, this is for a school project and basically I just have to make it look pretty. Quote Link to comment Share on other sites More sharing options...
monkeytooth Posted August 3, 2010 Share Posted August 3, 2010 Do you have PHP installed, it doesnt come with apache by itself.. Quote Link to comment Share on other sites More sharing options...
DWilliams Posted August 3, 2010 Share Posted August 3, 2010 yes im using apache, and when i execute script.php everything happens exactly as it is supposed to. When i try and view script.php it executes the code. I doubt i have the apache server set up right thought. Any links / websites i should be checking out? Also i tried <?php .... ?> in other parts of my web page and it didn't work, so i don't think it is necessarily because im working with title tags. I hope that i dont have to rename the file .php cause rewriting that page to write everything with php doesn't sound fun. Can you be more specific about what you mean when you say you're "executing" script.php? Are you navigating to it through the web browser or calling it on the command line with something like "php -f script.php"? As for "rewriting" the page in PHP, that isn't necessary. In a PHP document, any HTML outside of the opening and closing PHP tags will get sent as normal, only PHP inside those tags will be executed. Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted August 3, 2010 Share Posted August 3, 2010 yes im using apache, and when i execute script.php everything happens exactly as it is supposed to. When i try and view script.php it executes the code. I doubt i have the apache server set up right thought. Any links / websites i should be checking out? Also i tried <?php .... ?> in other parts of my web page and it didn't work, so i don't think it is necessarily because im working with title tags. I hope that i dont have to rename the file .php cause rewriting that page to write everything with php doesn't sound fun. You don't have to rewrite it in PHP. You mix PHP and HTML just like you were doing. Rename the file to .php and try it. Quote Link to comment Share on other sites More sharing options...
monkeytooth Posted August 3, 2010 Share Posted August 3, 2010 Reiterating my previous example... <?php $mytitle = "the Homepage!"; ?> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title><?php echo $mytitle; ?></title> </head> <body> your content here.. </body> </html> HTML/PHP are seamless together. PHP just constructs dynamic elements on the server side for display on the client side. As long as you keep your php and html separate (well in most cases, you can write html code within php to be echoed out as well dependent upon your needs) But my above example is a small case senerio based on your original title code you placed. Showing you that HTML and PHP can co-exhist in the same file, all you need do is change the extension from what it is to .php and your on your way. Otherwise the methods you could use to trick your server into serving the extensions you are currently using and out put it as php wont work for everyone. and would render your code as "private" stock only. Quote Link to comment Share on other sites More sharing options...
anotherphpnoob Posted August 3, 2010 Author Share Posted August 3, 2010 Can you be more specific about what you mean when you say you're "executing" script.php? Are you navigating to it through the web browser or calling it on the command line with something like "php -f script.php"? Its a login script i execute from an html form like so : <form action="script.php" As for "rewriting" the page in PHP, that isn't necessary. In a PHP document, any HTML outside of the opening and closing PHP tags will get sent as normal, only PHP inside those tags will be executed. awesome, exactly what i needed to know and how to do it. I think like three people posted something along these lines thanks for the help everyone. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.