What is PHP?
Posted: January 27, 2009
PHP is a server-side, interpreted scripting language.
What does that mean? Well server-side means it runs the code on the web server, so your users don’t download the code and run it in their browser, like with JavaScript. This means that you’re not making your visitor’s computer do all the work, and it also means that your code is more secure, because your visitors don’t ever see the actual code, only what you send to their browser.
Interpreted means that it doesn’t have to be compiled into a program, like a windows .exe file, to run, but the web server interprets the code live as it comes to it.
A scripting language is really basically a ‘lite’ programming language.
Basically PHP is a way of running code on the web server, so you can output dynamic content to a visitor’s browser.
What does PHP look like?
PHP takes a lot of its syntax from C/C++, like JavaScript, so learning one language will help make it easy to learn the other. You’ll see PHP show up embedded into html code, surrounded by PHP tags, like:
<?php
Some PHP code here
?>
How to use PHP with HTML
As stated above, you can use PHP easily with html, just by including the PHP code between the tags. There are a few other requirements to use PHP on a page. First, the web server has to have PHP installed and be set up correctly to understand PHP. Secondly, you have to name the file that is going to have PHP code in it with a .php extension instead of .html. The web server will still be able to understand all the html you want to put in there as long as it is outside of the PHP tags, but having the PHP extension lets the server know to try and interpret the code before outputting anything to the browser.
First PHP Program:
Create a file. Call it sample.php.
Inside the file, put the following:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My First PHP Project</title>
</head>
<body>
<?php
echo "Hello, world!";
?>
</body>
</html>
Congratulations! You have just created your first ever PHP program!
If you upload this to your web server, you’ll see a page that says
Hello World!
This may not seem like much functionality, but it’s a good start. Outputting things to the web browser is pretty fundamental. What’s more, if you view the source code, you won’t see any sign of the PHP tags. You’ll only see the Hello World text in the source code. That’s because the web server interpreted the code before it sent it to the browser, so all your browser gets is the html.
What is this code doing? Well, the PHP command ‘echo’ means to print whatever text follows it, up to the semicolon. Every PHP command will generally end with a semicolon to let the PHP interpreter on the web server know that it’s the end of the command.
Another important thing to notice is that we put quotes around Hello, world! because it is a text string. Any time you want to store or refer to a string of text, you have to put quotes around it. You can use either single quotes or double quotes. There are some differences in which quotes you use, and I’ll explain them later. But for now, we’ll use the double quotes.
