What is PHP?
- PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML.
- PHP is probably the most popular scripting language on the web. It is used to enhance web pages.
- With PHP, you can do things like create username and password login pages, check details from a form, create forums, picture galleries, surveys, and a whole lot more.
- If you've come across a web page that ends in PHP, then the author has written some programming code to liven up the plain, old HTML.
- PHP is known as a server-sided language.
- That's because the PHP doesn't get executed on your computer, but on the computer you requested the page from.
CONTROL STATEMENTS
- PHP supports a number of traditional programming constructs for controlling the flow of execution of a program.
- Conditional statements, such as if/else and switch, allow a program to execute different pieces of code, or none at all, depending on some condition.
- Loops, such as while and for, support the repeated execution of particular code.
If Statement
The if statement
checks the truthfulness of an expression and, if the expression is true,
evaluates a statement.
An if statement looks like: Syntax:
if (expression)
statement
For example:
if ($user_validated)
echo
"Welcome!";
If ... Else Statement
- The If Statement is a way to make decisions based upon the result of a condition. For example, you might have a script that checks if boolean value is true or false, if variable contains number or string value, if an object is empty or populated, etc.
- The condition can be anything you choose, and you can combine conditions together to make for actions that are more complicated.
- Use the if statement to execute a statement if a logical condition is true. Use the optional else clause to execute a statement if the condition is false.
The syntax for If statement looks as follows:
if (condition) {
statements_1 } else { statements_2 }
For example:
$result = 70;
if ($result >= 57) { echo "Pass "; } else { echo "Fail "; } ?>
Else ......If Statement
|
- Next example use the elseif variant on the if statement. This allows us to test for other conditions if the first one wasn't true.
- The program will test each condition in sequence until:
- It finds one that is true. In this case it executes the code for that condition.
- It reaches an else statement. In which case it executes the code in the else statement.
- It reaches the end of the if ... elseif ... else structure. In this case it moves to the next statement after the conditional structure.
For example:
$result = 70;
if ($result >= 75) {
echo "Passed: Grade A
";
}
elseif ($result >= 60) {
echo "Passed: Grade B
";
}
elseif ($result >= 45) {
echo "Passed: Grade C
";
}
else {
echo "Failed
";
}
?>
if ($result >= 75) {
echo "Passed: Grade A
";
}
elseif ($result >= 60) {
echo "Passed: Grade B
";
}
elseif ($result >= 45) {
echo "Passed: Grade C
";
}
else {
echo "Failed
";
}
?>
Switch Statement
- Switch statements work the same as if statements.
- However the difference is that they can check for multiple values.
- A switch statement allows a program to evaluate an expression and attempt to match the expression's value to a case label.
- If a match is found, the program executes the associated statement.
switch (expression) {
case label_1: statements_1 [break;] case label_2: statements_2 [break;] ... default: statements_n [break;] } |
- The program first looks for a case clause with a label matching the value of expression and then transfers control to that clause, executing the associated statements.
- If no matching label is found, the program looks for the optional default clause, and if found, transfers control to that clause, executing the associated statements.
- If no default clause is found, the program continues execution at the statement following the end of switch.
- Use break to prevent the code from running into the next case automatically.
Let's consider an example:
$flower = "rose";
switch ($flower) { case "rose" : echo $flower." costs $2.50"; break; case "daisy" : echo $flower." costs $1.25"; break; case "orchild" : echo $flower." costs $1.50"; break; default : echo "There is no such flower in our shop"; break; } ?>
|
No comments:
Post a Comment