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:
- Create a file:
- Save that file into your web server.
- Enter only the code below and view the address in your web browser.
- The screenshot is operating on that exact code.
<?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
?>. The code
echo instructs to print the text that's within in.
If your browser looks like this next screenshot instead, and you have the exact code entered, a couple things could be causing that.
- Does your web server support PHP?
- Have you saved the file in a spot that can run the PHP script?
- Make sure you don't include those line numbers
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
« Go learn some more