Welcome to the Introduction To PHP tutorial. In this tutorial we will discuss the basics of PHP and what it can and is used for. It is best you learn HTML before attempting these tutorials.
What is PHP?
Quoted from PHP's homepage, PHP.net, "PHP is an HTML-embedded scripting language. Much of its syntax is borrowed from C, Java and Perl with a couple of unique PHP-specific features thrown in. The goal of the language is to allow web developers to write dynamically generated pages quickly."
This is generally a good definition of PHP. However, it does contain a lot of terms you may not be used to. Another way to think of PHP is a powerful, behind the scenes scripting language that your visitors won't see!
When someone visits your PHP webpage, your web server processes the PHP code. It then sees which parts it needs to show to visitors (content and pictures) and hides the other stuff (file operations, math calculations, etc.). Then it translates your PHP into HTML. After the translation into HTML, it sends the webpage to your visitor's web browser.
What does PHP do?
It is also helpful to think of PHP in terms of what it can do for you. PHP will allow you to:

Reduce the time to create large websites.

Create a customized user experience for visitors based on information that you have gathered from them.

Open up thousands of possibilities for online tools.

Allow creation of shopping carts for e-commerce websites.
Writing in PHP
All PHP code must be contained with PHP Tags. These tags consist of <?php or <? (opening tags), code goes after that, then ending the tag, we have ?>.
For PHP to work, the file must have a .php file extension.
Your first code
Let's put some text onto a page. Open your text editor, and paste this in. It is okay to change the text, it will just display that text on the page.
<html> <head> <title>My First PHP Page</title> </head> <body> <?php echo "Hello World!"; ?> </body> </html>
Select what you want to copy and in doing so you will keep the formatting when pasting it. |
Save the file as helloworld.php and open it in your browser.
This displays:
Hello World!
Select what you want to copy and in doing so you will keep the formatting when pasting it. |
If you view the source, all you get is:
Hello World!
Select what you want to copy and in doing so you will keep the formatting when pasting it. |
Note: This is displayed in between all of the other HTML tags.
If it displays Hello World!, you just created your first PHP file!
The PHP echo function basically tells the browser to write whatever comes after echo in the quotes onto the page, and the semicolon ends the statement.
If it shows code, you may need to download and install PHP.
The semicolon!
As you may or may not have noticed in the above example, there was a semicolon after the line of PHP code. The semicolon signifies the end of a PHP statement and should never be forgotten. For example, if we repeated our "Hello World!" code several times, then we would need to place a semicolon at the end of each statement.
<html> <head> <title>My First PHP Page</title> </head> <body> <?php echo "Hello World! "; echo "Hello World! "; echo "Hello World! "; echo "Hello World! "; echo "Hello World! "; ?> </body> </html>
Select what you want to copy and in doing so you will keep the formatting when pasting it. |
This displays:
Hello World! Hello World! Hello World! Hello World! Hello World!
Select what you want to copy and in doing so you will keep the formatting when pasting it. |
Good job!
Whitespace (extra blank spaces) are ignored in HTML and PHP.
And that ends our PHP introduction!