Home arrow PHP arrow Page 6 - Sample Chapter: PHP Pocket Reference
PHP

Sample Chapter: PHP Pocket Reference


Never worked with PHP? Curious? In this article Tim takes a look at a sample chapter from O'Reillys PHP Pocket Reference. This chapter is a great introduction to PHP and is explained simply and clearly.

Author Info:
By: Tim Pabst
Rating: 5 stars5 stars5 stars5 stars5 stars / 7
June 15, 2002
TABLE OF CONTENTS:
  1. · Sample Chapter: PHP Pocket Reference
  2. · Introduction
  3. · Installation and Configuration
  4. · Including Files
  5. · Arrays
  6. · Control Structures
  7. · Functions
  8. · Showing the Browser and IP Address
  9. · Web Database Integration
  10. · Conclusion

print this article
SEARCH DEVARTICLES

Sample Chapter: PHP Pocket Reference - Control Structures
(Page 6 of 10 )

The control structures in PHP are very similar to those used by the C language. Control structures are used to control the logical flow through a PHP script. PHP's control structures have two syntaxes that can be used interchangeably. The first form uses C-style curly braces to enclose statement blocks, while the second style uses a more verbose syntax that includes explicit ending statements. The first style is preferable when the control structure is completely within a PHP code block. The second style is useful when the construct spans a large section of intermixed code and HTML. The two styles are completely interchangeable, however, so it is really a matter of personal preference which one you use.

if

The if statement is a standard conditional found in most languages. Here are the two syntaxes for the if statement:

if(expr) {
statements
}
elseif(expr) {
statements
}
else {
statements
}

if(expr):
statements
elseif(expr):
statements
else:
statements
endif;


The if statement causes particular code to be executed if the expression it acts on is true. With the first form, you can omit the braces if you only need to execute a single statement.

switch
The switch statement can be used in place of a lengthy if statement. Here are the two syntaxes for switch:

switch(expr) {
case expr:
statements
break;
default:
statements
break;
}

switch(expr):
case expr:
statements
break;
default:
statements
break;
endswitch;


The expression for each case statement is compared against the switch expression and, if they match, the code following that particular case is executed. The break keyword signals the end of a particular case; it may be omitted, which causes control to flow into the next case. If none of the case expressions match the switch expression, the default case is executed.

while

The while statement is a looping construct that repeatedly executes some code while a particular expression is true:

while(expr) {
statements
}

while(expr):
statements
endwhile;


The while expression is checked before the start of each iteration. If the expression evaluates to true, the code within the loop is executed. If the expression evaluates to false, however, execution skips to the code immediately following the while loop. Note that you can omit the curly braces with the first form of the while statement if you only need to execute a single statement.

It is possible to break out of a running loop at any time using the break keyword. This stops the current loop and, if control is within a nested set of loops, the next outer loop continues. It is also possible to break out of many levels of nested loops by passing a numerical argument to the break statement (break n) that specifies the number of nested loops it should break out of. You can skip the rest of a given loop and go onto the next iteration by using the continue keyword. With continue n, you can skip the current iterations of the n innermost loops.

do/while
The do/while statement is similar to the while statement, except that the conditional expression is checked at the end of each iteration instead of before it:

do {
statements
} while(expr);


Note that due to the order of the parts of this statement, there is only one valid syntax. If you only need to execute a single statement, you can omit the curly braces from the syntax. The break and continue statements work with this statement in the same way that they do with the while statement.

for

A for loop is a more complex looping construct than the simple while loop:

for(start_expr; cond_expr; iter_expr) {
statements
}

for(start_expr; cond_expr; iter_expr):
statements
endfor;


A for loop takes three expressions. The first is the start expression; it is evaluated once when the loop begins. This is generally used for initializing a loop counter. The second expression is a conditional expression that controls the iteration of the loop. This expression is checked prior to each iteration. The third expression, the iterative expression, is evaluated at the end of each iteration and is typically used to increment the loop counter. With the first form of the for statement, you can omit the braces if you only need to execute a single statement.

The break and continue statements work with a for loop like they do with a while loop, except that continue causes the iterative expression to be evaluated before the loop conditional expression is checked.
blog comments powered by Disqus
PHP ARTICLES

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials