PHP is a scripting language. It first appeared in 1995 so it’s not new yet not old. The reason PHP is good to know is because it was specifically designed for web development, whereas other languages are not. This is a big reason why PHP runs so fast on the internet.
Why know PHP?
The vast majority of blogs and forums are programmed using PHP. Knowing it will allow you to “hack your own” modifications.
Below are some very simple examples of how PHP can work. In order to try these for yourself, you will need the following:
1. A text editor.
Notepad is fine, but I suggest using Notepad++ (free) because it has automatic color coding whereas Notepad is just plain black-and-white.
2. An environment to run PHP scripts.
You can do this in several ways.
- Have your own hosted web site? It most likely has PHP already installed. Save your test file as test.php, upload to your site via FTP, then load in your browser.
- Physically installing PHP on your computer and running locally.
- Using www.phpsandbox.net (this is the easiest to use)
Note on phpsandbox: Any script you run in there will be shown at the very bottom of the site once you run it.
How all PHP scripts start and end
A PHP script starts with:
<?php
..and end with
?>
This is always required, else the script no matter what it is won’t work.
A site to remember for everything PHP?
Everything you ever wanted to know about PHP is there. However for beginners it may be confusing to navigate, so hopefully after seeing a few examples here you’ll have a very basic understanding of how PHP works.
The classic “Hello World!” script
In all programming languages, the first script everybody does is what’s known as a “Hello World!” All this does is show on screen the phrase Hello World! so you know your script it working and that the language installed is working properly.
This is the “Hello World!” script:
echo ("Hello World!");
On run, all this will do is output Hello World! This is what echo does.
Remember that <?php must be before it and ?> must be after it for the script to work.
Echoing the date
Date can be used in combination with echo to show the server’s current date and/or time. For the letters used with date below, all of them are explained on php.net’s date page.
Examples:
echo date("r");
The above outputs an RFC 2822 formatted date. It’s a “one-for-all” output that includes the weekday, day of month, month, year, time and time zone.
echo date("m/d/Y");
The above outputs two-digit month, day of month and four-digit year. Note that the case of the letters matters. If for the year you used y instead of Y, the year would be two-digit and not four.
echo date("l, F j Y g:ia");
The above is a very fancy date output. What is outputs is this:
Long-format day of week (lowercase “l”), Long-format month (F), day of month without leading zeroes (j), four-digit year (Y), hour in 12-hour format (g), minutes with leading zeroes (i), am/pm output in lowercase letters (a).
Remember that date outputs the server’s time, not the time on your computer.
Using variables
Variables in PHP start with the dollar sign ($), followed by a name of your choosing, then the equals sign, then a value.
Example:
$apple = '5'; echo $apple;
If you run the above script, all that will be output is 5, because $apple is 5.
Now we’ll use some very basic math to add two variables together.
$apple = '5'; $orange = '2'; echo $apple + $orange;
The above script when run will output 7, because 5 + 2 is 7.
Some quick questions answered
Why is there a semi-colon at the end of each line?
This is how PHP knows where the end of a line of code is and where a new one begins. You can consider it the same way the English language is structured. We know a sentence stops when we see a period. PHP knows a line of code stops when it sees a semi-colon.
Lines of code do not have to be on separate lines. For example, you could have:
echo ("Hello World!"); echo date("r");
This would in fact work, but note the semi-colons. They have to be there so PHP knows how go to from one line to the next.
It is highly recommended that when writing code to purposely separate lines, else you can “get lost” easily.
I sometimes see PHP code with or without, double-quotes, single quotes and parentheses. Does it matter which you use?
No. What matters is how it’s used.
These three lines do the exact same thing:
echo ('Hello World!');
echo ("Hello World!");
echo 'Hello World!';
Where the differences come in depends on what you want to output. For example, if you wanted to output something in quotation marks, you need to use a different way to output the phrase.
Let’s say you want to output this:
She said to me, “Meet me at 9pm” while I was speaking with her on the phone.
There are quotation marks within this phrase, so you can’t use those quotation marks to start/end the output. Instead you’ll have to use a single quotation mark, like this:
Like this:
echo 'She said to me, "Meet me at 9pm" while I was speaking with her on the phone.';
The above will output correctly.
Parentheses are optional most of the time. I personally recommend using them because it gives a better visual cue of what’s going on in your scripts when editing them.
Leave A Reply (No comments So Far)
You must be logged in to post a comment.
No comments yet