IT Community - Software Programming, Web Development and Technical Support

PHP Control Structures

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, ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > PHP Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 05-07-2008, 03:37 AM
Falcon Falcon is offline
D-Web Analyst
 
Join Date: Nov 2007
Location: Chennai
Posts: 288
Falcon is on a distinguished road
Default PHP Control Structures

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-07-2008, 03:42 AM
Falcon Falcon is offline
D-Web Analyst
 
Join Date: Nov 2007
Location: Chennai
Posts: 288
Falcon is on a distinguished road
Default Re: PHP Control Structures

Hi

Types of the Control Structures in PHP is given bellow
* if
* else
* elseif
* Alternative syntax for control structures
* while
* do-while
* for
* foreach
* break
* continue
* switch
* declare
* return
* require
* include
* require_once
* include_once
Regards
Falcon
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-07-2008, 11:36 PM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,158
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: PHP Control Structures

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 05-08-2008, 12:22 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,158
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: PHP Control Structures

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 05-08-2008, 02:52 AM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Re: PHP Control Structures

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:
if ($var >= 50
{
print 
'$var is in range';
} else 
{
print 
'$var is invalid';

Notice the braces that delimit the statements following if and else,
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.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 05-08-2008, 02:54 AM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Re: PHP Control Structures

Hi,

For example:
PHP Code:
if ($num 0
{
print 
'$num is negative';

elseif (
$num == 0
{
print 
'$num is zero';

elseif (
$num 0
{
print 
'$num is positive';

The last elseif could be substituted with an else because, if $num is not
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; ?>
As you can see, HTML blocks can be used just like any other statement.
Here, only one of the HTML blocks are displayed, depending on the value of
$num.
__________________
Regards,
Senraj.A
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 05-09-2008, 04:30 AM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Re: PHP Control Structures

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.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 05-09-2008, 04:32 AM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Re: PHP Control Structures

Hi,

switch Statements

case must appear last in the list of cases or not appear at all:

PHP Code:
switch ($answer) {
case 
'y':
case 
'Y':
print 
"The answer was yes\n";
break;
case 
'n':
case 
'N':
print 
"The answer was no\n";
break;
default:
print 
"Error: $answer is not a valid answer\n";
break;

__________________
Regards,
Senraj.A
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 05-14-2008, 11:37 PM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,158
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: PHP Control Structures

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 05-14-2008, 11:39 PM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,158
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: PHP Control Structures

Hi,
Which is best use between IF and SWITCH case in the program?
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #11 (permalink)  
Old 05-29-2008, 01:25 AM
naina naina is offline
D-Web Trainee
 
Join Date: May 2008
Posts: 8
naina is on a distinguished road
Default Re: PHP Control Structures

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.
__________________
Web Templetes
Web site Design
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #12 (permalink)  
Old 07-02-2008, 12:16 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,158
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: PHP Control Structures

Quote:
Originally Posted by Jeyaseelansarc View Post
Hi,
Which is best use between IF and SWITCH case in the program?
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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #13 (permalink)  
Old 07-02-2008, 12:17 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,158
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: PHP Control Structures

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT -7. The time now is 10:02 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0