611Chapter 33Stylefor ($i = $n, $plist = array(); (Free web space)
Monday, September 10th, 2007611Chapter 33Stylefor ($i = $n, $plist = array(); $i > 1; $i–) if (!$carray[$i]) array_push($plist,$i); return($plist); } Obviously, this implements the Sieve of Erasthones, and $plistis a list of all the prime num- bers less than $n. Obviously. So why do programmers strive for conciseness? The first reason is that it saves them time (butonly at the time of actual code writing). The second reason (and we re only half-joking) is thatthey re afraid some other programmer (probably one trained in C) will come along later, laughat them, and point out that their code could have been written in only half the space. Conciseness tipsIf you must write code that fits in less space, try some of the following techniques. Use return values and side effects at the same timeIt s a very common trick to exploit the fact that the value of an assignment is the valueassigned, as in the following pseudocode: while ($next = GetNextOne()) DoSomethingWith($next); where GetNextOne()is some function that returns useful values in sequence and thenreturns a false value when it runs out of them. When a false value is returned, $nextis false, and the whileloop terminates. Use incrementing and assignment operatorsThe incrementing operators (++and –) shorten statements that involve adding or subtract- ing one from a variable, and the combined assignment operators (+=, *=, .=, and so on) makecertain kinds of assignments more concise. The incrementing operators and the arithmetic assignment operators are covered in Chapter 10, and the combined string assignment operator (.=) is covered in Chapter 8. Often these operators are used in combination with the previous trick, as in: while ($count–) DoSomethingWith($count); which (assuming that $countstarts as a positive integer) would call its function for the verylast time on the value 1. Reuse functionsThis is one case where conciseness is good, because functions are good. If you can identifyany stretches of code that get duplicated in your pages, try to replace each one with a call toa single function that packages up that code. Your code will be shorter by the amount of theduplication and also easier to maintain. Cross- Reference37
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check mysql web server services.