Simple PHP Part 2
Author: | Ferg
|
Submitted: | 9th July, 2002
|
Views: | 4785
| Rated: |
|
|
Part 2 of the PHP tutorials following on from Part 1. You should read part one first.
If Conditionals
If conditionals are in the form:
If something is true then do something
Type the following in your text editor:
<?php
$value = 1;
if ($value == "1") {
echo "TRUE";
}
?>
Line one starts the PHP code. Line two assigns the value 1 to $value. The third line means that if the value of $value is equal to 1 then execute the code between the { and } characters. The == means equal to. Since the value of $value is 1 the code is executed and the word TRUE is printed to the screen.
Using else
The else statement is used with the if conditional. It is in the form:
If something is true do something, else do something else
Type the following in your text editor:
<?php
$value = 0;
if ($value == "1") {
echo "TRUE";
} else {
echo "FALSE";
}
?>
The first line starts the PHP code. The second line assigns the value 0 to the variable $value. The third line means that if the value of $value equals 1 then print the word TRUE to the screen. The part:
else {
echo "FALSE"
}
means that if the value of $value is not equal to 1 then the word FALSE will be printed to screen.
You have now learned how to use if and else statements. Here is a reminder of how to use them:
<?php
if ($variable == "something") {
execute this PHP code
} else {
execute this PHP code
}
?>
I have only shown you examples here of if something equals something. You can also have if something is not equal to somthing or if something is > than something. Here is a list of operators you can use instead of the ==
== equal to
!= not equal to
>= greater than or equal to
<= less than or equal to
> greater than
< less than
Elseif
You can use if and else statements so now I will show you elseif statements. These are used in the form:
if something is true do something elseif something else is true do something else.
Type the following in your text editor:
<?php
$value1 = 1;
$value2 = 0;
if ($value1 == "0") {
echo "0";
} elseif ($value2 == "0") {
echo "0";
} else {
echo "Neither value is 0";
}
?>
The new part here is the elseif line of code. Since $value1 is not equal to 0 then the first if statement is false and 0 is not printed. The elseif statement here menas that if $value2 is equal to 0 then 0 will be printed to the screen. Since $value2is equal to 0 then 0 will be printed to the screen. But, if $value2 was also not equal to zero then the elseif statement would also be false meaning that the else statement would then be executed. If this were the case then Neither value is 0 would be printed to the screen.
Using AND and OR
If you only want some PHP code to execute as long as something is true AND something else is true you can do the following. Type this in your text editor:
<?php
$value1 = 1;
$value2 = 0;
if (($value1 == "1") AND ($value2 == "0")) {
echo "My first AND statement";
}
?>
Basically, the only new thing here is the word AND in the if statement. What this basically means is that if $value1 equals 1 AND $value2 equals 0 then the PHP code will be executed.
Similarly you can use OR too. Here is an example of using OR:
<?php
$value1 = 1;
$value2 = 0;
if (($value1 == "1") OR ($value2 == "0")) {
echo "My first AND statement";
}
?>
This basically means that if $value1 is equal to 1 OR $value2 is equal to 0 then execute the PHP code.
I did say I would cover while, for and switch in this part but because of how long it took me to explain the if statement I though I'd cover those in the next part. Happy coding!
Ferg
|
|
FergReincarnation Registered 14/05/2002
Points 131
|