I'm going through one of the O'Reilly books, learning about PHP and MySQL. I'm in the PHP section and it's giving several examples of what I can type in, but it won't show up in my browser for some reason. I made sure to save the file with a PHP extension, but it still won't show up. I started out with very simple code to try it out: -
<html>
-
<head>
-
<title>Example!!!</title>
-
</head>
-
<body>
-
<?php
-
$counter=1;
-
echo $counter
-
?>
-
</body>
-
</html>
16 15301
You have an error on line 6. You should close it with a semicolon ";"
Is there any kind of error message?
If the page is blank do a view source. Do you see php code in there instead of html ouput?
Well, I did a view source, and the PHP is showing up in a different color than the HTML, so it knows it's there. I tried adding a semicolon to the end of "echo $counter" and it still won't show up.
P.S. Forgive me, but it looks like I forgot to put <head> and </head> tags in my code I posted. I edited and changed it.
Atli 5,058
Expert 4TB
Hi.
So your PHP code is showing in the browser, when you do "view source"?
If that is the case, then you server is not executing the PHP code.
PHP is executed server side and the results of the execution sent to the browser.
For the code you posted, the browser should be getting: -
<html>
-
<head>
-
<title>Example!!!</title>
-
</head>
-
<body>
-
1
-
</body>
-
</html>
-
Are you sure you have set your HTTP server up properly?
Well, it looks like I found my problem. If you right-click and select "page info", it tells me that the page is text/html and is opening in quirks mode (It isn't registering as a PHP page, even though it has a PHP extension). If I recall, when I was making static pages with just HTML and CSS, at the top of the page you address that the page you are making is an HTML page. Do I have to let the browser know in my code "Hey! This is PHP! Not just HTML!"
What do I need to insert in the top of my code to do that?
@Atli
You know, I think something changed (some of the server settings) because I had it running fine yesterday, and now the PHP isn't registering. I'll go back and reconfigure my settings and make sure everything is in it's place.
It's fixed.
Quick question:
Why will the PHP only work on my server, and not when I open it as a file on the C:\ drive?
Atli 5,058
Expert 4TB
PHP is a server side language.
A browser doesn't know nor care what PHP is. If you feed a browser PHP code it will just say "Huh!?" and display it as plain-text. (Or, more likely, it won't display it at all. The <?php ..?> tags will be interpreted as an invalid HTML tag and the entire thing disregarded.)
Your server executes the PHP code and relays the output generated by your PHP code to your browser.
The browser never knows that PHP had anything to do with the process. It just takes the HTML it was fed (or whatever the output is) and displays it as if it was a static page.
@Atli
Oh cool. Thanks for the tip :)
Glad to see you're getting stuck in with PHP, Tharden. Good luck.
@Markus
Thanks, I'll let you know how it goes.
Hi I'm interested in the solution to this problem. I've downloaded MAMP so I can use a server on my mac but I'm getting the same issue I think.
Choosing OPEN and navigating to the .php file I get a window that asks me what should be done with it - use firefox.app or maybe dreamweaver. If I select Firefox, I get a blank page and then another window opens up asking the same question and so it goes on ad infinitum.
If I navigate via localhost~usrname to the mac 'sites' directory I do find the file, but if I open it up I get all the contents of the file displayed as if it were source code - with the exception that if the php had been processed by the browser all references to php should have been stripped out.
So what was the solution? I'm dying to find out! I realise you're on a pc but my guess is the solution isn't that different.
Regards,
Billy
Atli 5,058
Expert 4TB
Hey Billy.
The solution in this case (I assume) was to use a http://localhost/file.php URL rather than a local URL like file:///C:\file.php" (or the Mac equivalent to that).
Looking through the MAMP documentation, it indicates the free version starts on port 8888, so you would have to append that to the URL. For example: http://localhost:8888/file.php (Without specifying the port, the browsers default to the standard HTTP port, 80)
The solution in this case (I assume) was to use a http://localhost/file.php URL rather than a local URL like file:///C:\file.php" (or the Mac equivalent to that).
it would be http://localhost/~<username>/file.php (when you place your files in /Users/<username>/Sites) when you use Mac’s default server (every recent Mac comes with an Apache server and a PHP 5 installation by default).
Hi,
I have tried http://localhost/~<username>/file.php and the document appears as shown below at the foot of this reply.
If I don't put the filename in the URL I do get a list of directories as I would expect, but clicking on the helloworld file delivers the same result. Does this suggest the file is not being interpreted by apache correctly even before the browser gets it?
Document "helloworld.php": - <html>
-
<head>
-
<title>Hello World!</title>
-
</head>
-
<body>
-
<?php
-
// single-line comments can be like this
-
# or even like this
-
/* multi-line comments can
-
be like this */ ?>
-
<h1>Examples</h1>
-
<?php echo "Hello World!"; ?><br />
-
<?php
-
// The semicolon at the end of the statement is important!
-
?>
-
<?php
-
// print works like echo
-
print "Hello World!";
-
?><br />
-
-
<?php
-
// concatenation
-
echo "Hello" . " World!<br />";
-
-
// simple math
-
echo 2 + 3;
-
-
?><br />
-
</body>
-
</html>
I'm also aware that I have downloaded MAMP. Maybe there is some conflict with the apache that comes with Mac OS?
Atli,
Hang on there, I may have done you a disservice. I was testing out your suggestions after being away for a few hours and did so without starting the servers. Having started them, the solution http://localhost:8888/file.php has just delivered.
Thank you so much. I've been wrestling with this for ages.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Sean McCourt |
last post by:
Hi I am doing a JavaScript course and learning from the recommed book
(JavaScript 3rd Edition by Don Gosslin)
Below is one of the exercises from...
|
by: Jim |
last post by:
Forgive me if this is a stupid question. I am completely new to javascript.
If a javascript refernces a javascript file (*.js), when the page is...
|
by: David Van D |
last post by:
Hi there,
A few weeks until I begin my journey towards a degree in Computer
Science at Canterbury University in New Zealand,
Anyway the course...
|
by: mfasoccer |
last post by:
I am sorry if this is an inappropriate place to put this post, if so
please delete it.
I am wondering about a few things. Do you guys recommend...
|
by: K.J.Williams |
last post by:
Hello,
A friend and I want to learn PHP but we have two totally different
programming backgrounds.
I have experience with procedural programming...
|
by: John Salerno |
last post by:
Just something that crosses my mind every time I delve into "Learning
Python" each night. Does anyone see any value in learning Python when you...
|
by: Elizabeth Barnwell |
last post by:
Hi Everyone,
This is a free, web-development learning tool that may be useful to
you in learning CSS.
http://www.yoyobrain.com/subjects/show/471...
|
by: Elizabeth Barnwell |
last post by:
We've built this tool to help with the process of learning programming
languages. You can use material on the site, or add your own to study.
We've...
|
by: oranoos3000 |
last post by:
hi
would you please help me
i have a online shopping center that i show pictures of the my product in home page.
in the InterExplorer pictures...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
|
by: CD Tom |
last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
|
by: jalbright99669 |
last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
...
|
by: Matthew3360 |
last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
| | |