Archive for August, 2007

100Part IPHP: The Basicscontinue; print( $x ); } prints: (Affordable web design)

Thursday, August 9th, 2007

100Part IPHP: The Basicscontinue; print( $x ); } prints: 2 4 6 8because the effect of the continuestatement is to skip the printing of any odd numbers. Using the breakcommand, the programmer can choose to dispense with the main termina- tion test altogether. Consider the following code, which prints a list of prime numbers (thatis, numbers not divisible by something other than 1 or the number itself): $limit = 500; $to_test = 2; while(TRUE) { $testdiv = 2; if ($to_test > $limit) break; while (TRUE) { if ($testdiv > sqrt($to_test)) { print $to_test ; break; } // test if $to_test is divisible by $testdivif ($to_test % $testdiv == 0) break; $testdiv = $testdiv + 1; } $to_test = $to_test + 1; } In the preceding code, we have two whileloops the outer loop works through all the num- bers between 1 and 500, and the inner loop actually does the testing with each possible divisor. If the inner loop finds a divisor, the number is not prime, so it breaks out without printing any- thing. If, on the other hand, the testing gets as high as the square root of the number, we cansafely assume that the number must be prime, and the inner loop is broken without printing. Finally, the outer loop is broken when we have reached the limit of numbers to test. The resultin this case is a list of primes less than 500: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 8389 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449457 461 463 467 479 487 491 499Notice that it is crucial to this code that breakinterrupt the inner whileloop only. There is another iteration construct, called foreach, which is used only for iterating overarrays. We cover it in Chapter 9. Cross- Reference08
Searching for affordable and reliable webhost to host and run your web applications? Go to our java web server services and you will be pleased.

Best web site - 99Chapter 6Control and FunctionsFigure 6-2:Approximating a square rootBreak

Wednesday, August 8th, 2007

99Chapter 6Control and FunctionsFigure 6-2:Approximating a square rootBreak and continueThe standard way to get out of a looping structure is for the main test condition to becomefalse. The special commands breakand continueoffer an optional side exit from all thelooping constructs, including while, do-while, and for: .The breakcommand exits the innermost loop construct that contains it. .The continuecommand skips to the end of the current iteration of the innermost loopthat contains it. For example, the following code: for ($x = 1; $x < 10; $x++) { // if $x is odd, break outif ($x % 2 != 0) break; print( $x ); } prints nothing, because 1 is odd, which terminates the forloop immediately. On the otherhand, the code: for ($x = 1; $x < 10; $x++) { // if $x is odd, skip this loopif ($x % 2 != 0)
We highly recommend you visit web and email hosting services if you need stable and cheap web hosting platform for your web applications.

Web site - 98Part IPHP: The BasicsThe main body of this

Tuesday, August 7th, 2007

98Part IPHP: The BasicsThe main body of this code simply has one forloop nested inside another, with each loopexecuting ten times, resulting in a 10 10 table. Each iteration of the outer loop prints a row, whereas each inner iteration prints a cell. The only novel feature is the way we chose to printthe numbers we used printf(covered in Chapter 8) , which allows us to control the num- ber of decimal places printed. The $variable_name++feature used above is called an increment. It s a fairly standardshorthand for $variable_name+1. An unbounded while loopNow let s look at a loop not so obviously bounded. The sole purpose of the code in Listing 6-2is to approximate the square root of 81 (using Newton s method). The approximation startswith a guess of 1 and then zeros in on the actual square root of 9 by improving the guesses. A trace of this approximation is shown in Figure 6-2. Listing 6-2:Approximating a square root Approximating a square root

Approximating a square root

$precision) or($guess_squared - $target < - $precision)) { print( Current guess: $guess is the squareroot of $target
); $guess = ($guess + ($target / $guess)) / 2; $guess_squared = $guess * $guess; } print( $guess squared = $guess_squared
); ?> Now, although it nicely illustrates a potentially unbounded loop, this approximation exampleis very artificial first, because PHP already has a perfectly good square-root function (sqrt) and second, because the number 81 is hardcoded into the page. We can t use this page to findthe square root of any other number. Note08
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.

Hp web site - 97Chapter 6Control and Functions$count_1

Tuesday, August 7th, 2007

97Chapter 6Control and Functions$count_1 <= $end_num; $count_1++) print( $count_1 ); print( ); for ($count_1 = $start_num; $count_1 <= $end_num; $count_1++) { print( $count_1 ); for ($count_2 = $start_num; $count_2 <= $end_num; $count_2++) { $result = $count_1 / $count_2; printf( %.3f , $result); // see Chapter 8} print( n ); } ?> Figure 6-1:A division table08
If you are in need for cheap and reliable webhost to host your website, we recommend http web server services.

96Part IPHP: The Basicsis equivalent to: while (TRUE) (Post office web site)

Friday, August 3rd, 2007

96Part IPHP: The Basicsis equivalent to: while (TRUE) statementIt is also legal to include more than one of each kind of forclause, separated by commas. The termination-check will be considered to be true if any of its subclauses are true; it is likean or test. For example, the following statement: for ($x = 1, $y = 1, $z = 1; //initial expressions$y < 10, $z < 10; // termination checks$x = $x + 1, $y = $y + 2, // loop-end expressions$z = $z + 3) print( $x, $y, $z
); would give the browser output: 1, 1, 12, 3, 43, 5, 7Although the forsyntax is the most complex of the looping constructs, it is often used forsimple bounded loops, using the following idiom: for ($count = 0; $count < $limit; $count = $count + 1) statementLooping examplesNow let s look at some examples. A bounded for loopListing 6-1 shows a typical use of bounded forloops. The page produced by Listing 6-1 isshown in Figure 6-1. Listing 6-1:A division table A division table

A division table

); print( ); for ($count_1 = $start_num;
Searching for affordable and proven webhost to host and run your servlet applications? Go to Linux Web Hosting services and you will find it.

95Chapter 6Control and FunctionsDo-whileThe do-whileconstruct is similar to (Unable to start debugging on the web server)

Friday, August 3rd, 2007

95Chapter 6Control and FunctionsDo-whileThe do-whileconstruct is similar to while, except that the test happens at the end of theloop. The syntax is: do statementwhile (expression); The statement is executed once, and then the expression is evaluated. If the expression istrue, the statement is repeated until the expression becomes false. The only practical differ- ence between whileand do-whileis that the latter will always execute its statement at leastonce. For example: $count = 45; do{ print( count is $count
); $count = $count + 1; } while ($count <= 10) prints the single line: count is 45ForThe most complicated looping construct is for, which has the following syntax: for (initial-expression; termination-check; loop-end-expression) statementIn executing a forstatement, first the initial-expressionis evaluated just once, usually to ini- tialize variables. Then termination-checkis evaluated if it is false, the forstatement con- cludes, and if it is true, the statement executes. Finally, the loop-end-expressionis executedand the cycle begins again with termination-check. As always, by statementwe mean a single(semicolon-terminated) statement, a brace-enclosed block, or a conditional construct. If we rewrote the preceding forloop as a whileloop, it would look like this: initial-expression; while (termination-check) { statementloop-end-expression; } Actually, although the typical use of forhas exactly one initial-expression, one termination- check, and one loop-end-expression, it is legal to omit any of them. The termination-check istaken to be always true if omitted, so: for (;;) statement08
Searching for affordable and proven webhost to host and run your servlet applications? Go to Linux Web Hosting services and you will find it.

94Part IPHP: The BasicsLoopingCongratulations! You just passed the (Web site directory)

Thursday, August 2nd, 2007

94Part IPHP: The BasicsLoopingCongratulations! You just passed the boundary from scripting into real programming. Thebranching structures we have looked at so far are useful, but there are limits to what can becomputed with them alone. On the other hand, it s well established in theoretical computerscience that any language with tests plus unbounded looping can do pretty much anythingthat any other language can do. You may not actually want to write a C compiler in PHP, forexample, but it s nice to know that no inherent language limits are going to stop you. Bounded loops versus unbounded loopsA bounded loopexecutes a fixed number of times you can tell by looking at the code howmany times the loop will iterate, and the language guarantees that it won t loop more timesthan that. An unbounded looprepeats until some condition becomes true (or false), and thatcondition is dependent on the action of the code within the loop. Bounded loops are pre- dictable, whereas unbounded loops can be as tricky as you like. Unlike some languages, PHP doesn t actually have any constructs specifically for boundedloops while, do-while, and forare all unbounded constructs but as we will see in thissection, an unbounded loop can do anything a bounded loop can do. In addition to the looping constructs in this chapter, PHP provides functions for iterating overthe contents of arrays, which are covered in Chapter 9. WhileThe simplest PHP looping construct is while, which has the following syntax: while (condition) statementThe whileloop evaluates the conditionexpression as a Boolean if it is true, it executesstatementand then starts again by evaluating condition.If the condition is false, the whileloop terminates. Of course, just as with if, statementmay be a single statement or it may be a brace-enclosed block. The body of a whileloop may not execute even once, as in: while (FALSE) print( This will never print.
); Or it may execute forever, as in this code snippet: while (TRUE) print( All work and no play makesJack a dull boy.
); or it may execute a predictable number of times, as in: $count = 1; while ($count <= 10) { print( count is $count
); $count = $count + 1; } which will print exactly 10 lines. (For more interesting examples, see the Looping examples section, later in this chapter.) Cross- Reference08
Please visit our professional web hosting services to find out about cheap and reliable webhost service that will surely answer all your demands.

93Chapter 6Control and Functionsswitch(expression) (Affordable web design) { case value-1: statement-1statement-2…

Thursday, August 2nd, 2007

93Chapter 6Control and Functionsswitch(expression) { case value-1: statement-1statement-2… [break;] case value-2: statement-3statement-4… [break;] … [default: default-statement] } The expression can be a variable or any other kind of expression, as long as it evaluates to asimple value (that is, an integer, a double, or a string). The construct executes by evaluatingthe expression and then testing the result for equality against each case value. As soon as amatching value is found, subsequent statements are executed in sequence until the specialstatement (break;) or until the end of the switchconstruct. (As we ll see in the Looping section of this chapter, breakcan also be used to break out of looping constructs.) A specialdefaulttag can be used at the end, which will match the expression if no other case hasmatched it so far. For example, we can rewrite our if-elseexample as follows: switch($day) { case 5: print( Five golden rings
); break; case 4: print( Four calling birds
); break; case 3: print( Three French hens
); break; case 2: print( Two turtledoves
); break; default: print( A partridge in a pear tree
); } This will print a single appropriate line for days 2 5; for any day other than those, it will printApartridgeinapeartree. Although switchwill accept only a single argument, there s noreason why that argument can t be the value of expressions evaluated previously in your code. The single most confusing aspect of switchis that all cases after a matching case will exe- cute, unless there are breakstatements to stop the execution. In the partridge example, the breakstatements ensure that we see only one line from the song at a time. If weremove the breakstatements, we will see a sequence of lines counting down to the finalline, just as in the song. Caution08
We recommend high quality webhost to host and run your jsp application: christian web host services.

92Part IPHP: The BasicsThis pattern is common (Web site) enough

Wednesday, August 1st, 2007

92Part IPHP: The BasicsThis pattern is common enough that there is a special elseifconstruct to handle it. We canrewrite the preceding example as: if ($day == 5) print( Five golden rings
); elseif ($day == 4) print( Four calling birds
); elseif ($day == 3) print( Three French hens
); elseif ($day == 2) print( Two turtledoves
); elseif ($day == 1) print( A partridge in a pear tree
); The if, elseifconstruct allows for a sequence of tests that executes only the first branchthat has a successful test. In theory, this is syntactically different from the previous example(we have a single construct with five branches rather than a nesting of five two-branch con- structs), but the behavior is identical. Use whichever syntax you find more appealing. SwitchFor a specific kind of multiway branching, the switchconstruct can be useful. Rather thanbranch on arbitrary logical expressions, switchtakes different paths according to the valueof a single expression. The syntax is as follows, with the optional parts enclosed in squarebrackets ([]): ContinuedThe men-only site
This site has been specially constructedfor men only.
No women allowed here! This version is somewhat more difficult to read, but the only difference is that it replaces each setof printstatements with a block of literal HTML that starts with a closing PHP tag (?>) and endswith a starting PHP tag (Check Tomcat Web Hosting services for best quality webspace to host your web application.

91Chapter 6Control and FunctionsBranching and HTML ModeAs you (Web proxy server)

Wednesday, August 1st, 2007

91Chapter 6Control and FunctionsBranching and HTML ModeAs you may have learned from earlier chapters, you should feel free to use the PHP tags to switchback and forth between HTML mode and PHP mode, whenever it seems convenient. If you needto include a large chunk of HTML in your page that has no dynamic code or interpolated vari- ables, it can be simpler and more efficient to escape back into HTML mode and include it literallythan it is to send it using printor echo. What may not be as obvious is that this strategy works even inside conditional structures. That is, you can use PHP to decide what HTML to send and then send that HTML by temporarily escap- ing back to HTML mode. For example, the following cumbersome code uses printstatements to construct a completeHTML page based on the supposed gender of the viewer. (We re assuming a nonexistentBoolean function called female()that tests for this.) The women-only site
); print( ); print( This site has been specially constructed ); print( for women only.
No men allowed here! ); } else{ print( The men-only site
); print( ); print( This site has been specially constructed ); print( for men only.
No women allowed here! ); } ?> Instead of all these printstatements, we can duck back into HTML mode within each of the twobranches: The women-only site This site has been specially constructedfor women only.
No men allowed here! Continued08
We would like to recommend you tested and proved virtual web hosting services, which you will surely find to be of great quality.