Programming PHP: A Beginner`s Guide

PHP has been taking the web by storm because of its power and versatility. If you’d like to add PHP-based applications to your web site, keep reading. This series of articles will teach you the language from scratch.

 I’m going to start this article off by giving you two histories. It’s up to you to decide which is true.

The year 1968 gave birth to many great things: Johnny Cash’s album "Live at Folsom Prison", the comedy show Laugh-In, the Boeing 747, the musical Hair opens on Broadway, Cuba Gooding Jr., the great Gary Coleman, and his polar opposite, Rasmus Lerdorf.

Rasmus Lerdorf was born in a part of Greenland whose name I can’t pronounce, much less spell. Standing six feet tall by the age of one, the towering Greenlandic monster began working in the harsh arctic forests of Qeqertasuaq (yeah, you pronounce that). By the age of three, the eight foot tall tree clobberer was a legend, said to knock down trees with one mighty blow. At age five he was mistaken for a snow monster, and thus the legend of the Yeti, Big Foot’s really cold, mountainous cousin, was born.

Rasmus Lerdorf was born in a part of Greenland known as Qeqertarsuaq. He graduated fom the University of Waterloo in 1993 with a Bachelor’s of Applied Science and Systems Design Engineering. He presently works for Yahoo! Tired of being mistaken for a snow monster, he put his programming skills to work on his personal home page. He wanted to display his resume and collect data on how much traffic his pages were receiving. He had been using a set of Perl scripts for that task, but came up with something new: a set of Common Gateway Interface (CGI) binaries in the C programming language. These were publicly released on June 8, 1995 along with Lerdorf’s Form Interpreter as Personal Home Page Tools, the package becoming known as PHP/FI (or PHP version 2).

Programmers can never leave well enough alone of course, so when it started spreading, others put their two cents in. When Israeli programmers Zeev Suraski and Andi Gutmans got their hands on it in 1997, they rewrote the parser, came out with a new version and changed the language’s name. With the kind of recursive humor that only a true programmer could appreciate, they dubbed it  PHP: Hypertext Preprocessor. The official launch of PHP 3 came in June 1998. We’re now up to PHP 5, released in July 2004.

{mospagebreak title=PHP Means PHP: Hypertext Preprocessor}

Ow! That makes my brain hurt. How can the P in PHP stand for PHP? Doesn’t that create some infinite name like PHP: Hypertext Preprocessor PHP…bah you get the point. Its like that boxer George Foreman; you know the guy has been hit in the head too many times because all five of his children have the same exact name as him. I mean, even the things he invents have the same name. Don’t think that’s crazy? Call his house and ask to speak to George Foreman. See how long it takes to figure out the right one.

Before we begin, I am going to assume several things:


  • You know HTML

  • You have a modicum (yay big word) of scripting know how

  • You know what modicum means or can look it up in google

  • You have installed an Apache server, PHP, and MySQL on your computer

If you mentioned no to any of those (except the modicum one; you can skip that), then avert your beady eyes from my tutorial and go rectify the situation. If you answered yes to all of those, then come on in. Grab yourself a warm mug of hot cocoa and let us discuss the legacy of the first programming language created by a Yeti.

Syntax

When programming blocks of PHP, you must always start with <?php and end with ?>. This tells the interpreter that the line of code you have written is PHP. Remember when you right PHP, you will usually be intermingling it with HTML.

Most tutorials start you off with that dumb "Hello World" script. Forget that. We are l33t PHP h4xors now. We don’t want the world to know we exist. We are like ninjas. Or like that song from the Patrick Swayze movie: Like the Wind.

Does that guy ever wear a shirt by the way? [Does he need to? --Ed.]


<html>

<body>


<?php // This tells the browser you are using PHP


echo "Shhhh…don’t tell the world we are here h4xoring their bases";

?> // This ends your PHP block of code


</body>

</html>

The above would print the following to your monitor:

  Shhhh…don’t tell the world we are here h4xoring their bases

In the above code, the echo command tells the computer to print what follows it to the monitor. You can also use the print command.

You will also notice the semicolon (;). This is a separator, placed at the end of a statement, that tells the program that this line is finished. Basically, it keeps your instructions separate.

You might have wondered about the // in the above example. This is called commenting. When PHP sees the // it skips everything on that line after it. You use comments to leave notes to future programmers (or even yourself) about what this line or block of code was used for.

If you want to do a whole block of comments, you can do this:


<html>

<body>


<?php


/* I have a bunch of comments to make and am too lazy to

use those stupid // symbols

Blah blah blah

ti-blah

*/


?>


</body>

</html>

Anything between the /* and */ is ignored by the program.

{mospagebreak title=Variables}

The best way to think of a variable is to think of a box. You store things in them. You can put stuff in them, take things out, move them around, and so forth. If you are an angry UPS guy who has some computer parts we just ordered, you can slam it on the floor and kick it a few times while glaring at all of the employees. Then you can politely ask for the signature and storm out (you know who you are!).

Variables in PHP (and in programming languages in general) store data. Most programming languages are a real pain in the butt and make you define what type of data is being stored. Not PHP. In Soviet Russia, PHP tells you what type of data is in the variable!

(Note: A special thanks to the great world-renowned comedian Yakov Schmirnoff).

Here is how you work with variables:


<html>

<body>


<?php

$the_greatest = "James Payne";

echo $the_greatest;


?>


</body>

</html>

The above code store the string (or text) James Payne in a variable named $the_greatest. We then used echo and told it to print the value of $the_greatest. Which would result in:

  James Payne

A note about naming variables. They must begin with either a letter or an underscore(_) and can only contain letters, numbers, and underscores. Spaces are not allowed. If you wish to use a two or more word name, use either of these methods: $string_name or $StringName. And lastly, a $ must precede the name of the variable, to let PHP know you are using a variable.

{mospagebreak title=Working with Strings}

Now that we know how to print a string and create variables to hold strings, let’s look at some ways to manipulate strings.


<html>

<body>


<?php

$first_name = "James";

$last_name = "Payne";


echo $first_name . " " . $last_name; concatenation

?>


</body>

</html>

The above code is called concatenation and is used to join two things together (in this case, two words). The above code prints out the text:

  James Payne

You will note the " " places a space between the words.

There are a ton of functions that let you goof around with strings in PHP, and I will write a tutorial on them in the near future.

In addition to toying with strings, you can also manipulate numeric data as well. To do this we use operators. Below is a giant list of the different operators available to you in PHP.

 

Symbol

What it Does

Type

+

Used for Addition

Arithmetic

-

Used for Subtraction

Arithmetic

*

Used for Multiplication

Arithmetic

/

Used for Dividing

Arithmetic

%

Used for Modulation

Arithmetic

++

Used to increase value by one

Arithmetic

Used to decrease value by one

Arithmetic

"="

Used to Assign a Value

Comparison

+=

Used to Add and Assign a Value

Comparison

-=

Used to Subtract and Assign a Value

Comparison

*=

Used to Multiply and Assign a Value

Comparison

/=

Used to Divide and Assign a Value

Comparison

.=

Decimals and Adds a Value

Comparison

%=

Used to Modulate and Add a Value

Comparison

"=="

Equal To

Comparison

!=

Not Equal To

Comparison

>

Greater Than

Comparison

<

Less Than

Comparison

>=

Greater Than or Equal To

Comparison

<=

Less Than or Equal To

Comparison

&&

Checks for more than one criteria

Logical

||

Checks to see if at least one of a set of criteria is true

Logical

!

Checks to see if a criteria is not true

Logical

Well that’s all the time we have for this tutorial. In our next episode, we will discuss how to use the various operators to further manipulate our data. If we have time we discuss Conditional Statements as well.

Till then…

Google+ Comments

Google+ Comments