Basics of PHP

Written By: Ryan “boxdain” Macy

- 19 May 2006 -

Description: This intro to PHP covers syntax, variables, operators, condition statements, and an intro to arrays.

  1. Basic Syntax
  2. Variables and Operators
  3. Condition Statements
  4. Arrays

IF, ELSE, and ELSEIF statements

Sometimes you need a way to check to see if certain conditions are met to run a snippet of code. Here is the syntax:

IF (conditions) {
        code to execute
}

Here is an example:

$name = "Ryan";
if ($name == "Ryan") {
        print "Hello Ryan";
}

Here is what you should get:

Hello Ryan

Well that's all fine and dandy, but what if your name is not Ryan, and you want to perform a specfic action the condition is'nt met? This is where else comes into play:

$name = "Kevin";
if ($name == "Ryan") {
        print "Hello Ryan";
}else{
        print "Hey stranger";
}

You should get:

Hey stranger

Now what if you want to check through a line of conditions to see if any are met?

$name = "Kevin";
if ($name == "Ryan") {
        print "Hello Ryan";
}elseif ($name == "Kevin"){
        print "Hey Kevin";
}else{
        print "Hey stranger!";
}

You should get:

Hey Kevin

Well that's about it for if else and elseif statements, let's now go on to arrays!

<< Previous

Next >>