Jump to content

php code not executing


anotherphpnoob

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.