PHP #1: Basics
PHP is a scripting language that is widely used on the web. If you know another programming language it’ll be a piece of cake. If not, you’re still in luck because it’s pretty easy to learn.
What it does
PHP is a server-side language, meaning the actual PHP code is interpreted by the server, not by your local computer. A server with php installed will interpret the php code embedded within the html on any page with a .php file extension. When a user loads a php driven website, the server finds the php code within the normal html code and executes this code. When it’s finished, it is left with what looks like just a normal html document (except the .php file extension), which it then sends off to the users’ computer. As with any html file, the html is then interpreted by the web browser and displayed on the screen. In fact, your web browser has no idea whether it was a php generated page or not - it looks just the same.
As I noted before, PHP is embedded within normal html. So how does the server find the PHP code that it needs to execute? It looks for any pairs of opening and closing php tags and executes the code between them.
The opening PHP tag looks like this: <?php
and then closing php tag looks like this: ?>
If you didn’t include any php tags at all, the server would just treat it like a normal html document. In fact, you can change all the file extensions on your website from .htm and .html to .php with no problem whatsoever.
So let’s set up a basic html document:
Now let’s say we wanted to add some PHP inside of the two body tags. We need to add the opening and closing PHP tags in order to do this: <!doctype html> <html lang="en"> <head> <title>My Website</title> </head> <body> <?php // PHP Code goes here ?> </body> </html>
In order for you to run PHP code, you’ll need web hosting or you can just install PHP on your own computer.
I like to edit PHP code in Adobe Dreamweaver, but this is costly and unnecessary. You can in fact edit PHP on any simple text editor like notepad on windows or textedit on mac. If you’d like syntax highlighting, you can download a free text editor like Notepad++ or Notepad2.
Almost every line of PHP code ends with a semicolon, with a few exceptions that we’ll get to when the time comes.
Let’s start by just making a program that will “print” out a message into the body of the html document that says “Hello World”
<html> <head> <title></title> </head> <body> <?php print "Hello World"; ?> </body> </html>
Here we used the print command. All this does is outputs whatever you want into the html document. Here we outputted the text “Hello World”. In this case we wanted to output a string (a series of characters) so we enclose the string in quotes. Single or double quotes will work in this case, but there are some differences that we’ll get to later.
If we view this page in a web browser, you’ll notice we now have a page that says “Hello World”. When viewing the page, go up to the view menu (this may vary between different web browsers) and choose “View Source” or “View Page Source”. Now you’ll see the code that was sent by the server to the browser. Notice that you don’t see the php anymore, and it just looks like a normal HTML document. The PHP code has been executed and is no longer there. Of course, this example is not at all practical, as we could have done the same thing with just some html, and no php.
From now on, I’m just going to assume you know the basic html structure and we’ll just work with the php code that goes inside the php tags.
We could also print out some numbers, which don’t need quotes like strings do:
Of course, we could also use quotes around this if we wanted to, as it is a series of characters (a string).
You’re probably thinking “I know what variables are, I use them in math all the time” and you’re right! You do know what they are! Unlike in math though, variables are not named with names like x and y. Instead, variable names always start with a $ followed by whatever you want to call it. For example, we could have a variable called $value.
Also a little different than in math, in php, the symbol = does not mean “is equal to” it means “is set to”. The equals sign in php takes whatever is on the right side of it, and sticks it into the variables on the left side of it. Let’s make a variable called $height and set it to 10.
$height = 10;
NOTE TO EXPERIENCED PROGRAMMERS: In PHP, you don’t need to declare variable type. Variable type is handled automatically.
Now let’s try printing out this variable into a page.
<?php $height = 10; p rint $height; ?>
Now when you view this page, you’ll see the output is 10. We set the variable height to 10 and then we printed it out into the html. Notice that php code is executed from the top down. If we tried to do something like this:
<?php print $height; $height = 10; ?>
Here printing out $height wouldn’t print 10, because it hasn’t been set to 10 yet.
We can also do some math on our variables:
<?php $height = 10; $width = 38; $area = $height*$width; print $area; ?>
Here we set a variable $height to 10 and variable $width to 38. We then set a variable called $area to the variable $height multiplied by $width. Then we print the $area variable. If you run this script you’ll see you get 380 outputted, which is 10 times 38. Here we used * to multiply. You can also use + to add, - to subtract, and / to divide.
But it doesn’t end there! Variables don’t have to only hold numbers, they can hold strings of text as well:
$firstname = "Jackson"; $lastname = "Hines";
Now we could print this out using the print function:
print "Your name is $firstname $lastname.";
This code will output “Your name is Jackson Hines.” This is a case where single quotes wouldn’t work. Single quotes interpret everything literally. If you tried single quotes on this example you would get out exactly what you put in: “Your name is $firstname $lastname.”