Where do you start?!
Let's look at the basics to start with. Getting PHP to write things to the web browser, what variables are and how to use them...examples you can use in just about every web page you create.
Let’s start with the most basic one.
The PHP Echo command
We’re going to tell PHP to output something to the screen. Keep in mind that PHP can be used in conjunction with HTML, but we are not showing the code in this example, to keep it simple:
(I hate the “Hello World” sample that every other book in the world uses, so I’m using “Dolphin Community” instead.)
Let’s dissect the command example bit by bit, shall we? It helps to do this when you are looking at a LOT of PHP code...because, trust me, it can look like a jumbled blurry mess sometimes if you don’t take it piece by piece! Next page, please, if you will...
- tells the server to process this as php code...
echo “Dolphin Community” ; - tells the server to write what’s in the quotes to the screen, and that the semicolon is ending this particular command...
?> - tells the webserver, “OK, I’m done with PHP for now. Back to regular HTML”.
Pretty simple when you look at it that way, yes?
“OK, that’s cool..but what if I want to see quotes on my screen?”
This can be done by “escaping” the PHP code for what you want to show up in quotes. Let’s use the example from above...
If you wanted to see quotes around Dolphin Community, you would use the following code instead:
Using \” tells PHP that you want a quotation mark to appear. Remember this - we’re going to use it later!
Pull some Strings...
In PHP, as a general rule, a String is any line of text contained within quotation marks. You can use either double quotation marks (“) or single quotation marks also known as apostrophes (‘) in a string. Strings could be considered the building blocks of PHP, considering most all data is going to come from what’s in a string.
$double = "quotation marks.";
$single = 'single quotes.';
When using single quotes, you need to “escape” the apostrophe with the slash (just like you would with double quotation marks) if you wish to display it in the output text...
echo 'Let\'s start our own social network\'s with Boonex';
Special commands within strings...
There are some “secret commands” you can use within strings to manipulate the output text:
\n: makes a new line
\r: a carriage return
\t: a tab
\$: shows a dollar sign - remember PHP will be looking for a variable if you want to display a dollar sign and don’t use a slash...and throw an ugly error!
A Variable in PHP, simply put, is one thing that means another thing or things - a “container” if you will. It can represent text, numbers, calculations, and more.
Variables are quite powerful, and if you mess ‘em up, they’ll come get you in the middle of the night.
Declaring a variable is easy. No spaces in the variable name, please - PHP doesn’t like that...
$This_thing = “The Other Thing”;
Now before you go off saying “What the heck would I need THAT for?”, remember that variables are very useful...especially if you are PHP Include-ing other files (Like the foreshadowing there? Do ya?)
Real World Usage For Variables
Here’s an example of how you can use a variable in the real world: show the current date on your website.
$today = date("F j, Y");
This example sets the date command as a variable called “$today”, and uses echo to display it on the screen.
And now, for a quick tangent...
More about the “DATE” command - it is very versatile and flexible - see the guide below to use it to it’s potential!
SHAPE \* MERGEFORMAT
And now...back to Variables!
ECHOing more that one Variable at a time
You can use the ECHO command we learned earlier to display more than one variable at a time. Combining variables can be extremely useful. Take a look at this example...
$phrase1 = "That's No Moon,";
$phrase2 = "It's a Space Station!";
echo "$phrase1 $phrase2";
This code example will show up in your web browser like this:
So, you can see where this might be useful, I hope?
You can also echo text and variables in the same statement by putting periods around the variable....like so...
$items = 3;
echo "You have purchased ".$items." items.";