469,338 Members | 8,445 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,338 developers. It's quick & easy.

Learning PHP, Can't Get It to Show Up in a Browser

tharden3
916 512MB
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:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <title>Example!!!</title>
  4. </head>
  5. <body>
  6. <?php
  7. $counter=1;
  8. echo $counter
  9. ?>
  10. </body>
  11. </html>
Jan 2 '09 #1
16 14919
Niheel
2,429 Expert Mod 2GB
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?
Jan 2 '09 #2
tharden3
916 512MB
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.
Jan 2 '09 #3
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:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <title>Example!!!</title>
  4. </head>
  5. <body>
  6. 1
  7. </body>
  8. </html>
  9.  
Are you sure you have set your HTTP server up properly?
Jan 2 '09 #4
tharden3
916 512MB
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?
Jan 2 '09 #5
tharden3
916 512MB
@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.
Jan 2 '09 #6
tharden3
916 512MB
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?
Jan 2 '09 #7
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.
Jan 2 '09 #8
tharden3
916 512MB
@Atli
Oh cool. Thanks for the tip :)
Jan 2 '09 #9
Markus
6,050 Expert 4TB
Glad to see you're getting stuck in with PHP, Tharden. Good luck.
Jan 3 '09 #10
tharden3
916 512MB
@Markus
Thanks, I'll let you know how it goes.
Jan 3 '09 #11
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
Sep 20 '10 #12
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)
Sep 20 '10 #13
Dormilich
8,651 Expert Mod 8TB
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).
Sep 20 '10 #14
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":

Expand|Select|Wrap|Line Numbers
  1. <html>
  2.     <head>
  3.         <title>Hello World!</title>
  4.     </head>
  5.     <body>
  6.         <?php 
  7.             // single-line comments can be like this
  8.             # or even like this
  9.             /* multi-line comments can 
  10.                 be like this */ ?>
  11.         <h1>Examples</h1>
  12.         <?php echo "Hello World!"; ?><br />
  13.         <?php
  14.             // The semicolon at the end of the statement is important!
  15.         ?>
  16.         <?php 
  17.             // print works like echo
  18.             print "Hello World!";
  19.         ?><br />
  20.  
  21.         <?php
  22.             // concatenation
  23.             echo "Hello" . " World!<br />";
  24.  
  25.             // simple math
  26.             echo 2 + 3;
  27.  
  28.         ?><br />
  29.     </body>
  30. </html>
Sep 20 '10 #15
I'm also aware that I have downloaded MAMP. Maybe there is some conflict with the apache that comes with Mac OS?
Sep 20 '10 #16
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.
Sep 20 '10 #17

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Sean McCourt | last post: by
5 posts views Thread by Jim | last post: by
26 posts views Thread by mfasoccer | last post: by
26 posts views Thread by K.J.Williams | last post: by
16 posts views Thread by John Salerno | last post: by
reply views Thread by Elizabeth Barnwell | last post: by
11 posts views Thread by Elizabeth Barnwell | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.