Your Ad Here

 Navigation

Home
Source code
Tutorials
RSS feeds
Articles
Books
Software
Downloads
Hosting
manuals
Forums
Script directory
Training
Our Links

 Sponsors
 Links
dvd information
finance products
programmershelp
 Downloads
 Misc

Webmaster Resources
Only PHP
ScriptSearch.com
Scripts.com - Get the best scripts NOW!
AndreaPHP Programming



The if Statement


 

Conditional structures are used to control which statements get executed. They are composed of three fundamental elements:

* if statements;
* elseif statements; and
* else statements.

Conditionals in PHP are structured similarly to those found in C++ and Java. The structure begins with an if clause, which is composed of the word "if" followed by a true/false statement in parentheses ( ). The subsequent code will be contained in a block, denoted by curly braces { }. Sometimes the braces are omitted, and only one line will follow the if statement. elseif and else clauses sometimes occur after the if clause, to test for different statements.

The if clause says "If this statement is true, I want the program to execute the following statements. If it is false, then ignore these statements." In technical terms, it works like this: When an if statement is encountered, the true/false statement in parentheses is evaluated. If the statement is found to be true, the subsequent block of code contained in curly braces is executed. However, if the statement is found to be false, the program skips those lines and executes the next non-blank line.

Following the if clause are two optional clauses: else and elseif. The elseif clause says "If the last statement was false, let's see if this statement is true. If it is, execute the following code. If it isn't, then skip it." elseif statements are only evaluated when the preceding if statement comes out to be false. Otherwise they are skipped. Other than that, the elseif clause works just like a regular if clause. If it is true, its block is executed, if not, its block is skipped.

Finally, the else clause serves as a "catch-all" for an if statement. Essentially the else statement says "If all of the preceding tests fail, then execute this code."

Example 1

<?php
$foo = 1;
$bar = 2;
if($foo == $bar) {
echo "$foo is equal to $bar.";
} elseif ($foo > $bar) {
echo "$foo is greater than $bar.";
} else {
echo "$foo is less than $bar.";
} ?>

Example 2

<?php
$lower = 10;
$upper = 100;
$needle = 25;
if(($needle >= $lower) && ($needle <= $upper)) {
echo "The needle is in the haystack.";
} else if(($needle <= $lower) || ($needle >= $upper)) {
echo "The needle is outside of the haystack.";
} ?>

Conditional Expressions

Conditional Values function via basic formal logic. It is important to understand how the if clause, among other clauses, evaluates these conditional values.

It is easiest to examine such with boolean values in mind, meaning that the result of a conditional value will be either TRUE or FALSE and not both. For example, if variable $x = 4, and a conditional structure is called with the expression if($x == 4), then the result of the expression will be TRUE, and the if structure will execute. However, if the expression is ($x == 0), then the result will be FALSE, and the code will not execute. This is simple enough.

This becomes more complicated when complex expressions are considered. The two basic operators that expressions can be conjoined with are the AND (&&) and OR (||).

Examples

We are given variables $x and $y.

$x = 4;
$y = 8;

Given the complex expression:

($x == 4 AND $y == 8)

We are given a result of TRUE, because the result of both seperate expressions are true. When expressions are joined with the AND operator, both sides MUST be true for the whole expression to be true.
Similarly:

($x == 4 OR $y == 8)

We are given a result of TRUE as well, because at least one expression is true. When expressions are joined with the OR operator, at least one side MUST be true for the whole expression to be true.
Conversely,

($x == 4 AND $y == 10)

This expression will return FALSE, because at least one expression in the whole is false.
However,

($x == 4 OR $y == 10)

This expression will return TRUE, because at least one expression in the whole is true.

Code Blocks

A code block is one or more statements or commands that are contained between a pair of curly braces { }. Blocks are used primarily in loops, conditionals and functions. Blocks can be nested inside one another, for instance as an if structure inside of a loop inside of a function.

If, after one of the conditional statements, there is no block of code enclosed by curly braces, only the next statement will be executed. It is recommended that you avoid using this this to help prevent accidents when adding extra code after the block.
The following code will not work as intended:

if(FALSE)
echo 'FALSE evaluates to true.';
echo 'Who knew that FALSE was TRUE?';

The second echo statement was executed, despite the if clause. The lack of brackets caused the if statement to only apply to the first statement, making the second statement evaluate regardless of the outcome of the if statement.

To avoid this problem, make sure to use brackets with conditional statements, even if there is only a single line of code to be executed. This prevents the error in the above code from occurring when you add an extra line after the existing block.
This code fixes the previous bug.

if(FALSE)
{
echo 'FALSE evaluates to true.';
echo 'Who knew that FALSE was TRUE?';
}

The second echo statement should never be executed in this snippet.

 

All text is available under the terms of the GNU Free Documentation License
Source : Wikibooks




Books
 Sponsors
 Random Code
hexadecimal to decimal conversion(math)

getservbyport function returns the name of the service that uses a specified port(functions)

an explode function example(functions)

 Random Article
  Network

Programming resources
Tutorials directory
General bid directory
sell software, make money
Find me wholesalers
Find me dropshippers
Free recipes online/a>
UK products and price comparison
Anime videos
Free lyrics search
free stuff
UK stores
Gambling directory
Sexy free wallpapers
Hosting resources
ASP site
Golf resources
iPod resources
Coupons and deals
Baby names
Domain names
Dating site
Scripts directory
Maxi directory
bigarticle : free articles
dawgwitch search
List of directories






beginners PHP Copyright © 2004 onwards by beginnersPHP.