A Brief First Tutorial on PHP by Jackline Hunter
In this brief tutorial, I'm going to show you how to get started with PHP.
PHP is a scripting language designed for creating web-based applications. It's also commonly used with MySQL, a database management system that accepts commands in a database language called SQL. PHP and MySQL are typically configured to run with a web server, most commonly the Apache web server. Apache, PHP, and MySQL are all free.
Now, onto the useful stuff:
First, we will setup a web server, on your computer, with PHP and MySQL using XAMPP. (Don't worry, this is absolutely painless!) Next, we will test to see if things are working by writing your first PHP script. I'll conclude by giving you an overview of your possible next steps.
Getting a Web Server Up and Running Using XAMPP.
If you use Windows, getting an Apache web server installed with PHP and MySQL support is as easy as downloading and installing XAMPP. Just click the download link and open the installer file. The rest you know already. ;)
(Even better: XAMPP is also supported for Linux, Solaris, and Mac. But, I'm assuming you're using Microsoft Windows for the duration of this tutorial. As long as you know how to change folders and rename files in your non-Windows operating system, you can follow along even if you're not using Windows.)
Your First PHP Script
Now that we have everything up and running, let's find out how to use it.
It's a common tradition amongst programmer's to start out one's first program in a new language with a "Hello World" application. The script simply displays "Hello World" with no user input. Let's try doing that!
- Open up Notepad or any plaintext editing software, and type in:
- The simplest way to get PHP to display something on-screen is by using the echo command. That's exactly what we've used!
- Note that there's a semicolon after "Hello World!". The semicolon is equivalent to a period in PHP. Proper "grammar" is essential for the PHP engine to understand what you want it to do. Every line of PHP code ends with a semicolon.
- The two slashes following the semicolon are not displayed on screen, but they appear in your code. It's one way to record a comment, or a programmer's-eyes-only memo, which is often indispensible in making your code readable to other people -- or even yourself.
- All PHP files end with an extension .php. However, PHP is configured to not run unless you surround your script with starting and ending wrappers. Start wrappers look like <?php, while end wrappers look like ?>.
- This is very convenient when you want to have your PHP code inline with HTML.
- Here's an example of how these start and end wrappers come into use. One way we can spice up your "Hello World!" is to display it in different font styles. Here, we'll make the output bold, italic, set in a giant red font:
Note that the HTML is not processed by the PHP engine, since the HTML tags are not wrapped between <?php and ?>.
- Now, save the file as hellow in C:/Program Files/XAMPP/htdocs. (I'm assuming you installed XAMPP in its default directory. If you installed it elsewhere, point it to the htdocs folder in wherever you installed XAMPP to.)
- If the file is saved as a text file with extension .txt, we'll have to rename it to .php. If you don't know how to rename files, here's one way to do it. If you use Windows:
- Go to Start > Run
- Type in command into the Run window that pops up. The DOS prompt should pop up.
- In the DOS prompt, navigate to the htdocs folder. Type in cd c:\progra~1\xampp\htdocs
- Assuming your file is called hellow.txt, type in rename hellow.txt hellow.php
- Fire up your favorite browser and load this address: http://localhost/hellow.php
- The "domain" localhost points to the loopback IP address 127.0.0.1, which is the "name" your computer uses when it's calling itself.
- By default, Apache loads port 80, which is XAMPP has connected to the htdocs folder. You can access any file in the htdocs folder by typing its name after http://localhost.
- And you're done!
Next Steps
So you now have a web server up and running with PHP support on your computer. But, you haven't quite made the neato server-side application you wanted to make. You can find tutorials all over the web on this stuff, or if you like learning from me, you can go onto my next tutorials to learn more about what you can do with PHP and MySQL.