Jump to content

PHP Scrips not executing


yrstruly
 Share

Recommended Posts

Hello I am doing this book by Larry Ullman. PHP for the Web Visual Quick start Guide. Im having trouble with a basic code like this: 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML1.0 Transitional//EN"2 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">3 <html xmlns="http://www.w3.org/1999/xhtml"xml:lang="en" lang="en">4 <head>5 <meta http-equiv="content-type"content="text/html; charset=utf-8" />6 <title>Hello, World!</title>7 </head>8 <body>9 <p>The following was created by PHP:10 <?php11 print "Hello, world!";12 ?>13 </p>14 </body>15 </html> Its not printing "Hello, World", all it prints is The following was created by php. What am i doing wrong? I find that most of the codes executes like this from this book. Pls HELP

Link to comment
Share on other sites

The code is correct, so it sounds like you're either not running it on a machine that has PHP, or you're not accessing the file through a web server. Make sure when you run the file that the URL that the browser shows starts with http:// and not something else.

Link to comment
Share on other sites

I am running it on localhost so it shows:file:///C:/xampp/hello1.php. When im running the following code it displays correctly, so i dont understand why the others wont execute and its url is: file:///C:/xampp/www/welcome.html<!DOCTYPE html PUBLIC "-//W3C//DTD? XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/? xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/? 1999/xhtml" xml:lang="en"?lang="en"><head><meta http-equiv="content-type"? content="text/html;? charset=utf-8"/><title>Welcome to this Page!? </title></head><body><h1>This is a basic XHTML page!? </h1><br /><p>Even with <span style=?"font-size: 150%;">some</span>? decoration, it's still not very? exciting.</p></body>

Link to comment
Share on other sites

You are opening the file localy which wont execute php. You need to use http://localhost/location/to/your/file.phpAlso you need to put the file in web root. In xampp there a 'htdocs' dir which is webroot in xampp

Edited by birbal
Link to comment
Share on other sites

My root is file:///C:/xampp/htdocs/mylife.php, thats what it shows. I have used xammp before and it worked, i could execute my files from any folder just as long as its routed correctly. Why is the the previous code working and not the last one, thats the problem or what im asking? The code is executing just not the <?phpprint "Hello, world!";?> When i put it http http://localhost it automatically direct it to file:///C:/xampp/htdocs/mylife.php. Whats wrong?

Edited by yrstruly
Link to comment
Share on other sites

Code inside php block will not be parsed as php unless you call the page via webserver eg localhost. Other html code will be sent to browser as text and will be interpreted and shown as html pageYour can execute your page from http://localhost/myfile.phpWhat do you see when you run it

Link to comment
Share on other sites

When i run this code:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>Hello, World!</title></head><body><p>The following was created by PHP:<?phpprint "<span style=\"font-weight:bold;\">Hello, world!</span>";?></p></body></html> It executes on the server, but its not making the "Hello, World" bold and reads like this: The following was created by PHP: Hello, world!" ?> Is there a quotation to many or any brackets that should not be there?

Link to comment
Share on other sites

It's still the same problem. Double-clicking on a file to run it does not execute PHP. PHP is set up with your web server. Therefore, if you want to execute PHP you need to use the web server. Double-clicking on the file does not use the web server, it just opens the file in the browser and the browser displays the PHP code as text. You need to use http://localhost to access your page through the web server, which will execute PHP. That's the only way to do it through the web server. If you don't do that then you're not going to have the PHP code executed.

Link to comment
Share on other sites

How do you know php is working in other case?

Link to comment
Share on other sites

Answer is already given.. Please read the other posts.Click this link and tell what you seeHttp://localhost/mylife.php

Edited by birbal
Link to comment
Share on other sites

The answer is not here cause i've been doing it like you guys said and its not working!It keeps on giving me this "; ?> on every page/code i do. Seems to only print the html or partially php...I saw post on the phpini file that needs to be set to on and not off, can this be it. In the xampp php file is php.ini-development and php.ini-production

Edited by yrstruly
Link to comment
Share on other sites

the simplest page I can think of to test/verify PHP is

<?phpinfo();?>

otherwise, as has been mentioned1) make you are actually running a web server locally, not sure which one you use, WAMP/MAMP are popular options. 2) make sure the file is in the webroot, either www or htdocs, depending on the server. 3) Then you have to make sure you access the file over http://localhost/path/to/file. those are the minimum requirements.

Edited by thescientist
Link to comment
Share on other sites

The answer is i dont know, maybe its just the html code thats working, obviuosly. So how do i fix this problem, i am running it through my localhost?
The answer was given above. go onto a different computer, when Xampp is on, and load the address;http://www.{IP OF THE MACHINE YOU'RE HOSTING XAMPP ON}/myfile.php and it'll work.
Link to comment
Share on other sites

To make sure it will work with short tags disabled, the phpinfo file should have this:

<?php phpinfo();

If you save a PHP file with only that and nothing else in it, and pull it up in your browser, it will be very obvious whether or not PHP is working.

Link to comment
Share on other sites

Thank you guys. I think it is the laptop thats the problem. I have run the same codes via an online server and it works! Another problem, when i run thiscode<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>Greetings!</title></head><body><?php // Script 3.7 - hello.php ini_set ('display_errors', 1); // Let me learn from my mistakes!error_reporting (E_ALL | E_STRICT); // Show all possible problems! // This page should receive a name value in the URL. // Say "Hello":$name = $_GET[''];print "<p>Hello, <span style=\"font-weight: bold;\">$name</span>!</p>"; ?></body></html> I get this output: PHP Error Message Notice: Undefined index: Tony in /home/a9677473/public_html/hello.php on line 17

Hello, ! The html code is working fine and all seems to be in place, what am i doing wrong here?

Edited by yrstruly
Link to comment
Share on other sites

Are you trying to run that page like this: page.php?=Tony You're looking for a value without a name, it would be better to give it a name: page.php?name=Tony And then look for the value of the name: $name = $_GET['name'];

Link to comment
Share on other sites

have you looked at what's on/around line 17? undefined index means you have a problem with an array, specifically accessing one of its members. Probably your empty $_GET['']

Link to comment
Share on other sites

When i run the code, it gives the following output: Hello, $name!"; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>Greetings!</title></head><body><?php // Script 3.7 - hello.phpini_set ('display_errors', 1); // Let me learn from my mistakes!error_reporting (E_ALL | E_STRICT); // Show all possible problems!// This page should receive a name value in the URL.// Say "Hello":$name = $_GET['Celia'];print "<p>Hello, <span style=\"font-weight: bold;\">$name</span>!</p>";?></body></html><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>Greetings!</title></head><body><!-- Script 3.6 - hello.html --><div><p>Click a link to say hello:</p><ul> <li><a href="hello.php?name=Michael">Michael</a></li> <li><a href="hello.php?name=Celia">Celia</a></li> <li><a href="hello.php?name=Jude">Jude</a></li> <li><a href="hello.php?name=Sophie">Sophie</a></li></ul></div></body></html> Im including the html code:

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...