1

I'm currently beginning with PHP and I wanted to get started with a mini-project for some hands-on experience. Here's the code that I began with:

<html>
<header>
	<title>Test</title>
</header>
<body>
	<h3>Some example title</h3>
	<?php
	$randomNumber = rand(1000,9000);
	print "Here's a random number: {$randomNumber}.";
	?>
    <p>Some more text.</p>
</body>
</html>

However, when I saved it as index.php and previewed it on Google Chrome, the PHP part doesn't show up. Can anyone help me realize what I'm doing wrong?

Thanks!

2
  • Did you preview this code on a Webserver or just on you local computer? Commented Jun 19, 2016 at 1:12
  • Its working fine in my localhost. Check the file extension again. Also as Marat pointed out PHP is a server side language so it needs a sever to run. Commented Jun 19, 2016 at 1:12

2 Answers 2

2

To run PHP code (server side) you need a server, or a 'server-emulator' some of the most commonly used are:

Unlike the html in your .php files, the php code must be processed by a server and returned to the browser. I've tested your code with a server and it works like a charm.


After installing one of the above "emulators" you need to (this is a simplified tutorial, check a real tutorial):

  1. place your .php files in the public_html or htdocs folders that are created in the emulator's folder in your computer.
  2. Turn on your server
  3. go to localhost/your-file-path/index.php

If you only need to test soft code (like what's on yout example) and not develop whole projects use IDONE

Sign up to request clarification or add additional context in comments.

Comments

0

PHP is used in the server side, so, you need to install an environment (Apache, PHP) to interpret the PHP. If you use Mac or Windows, you can install Mamp. For unix, you need to install some packages, i'll invite you to see this tutorial: Lamp tutorial.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.