Are you interested in learning how to code PHP scripts? It’s very interesting and rewarding. Besides, half of the servers on the internet use PHP, so it’s a wise choice.
What is PHP?
PHP is a scripting language developed by Rasmus Lerdorf. He created PHP back in the 1990s to compete with HTML web pages. He soon discovered there was much more PHP could do and bridges HTML and database functionality. Your HTML web pages cannot login people with an account easily without the help of PHP. To read more about PHP, go straight to the source, the PHP home page.
Where is PHP?
Well, it’s just about everywhere you go on the internet. Facebook is mostly PHP, WordPress is written in PHP, most websites that are database-driven are usually PHP. It’s being used heavily and it is a hot language to learn. Let’s see some PHP in action.
Create a PHP file
You will have to install PHP onto your server or workstation. I install it onto Linux LAMP servers where I will be running web sites, but you could install it onto your Windows machine using WAMP, or onto a MAC using MAMP. It is beyond the scope of this post to install and configure LAMP, MAMP or WAMP. Perhaps another post if anyone is interested. Comment below you want to see that and I will get on it for you!
Assuming you have a *AMP server already setup, you would navigate to a web server you have already created and confirmed working, then create the following file: hello.php Normally you would use an IDE (integrated Developers Environment) but you could use most text editors to create a simple PHP file. Here are directions if you are confused:
- Navigate to a web directory on your web server
- Create a file called hello.php. If you already have a file called hello.php, use a different file name.
- Open the hello.php file and insert this code:
<?php echo 'Hello'; ?>
NOTE – the closing ?> is not necessary if you end this file with PHP and have no other languages like HTML following it.
- Now save what you typed or copied in, usually with CONTROL+S or SAVE from your menu.
That’s all there is to a basic PHP script.
Explain The PHP Script
Let me explain the above PHP script. First, you created a file called hello.php. It was empty but you typed or copied in the code I showed you. Then you saved it so you would not lose it.
The very first line of a PHP script must contain the characters <?php and it must be fully left justified. Make sure there are no spaces before the <?php or the script may not run, and if it does, it will be flaky.
I added a few empty lines for space to help readability. You could take out the space and just add code to every line if you so desired. For short scripts, I often use space for readability, but long 500-liners, I take out a lot of space or I would scroll for eternity while working or troubleshooting the code.
The first stanza after my <?php declaration is echo ‘Hello’;. Echo is the command followed by two apostrophes, often called single ticks. You can also use the quote symbol, “ which is often called double-ticks. These are iterators that will display any text that is in between them. In this case, Hello would be displayed. The major difference between single tick and double-ticks is this:
- Single ticks – will display exactly what you wrote and will not resolve variables.
- Double-ticks – will display everything you wrote plus it will resolve all variables you included.
I closed the script with ?>. Note that you do not have to end the script with the ?> if there is nothing else in the file, but if you were going to continue the file with perhaps some HTML close, then you would need to code ?> to tell the code this is the end of PHP so that some other code could be inserted next.
Run Your PHP Script
Place your PHP scripts in any directory under a website you have created, then point your browser at the file, for example, type thsi into your browser:
You should see Hello displayed in an all white screen. The HTML designed in you would now doctor that up into something pretty, but before you go that far, you got the test a short PHP script to see it work.
After that the sky is the limit. You will want to learn about variables, functions, classes, and so much more than I can put in this post. But I will be expanding on this for people who wish to learn more about coding PHP! Just follow the blog and comment so I know you are interested.
TERMS I USED IN THIS POST
PHP – A scripting language by Rasmus Lerdorf
LAMP – Linux, Apache, MySQL, PHP
MAMP – Mac, Aapache, MySQL, PHP
WAMP – Windows, Apache, MySQL, PHP