This is a discussion on PHP Control Structures within the PHP Programming forums, part of the Web Development category; Hi PHP script is built out of a series of statements. A statement can be an assignment, a function call, ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| Hi PHP script is built out of a series of statements. A statement can be an assignment, a function call, a loop, a conditional statement of even a statement that does nothing (an empty statement). Statements usually end with a semicolon. In addition, statements can be grouped into a statement-group by encapsulating a group of statements with curly braces. A statement-group is a statement by itself as well. The various statement types are described in this chapter. Regards Falcon ![]() |
| Sponsored Links |
| |||
| Hi Types of the Control Structures in PHP is given bellow * ifRegards Falcon ![]() |
| |||
| Hi, Good topic to discuss. I think we can divide control structures into two main area as conditional statements and control loops
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| Conditional Statements allow you to branch the path of execution in a script based on whether a single, or multiple conditions, evaluate to true or false. Put simply, they let you test things and perform various actions based on the results. Frequently in PHP there are instances where you need to perform repetitive tasks, such as formatting data pulled from a database, sending out emails to a mailing list, or cycling through the contents of an array. Control Loops allow you to perform these tasks almost effortlessly.
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| Hi, PHP Control Structures Statement if (expr) statement elseif (expr) statement elseif (expr) statement ... else statement if Statements are the most common conditional constructs, and they exist in most programming languages. The expression in the if statement is referred to as the truth expression. If the truth expression evaluates to true, the statement or statement list following it are executed; otherwise, they’re not. You can add an else branch to an if statement to execute code only if all the truth expressions in the if statement evaluated to false: PHP Code: which make these statements a statement block. In this particular case, you can omit the braces because both blocks contain only one statement in them. It is good practice to write these braces even if they’re not syntactically required. Doing so improves readability, and it’s easier to add more statements to the if block later (for example, during debugging). The elseif construct can be used to conduct a series of conditional checks and only execute the code following the first condition that is met.
__________________ Regards, Senraj.A Last edited by senraj : 05-08-2008 at 03:01 AM. |
| |||
| Hi, For example: PHP Code: negative and not zero, it must be positive. Both styles of the if construct behave in the same way. While the statement style is probably more readable and convenient for use inside PHP code blocks, the statement list style extends readability when used to conditionally display HTML blocks. Here’s an alternative way to implement the previous example using HTML blocks instead of print: HTML Code: <?php if ($num < 0): ?> <h1>$num is negative</h1> <?php elseif($num == 0): ?> <h1>$num is zero</h1> <?php elseif($num > 0): ?> <h1>$num is positive</h1> <?php endif; ?> Here, only one of the HTML blocks are displayed, depending on the value of $num.
__________________ Regards, Senraj.A |
| |||
| Hi, switch Statements Statement switch (expr){ case expr: statement list case expr: statement list ... default: } You can use the switch construct to elegantly replace certain lengthy if/elseif constructs. It is given an expression and compares it to all possible case expressions listed in its body. When here’s a successful match, the following code is executed, ignoring any further case lines (execution does not stop when the next case is reached). The match is done internally using the regular equality operator (==), not the identical operator (===). You can use the break statement to end execution and skip to the code following the switch construct. Usually, break statements appear at the end of a case statement list, lthough it is not mandatory. If no case expression is met and the switch construct contains default, the default statement list is executed. Note that the default
__________________ Regards, Senraj.A Last edited by senraj : 05-09-2008 at 04:34 AM. |
| |||
| Hi, switch Statements case must appear last in the list of cases or not appear at all: PHP Code:
__________________ Regards, Senraj.A |
| |||
| The switch construct is generally more than twice as fast at matching a simple integer within a single large set of conditions. There is largely little difference between the time it takes to find a condition using if,if statements or if-elseif statements. Also the size of the resulting scripts were only negligibly different compared to the difference in execution time. At first I thought that since the switch statement breaks execution after it matches its value especially since I was using random numbers, that this could account for the time difference, but that also had no bearing. Setting $var=999999 in the below statements had little effect on the faster execution time of the switch statement.
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| Conditional Statements:- Conditional Statements allow you to branch the path of execution in a script based on whether a single, or multiple conditions, evaluate to true or false. Put simply, they let you test things and perform various actions based on the results. |
| |||
| The switch construct is generally more than twice as fast at matching a simple integer within a single large set of conditions.
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| There is largely little difference between the time it takes to find a condition using if,if statements or if-elseif statements. Also the size of the resulting scripts were only negligibly different compared to the difference in execution time.
__________________ With, J. Jeyaseelan Everything Possible |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| use of Custom Control and User Control? | a.deeban | ASP and ASP.NET Programming | 1 | 08-20-2007 07:25 AM |
| looping structures in Ruby? | vadivelanvaidyanathan | Ruby | 1 | 08-18-2007 12:53 AM |
| How can do test the Pointers & Structures? | sundarraja | Software Testing | 1 | 07-30-2007 09:54 PM |
| What is the difference between structures and enumeration? | anbuchezhians | VB.NET Programming | 1 | 07-28-2007 12:50 AM |
| Difference of Structures and Classes | vigneshgets | C and C++ Programming | 1 | 05-24-2007 10:53 AM |