php Tutorials
Creating An RSS Feed With PHP And MySQL
Retrieving XML With Curl and SimpleXML
Using curl to Query Remote Servers
MySQL Basics
PHP mySQL Voting Booth
File Download Security
PHP Fundamentals

PHP Fundamentals

Simple Math

<? 
$a=1;         #Sets value of $a to 1
$b=3;         #Sets $b to 2
$c=$a+$b;     #Sets $c to the sum of $a and $b
$d=$a-$b;     #Subtracts $b from $a and stores it in $d
$e=$a*$b;     #$e becomes $a multiplied by $b
$f=$a/$b;     #Sets $f to $a divided by $b
$a++;         #Increments the value of $a
$b--;         #Subtracts 1 from the value of $b
$a+=$b;       #sets $a = $a + $b
$a-=$b;       #sets $a = $a - $b
$b*=$c;       #sets $b = $b * $c
$b/=$c;       #sets $b = $b / $c
 ?>

Control Structures

Comparison Operators

== - Equal values
=== - Equal values with equal variable types
!= - Not equal
> - Greater than
< - Less than
>= - Greater than or equal
<= - Less than or equal

if ... else

If...else statements are used to determine true or false. If the statement is true do this else do that.

<? 
$a=2;
if ($a&lt;3) {
  echo "True";
} else {
  echo "false";
}
 ?>

The above will echo True because two is less than three. If $a is changed to a number three or higher then false will be displayed.

Along with if and else this control structure has another element, elseif. elseif can be placed in the control structure to give more than two possible options. So if $a is negative do one thing if $a is 0 do another and if $a is positive do a third thing.

<? 
if ($a < 0) {
  echo "$a is negative";
} elseif ($a == 0) {
  echo "$a is 0";
} elseif ($a == 1) {
  echo "$a is 1";
} elseif ($a > 1) {
  echo "$a is greater than 1";
}
 ?>

elseif can be chained together for as many conditions as you like. If you are going to be using this to filter the value of one variable use the switch...case statements discussed on the next page.

Variables and Arrays <<  1 2 3 4 5 6  >> Switch ... Case Flow Control
New Content