Can't get script to work from new ebook: PHP Novice to Ninja

Hello,
You will see from the following that I am a complete beginner with PHP.

I have recently downloaded the new PHP book by Tom Butler and Kevin Yank, PHP & MYSQL: Novice to Ninja.
I have followed all the instructions explicitly and have overcome some problems getting things working but I now cannot get even the first PHP script on page 16 to work and wonder if anyone can help.

The first script reads thus:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Random Number</title>
</head>
<body>
<p>Generating a random number between 1 and 10:
<?php
echo rand(1, 10);
?>
</p>
</body>
</html>

This has been copied directly from the ebook so I know there are no typos. When I try to view the webpage it displays the message ā€˜Generating a random number between 1 and 10:ā€™ but does not show anything else. Viewing the source code from the webpage, I can see the php code is still there but I know it should be replaced with a number.

Has anyone come across this and know where I am going wrong or what I am missing please?

I see my post is not displaying the text of the webpage correctly - it seems to have dropped all the HTML. I donā€™t know how to post this information, so I need help with that also :frowning:

regards

Brian

Are you running this on a server with php installed with a .php file extension?

Verify that the web server and php is installed properly by creating the below file in your web root. Once that is done navigate to it. You should see a table of all php configuration. If you donā€™t see that than go back through the steps in the book to set-up a local dev environment.

phpinfo.php
<?php phpinfo(); ?>

Yes, and by ā€œnavigate to itā€, donā€™t open it in Windows File Explorer, go into the browser and put ā€œhttp://localhost/yourscript.phpā€ into the address bar once you have your local server installed and running. I use WAMP here, there are plenty of others.

Assuming you created the file as random.php, if you have followed the instructions in the Installation chapter and set up the virtual machine, you can visit http://192.168.10.10/random.php in your browser to view the page.

If you have a blank page, you likely either have the file saved with a .html extension or have the file open on a local url such as file://c:/users/name/documents/file.php.

Thank you for all the replies. I have now sorted the problem thanks to your help. It was simply the case that I had given my file an HTML file extension. Once that was changed to PHP everything works as expected.

A fundamental mistake. Sorry to have troubled you guys but thanks so much for your help.

regards

Brian

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.