473,405 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 15434
Niheel
2,460 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,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
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

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

Similar topics

3
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 the book. I get this error message when I try to...
5
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 displayed in a browser, does the page get the...
1
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 tutors are going to be teaching us JAVA wth bluej...
26
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 learning C as a second language, as someone who...
26
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 in C, and he has experience with Visual BASIC....
16
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 don't need to for school, work, or any other reason?...
0
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 If you "Add the subject to your learning...
11
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 just rolled out a lot of changes to YoYoBrain, so...
1
oranoos3000
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 is shown correctly but in Firefox browser is shown...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.