472,342 Members | 1,300 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,342 software developers and data experts.

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

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 15301
2,452 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
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
5,058 Expert 4TB

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>
Are you sure you have set your HTTP server up properly?
Jan 2 '09 #4
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
916 512MB
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
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
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
916 512MB
Oh cool. Thanks for the tip :)
Jan 2 '09 #9
6,050 Expert 4TB
Glad to see you're getting stuck in with PHP, Tharden. Good luck.
Jan 3 '09 #10
916 512MB
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.


Sep 20 '10 #12
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
8,658 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

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 />
  21.         <?php
  22.             // concatenation
  23.             echo "Hello" . " World!<br />";
  25.             // simple math
  26.             echo 2 + 3;
  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

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

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

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.