The road to being a PHP ninja, part 1: Introduction to PHP

So, what is PHP?

PHP

I’m sure you’ve heard of it (and that’s why you’re here), but you probably don’t know how useful and nifty PHP actually is. PHP is seen in pretty much every asset of the Internet, from Facebook to this blog, it’s everywhere. But why?

PHP is (to say the least) an amazing language for beginners and professionals alike. It’s a very dynamic and powerful, but simple. It’s also free and who doesn’t like free?

I love PHP, so my goal with these tutorials is to help anyone I can learn and write it. By the time you finish these sets of tutorials you’ll be a skilled PHP ninja and maybe even teaching it to someone else.

A place to settle down

Before we begin, we should find a place to actually write your code. PHP is often installed on servers with Apache or Lighttpd. For the sake of this tutorial, you can probably google “Free PHP host” and just test your code in there or use XAMPP (windows) or MAMP (OS X) to develop on your computer. I won’t guide you through this process, but it’s mostly step-by-step.

This series will be based on PHP 5.3 running on Apache in OS X.

Let’s dive on in

One of the things that makes PHP such an easy language for new comers is that you can (but that doesn’t mean you should) mix HTML and PHP. Since you’re a beginner, we’ll let you grasp that before plunging into higher concepts.

To define PHP code in a document, you simply begin with:

[php]
<?php
//Your PHP Code
?>
[/php]

Simple enough? Two things to note, however.

First, notice the two slashes on line 2, those are for commenting in PHP without the interpreter rendering them. It helps you keep track of your code as it grows.

Second, on line 1 we started with a < ?php. However, < ? is also an acceptable opener, but keep in mind this may be disabled on some servers so using the first method makes your coding much more portable.

Let’s see some PHP in action now:

[php]
<html>
<head>
<title>Oh yeah</title>
</head>
<body>
<em>You didn’t think it was gonna be that easy, did you?</em>
<br /><br />
<?php echo "You know, for a second there, yeah, I kinda did."; ?>
</body>
</html>
[/php]

Above is how you integrate PHP into an HTML document. Line 7 is the only new thing (assuming you know HTML, I hope) and we’ll talk about it below. First, let’s see what the HTML output will be:

[php]
<html>
<head>
<title>Oh yeah</title>
</head>
<body>
<em>You didn’t think it was gonna be that easy, did you?</em>
<br /><br />
You know, for a second there, yeah, I kinda did.
</body>
</html>
[/php]

So, what’s happening? You see the familiar < ?php followed by the word echo. echo() is a special function as it does not require parentheses for it’s options. However, the same line can be written a few different ways:

[php]
<?php echo("You know, for a second there, yeah, I kinda did."); ?>
<?php print("You know, for a second there, yeah, I kinda did."); ?>
[/php]

print() has the same function as echo, but requires parentheses.

Also worth noting is that the semi-colon at the end. A common mistake is to forget to include it which will cause PHP to throw an error. However, it’s worth noting that you do not need the semi-colon at the end of your PHP script. The example below is perfectly acceptable:

[php]
<?php echo "Revenge is a dish best served cold" ?>
[/php]

The Variable

Variables are not unique to PHP and even if you haven’t had any experience with them, they’re quick to catch on. Wikipedia describes a variable as:

In computer programming, a variable is a symbolic name given to some known or unknown quantity or information, for the purpose of allowing the name to be used independently of the information it represents. A variable name in computer source code is usually associated with a data storage location and thus also its contents, and these may change during the course of program execution.

In a shorter, more readable definition: a variable stores information that can be used later. Used for what? They can be used to store data that can be rendered to HTML or used for comparisons. Variables are the core to the flexibility of PHP.

Variables can be various things, including strings (text), integers (full numbers), point numbers, and arrays (a collection of data) but unlike other languages, PHP is smart with it’s variable type assigning; PHP can use it’s superpowers to determine what kind of variable you want without having to manually specify it.

A variable is just a name behind a dollar ($) sign. There are not many requirements for variable names other than they are case-sensitive and must start with a letter or underscore (meaning not a number), after that they can contain numbers, letters, and underscores. Examples are $world, $_framework, and $profile1. Variable like $42 or $53variable won’t work and will throw an error.

Let’s see an example that incorporates variables:

[php]
<html>
<head>
<title></title>
</head>
<body>
<?php
//Here we are defining variables

$bills_speech = "No Kiddo, at this moment, this is me at my most… masochistic.";
$kiddos_speech = "Bill… it’s your baby.";

echo "<strong>Bill:</strong> ".$bills_speech." <br /><br /><strong>Nameless:</strong> ".$kiddos_speech;
?>
</body>
</html>
[/php]

In this example we set up the variables by defining their name (e.g., $bills_speech) and their strings, separated by a equals (=). You can see them integrated into the echo() function by “.. The equals ends and starts the string while the period connects the variable. This is how you integrate strings with your variables, which can be very useful.

Defining variables of other types are just as easy:

[php]

// A string, which we saw before. A string can be defined with double
// or single quotes and strings. Use a backslash to allow quotes inside
// of another quote.

$string = "The Bride: "Wiggle your big toe."";

//Integers are full numbers defined without quotes:
$integer = 42;

//Floating points are the same, with a decimal:
$float = 3.14;

//And defining boolean values can be done by putting true or false:
$boolean = true;
[/php]

Working with strings

Working with strings is a very common thing to do. Strings are dynamic and contain a lot of information. Remember how you used the period (“.“) above to stick a variable to an existing string? Well, to combine variables you can use the exact same thing.

[php]
<?php
$tagline = "Gotta catch em all! <br />";
$endline = "POKEMON!";

$tagline = $tagline.$endline; //Echoed: "Gotta catch em all <br />POKEMON!"

//You can condense this even further. Below represents the exact same thing:
$tagline .= $endline;
?>
[/php]

Wrapping it all up

This is it for this tutorial. Hopefully after working with this tutorial you have a grasp on some of the contexts of PHP. In the next we will be looking at applying these concepts to really see how amazing PHP is and what you can do with it.