2

I'm working with PHP and HTML, but I have an issue popping up whenever I write some PHP code. An example of this is as follows:

<?php
echo "<h2>Hello?</h2>";
$var = 5;
echo "You have $var minutes to go.";
?>

What this ends up outputting on screen is:

Hello?"; $var = 5; echo "You have $var minutes to go."; ?>

But what I want to happen is this: Hello? You have 5 minutes to go.

Is there something I'm forgetting to do? It doesn't seem to matter whether or not I add the HTML preamble, or if I put a tag like

around the second echo line. Does anyone have any advice?

EDIT: Apparently I have failed to parse PHP correctly. This computer is new and I have XAMPP installed on it, but nothing else. Did I miss something I needed in order to use PHP?

5
  • I get the output. ` Hello? You have 5 minutes to go.` Commented Oct 14, 2013 at 13:29
  • It doesn't print the first echo ? Commented Oct 14, 2013 at 13:29
  • Try removing the ? and check again and see what it outputs. Mean the ? after Hello. Maybe it is closing your php tag or something Commented Oct 14, 2013 at 13:30
  • Are you actually running this on a web server, or just attempting to open a .php file in a local browser? Commented Oct 14, 2013 at 13:32
  • Michael Berkowski, at the moment it's just on my local computer. Commented Oct 14, 2013 at 15:07

3 Answers 3

6

That sounds like if the php code wasn't interpreted.

Make sure to have the code in a file with a filename ending with .php and that PHP is installed/enabled on your server.

Sign up to request clarification or add additional context in comments.

5 Comments

If it isn't interpreted, how come the <?php echo part is not printed out on the screen?
@WebNovice this part is interpreted as a html tag. That's why it isn't visible in browser (you'll find it in source code)
5 upvotes for this? :O, Ive posted upvotes longer then my arm :(
Thanks for that. The file is saved and uploaded as a .php file. To be precise, it's: file:///C:/my_folder/test.php I may have forgotten to install something first. This is a new computer I am working with, and I just set up XAMPP and Notepad++.
@SamOfloinn Then you should make attention to access the file via xampp and not directly via the file system. (something that begins with http://localhost/...)
2

The PHP isn't being parsed properly.

  • Make sure you are saving your files as .php
  • If you are running this locally through WAMP the make sure to use localhost in your URL because if your URL looks like this file:///C:/wamp/www/index.php then that is incorrect.
  • I think CakePHP uses .ctp files so that could also be an issue
  • You can setup Apache to interpret any file extension as PHP

Comments

0

make php and html different.so things become much easier. try like this:

<h2>Hello?</h2>
<?php
$var = 5;
?>
You have <?php echo $var;?> minutes to go.

1 Comment

this really isn't the answer to the question; that's just a coding style advice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.