Jump to content

Php Code Is Appears In Web Browser


rjosepht

Recommended Posts

I am new to PHP and have been following the W3 PHP tutorial, from the beginning.In addition, I am using "PHP Designer 2007 - Personal" to test the code I am learning.Every example given in the tutorial has worked as described, until I reached the "Array" section.The following code works find in the debugger, providing the correct response, however,the code is appearing in the Web browser (this did not happen with any of the previous examples provided by W3)_________________________________________________________________________________________________<html><body><?php$families = array ( "Griffin"=>array ("Peter", "Lois", "Megan"), "Quagmire"=>array ("Glenn"), "Brown"=>array ("Cleveland", "Loretta", "Junior") ); echo "Is " . $families['Griffin'][2] . " a part of the Griffin family?" . "<br />";?></body></html>_______________________________________________________________________________________________The debugger shows the following:Is Megan a part of the Griffin family? (I expect this is the desired output)While, the browser is displaying the following:array ("Peter", "Lois", "Megan"), "Quagmire"=>array ("Glenn"), "Brown"=>array ("Cleveland", "Loretta", "Junior") ); echo "Is " . $families['Griffin'][2] . " a part of the Griffin family?" . ""; ?> The code seems simple enough, but I have not been able to figure out why it is appearing in the browser, particularly since all of the preceding tutorialexamples did not behave the same way.I'm certain the answer is right in front of me, but being a "newbie", I don't see it.Any guidance will be greatly appreciated.RJ

Link to comment
Share on other sites

Are you testing this code on your local machine? It has to be hosted in a server environment for it to work. That means either setting up a local instance of Apache, or uploading your files to a remote webhost. The code looks fine, by the way.

Link to comment
Share on other sites

Are you testing this code on your local machine? It has to be hosted in a server environment for it to work. That means either setting up a local instance of Apache, or uploading your files to a remote webhost. The code looks fine, by the way.
Thank you for your quick responseI have PHP, and Apache server loaded on my machine.Everything seems to be working fine.As I mentioned, up until I hit "Arrays" in the tutorial, I was not seeing the PHP code appearing in the Web browser.Here's an example, from the same "arrays" tutorial that does seem to work. . .<html><body><?php$cars[0]="Saab";$cars[1]="Volvo";$cars[2]="BMW";$cars[3]="Toyota"; echo $cars[0] . " and " . $cars[1] . " are Swedish cars.";?></body></html>The debugger shows the folowing:Saab and Volvo are Swedish cars. While, the browser does not show the PHP code.Obviously, the only difference in the two examples is how the arrays are declared and populated.Could this have something to do with why the PHP code is appears in the browser in the first code example?RJ
Link to comment
Share on other sites

It sounds as if you either did not name the file with a .php extension or you're not accessing the file through the HTTP.
The file is saved with a *.php extension. As far as accessing the file through http, I'm not sure how the development software works."PHP Designer 2007" has a "Preview" tab and a "Debug" tab.As far as I can figure out, if you click "Preview", you are displaying code through a Web browser, and if you click "Debug" it evaluates the PHP portion of the code.As I mentioned in my previous posts, most of the earlier tutorial examples provided by W3 work, including the 2nd array example I posted.For some reason the first array example is displaying the PHP code in the Web browser.I hate to sound like a "broken record", but I don't want to continue with the tutorials until I, or someone in the forum, can figure out the problem.Your help is greatly appreciated.RJ
Link to comment
Share on other sites

The problem isn't with the code, the code is fine. That's proven by the fact that the debugger shows the right output. I'm not sure what PHP Designer is or how you're previewing the code, but it will be best to open it in a browser and access it normally. Do you have a web server set up with PHP on it, or does PHP Designer include something you use for that?

Link to comment
Share on other sites

The problem isn't with the code, the code is fine. That's proven by the fact that the debugger shows the right output. I'm not sure what PHP Designer is or how you're previewing the code, but it will be best to open it in a browser and access it normally. Do you have a web server set up with PHP on it, or does PHP Designer include something you use for that?
Thanks, someone earlier also said the code looked fine."PHP Designer" has a built-in browser, but just to discount this as a problem, I migrated the code to "Evrsoft First Page 2006".I previewed it in "Internet Explorer", "Firefox", and "Safari".Each one displayed the following:array ("Peter", "Lois", "Megan"), "Quagmire"=>array ("Glenn"), "Brown"=>array ("Cleveland", "Loretta", "Junior") ); echo "Is " . $families['Griffin'][2] . " a part of the Griffin family?" . ""; ?>Any clue as to why it is displaying the code after the array name? Also, I did some searching on the Web and came up with some similar questions/problems.Some point to the php.ini settings. Could this be the source of the problem?If so, why do all the other W3 PHP tutorial examples I have run so far work?Thanks again,RJ
Link to comment
Share on other sites

I don't know how your programs work, but you should install a PHP server such as WAMP and test it in a real server environment. The error is most likely in the programs you're using to preview the pages.

Link to comment
Share on other sites

Any clue as to why it is displaying the code after the array name?
Because it's not executing the code, PHP is not being run.
Some point to the php.ini settings. Could this be the source of the problem?
Unlikely if other things work.
If so, why do all the other W3 PHP tutorial examples I have run so far work?
Because that's probably not the issue.This is the way I would run a PHP file:1. Create the source file. This can be in any text editor, such as Notepad or anything else.2. Save the file as a .php file inside the web server's folder.3. Open a browser and go to http://localhost/ and find the file from there.The problem here is that your web server, or whatever you're running the code on, is not executing the code. That happens when the server doesn't recognize that the file contains PHP code. Why your server doesn't recognize that as PHP is the question. I'm inclined to blame the software you're using like Ingolme mentioned. That code does run fine on my server, so the code is not the problem, your server is the problem. If the program you're using is the server, that's the problem. I would ditch the IDEs and just install a regular test server, you can use a package like WAMP to install everything you need to test.
Link to comment
Share on other sites

Because it's not executing the code, PHP is not being run.Unlikely if other things work.Because that's probably not the issue.This is the way I would run a PHP file:1. Create the source file. This can be in any text editor, such as Notepad or anything else.2. Save the file as a .php file inside the web server's folder.3. Open a browser and go to http://localhost/ and find the file from there.The problem here is that your web server, or whatever you're running the code on, is not executing the code. That happens when the server doesn't recognize that the file contains PHP code. Why your server doesn't recognize that as PHP is the question. I'm inclined to blame the software you're using like Ingolme mentioned. That code does run fine on my server, so the code is not the problem, your server is the problem. If the program you're using is the server, that's the problem. I would ditch the IDEs and just install a regular test server, you can use a package like WAMP to install everything you need to test.
Thanks everyone, I will try to figure this out and let the forum know what I uncover.RJ
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...