Make PHP Sites

Last updated: 2015-02-28

Your first PHP programming

Ok - So you have the tools you need to get going. Great! Let's create your very first PHP script.

This tutorial assumes that you have a web server capable of operating PHP and understand where you're supposed to save files to make that happen.

The traditional "Hello World"
Follow these steps:

					<?php
					
						echo '

Hello world!

'; ?>

If your browser matches the screenshot, way to go! You've successfully written your first PHP code and tested your web server to make sure it can display corrently.

So what's going on? The server understands that it needs to process some script between <?php and ?>. The code echo instructs to print the text that's within in.

Problem?

If your browser looks like this next screenshot instead, and you have the exact code entered, a couple things could be causing that.

Let's do some math with PHP

As we'll learn, PHP can do a lot more than just write text. Let's crunch some numbers for a tiny example. Update your code with this:

					<?php
						echo '

Hello world!

'; echo '

Five time five equals ' . 5 * 5 . '

'; echo '

Seven hundred divided by three hundred equals ' . 700 / 300 . '

'; ?>

If the code is entered correctly and the first step above worked, your browser should look exactly like this screenshot.

PHP has plenty of built-in mathematical operations. Check out the full list on the PHP documentation website.

Notice the decimal points between text and numbers in the code. Those decimal points have nothing to do with the mathematic operation; they allow more text to be added to the text before it in the echo operation.

« Go learn some more