Checked At:05:36
4 Guests
0 Users
Online doing the past 15 Minutes!
brugbart.com - Edition/Last Updated: 13. October 2008
Posted The: 09/05/2008 - AT: 13:17
Edited The: 25/10/2008 - AT: 20:50
If statements are quite useful in php, and when creating dynamic websites. Brugbart takes heavy advantage of "If Checks".
Alternative titles: if then else in php, php if then statement, if then else php, php if then else
In this example I'm going to use the rand function, to create a "random" number between 1 and 2.
If the number is 1, we will echo "The number was 1", and accordingly "The number was 2" if that was the case.
<?php
$RandomNumber = rand(1, 2);
if ($RandomNumber == '1') {
echo 'The number was 1';
} Else {
echo 'The number was 2';
}
?>All I'm really doing in above example, is checking if the number was "1", if it wasn't we can safely assume it was "2".
In the next example, i use the same approach, but using 4 cases instead.
<?php
$RandomNumber = rand(1, 4);
if ($RandomNumber == '1') {
echo 'The number was 1';
} ElseIf ($RandomNumber == '2') {
echo 'The number was 2';
} ElseIf ($RandomNumber == '3') {
echo 'The number was 3';
} Else {
echo 'The number was 4';
}
?>This was primarily to show how if statements work, the above would easily, and much faster, be done simply by echoing the variable.
<?php $RandomNumber = rand(1, 4); echo "The number was $RandomNumber"; ?>
Note how i used Double Quotes instead of Single Quotes, this will make php check the string for variables. Generally it is faster to use single quotes, therefor always use single quotes if you know the string doesn't contain variables.
Finally you can combine checks by using the "&" operator.
<?php
$DB_HashedPassword = 'Test';
$HashedPassword = 'Test';
$UserName = 'BlueBoden';
if (($UserName == 'BlueBoden')
&& ($DB_HashedPassword == $HashedPassword)) { echo 'Welcome User!'; }
else { echo 'Denied!'; }
?>The above would be useful when checking if a user was logged in.
Author: BlueBoden
Comments: [0]


Checked At:05:36
4 Guests
0 Users
Online doing the past 15 Minutes!
This page was created in 0.128071069717 seconds
Welcome Guest