Archive for August, 2007

120Part IPHP: The BasicsHTML forms are mostly useful (Web design seattle)

Friday, August 24th, 2007

120Part IPHP: The BasicsHTML forms are mostly useful for passing a few values from a given page to one single otherpage of a Web site. There are more persistent ways to maintain state over many pageviews, such as cookies and sessions, which we cover in Chapter 24. This chapter will focus on themost basic techniques of information-passing between Web pages, which utilize the GETandPOSTmethods in HTTP to create dynamically generated pages and to handle form data. This is where old-school ASP developers invariably say, PHP sucks! They think ASP sessionvariables are magic. Not to burst anyone s bubble, but Microsoft is just using cookies to storesession variables thereby opening the door to all kinds of potential problems. GET ArgumentsThe GETmethod passes arguments from one page to the next as part of the Uniform ResourceIndicator(you may be more familiar with the term Uniform Resource Locatoror URL) querystring. When used for form handling, GETappends the indicated variable name(s) andvalue(s) to the URL designated in the ACTIONattribute with a question mark separator andsubmits the whole thing to the processing agent (in this case a Web server). This is an example HTML form using the GETmethod (save the file under the nameteam_select.html): A GET method example, part 1

Root, root, root for the:

When the user makes a selection and clicks the Submit button, the browser agglutinatesthese elements in this order, with no spaces between the elements: .The URL in quotes after the word ACTION(http://localhost/baseball.php) .A question mark (?) denoting that the following characters constitute a GETstring. .A variable NAME, an equal sign, and the matching VALUE(Team=Cubbies) .An ampersand (&) and the next NAME-VALUEpair (Submit=Select); further name-valuepairs separated by ampersands can be added as many times as the server query- string-length limit allows. Note09
Please visit our professional web hosting services to find out about cheap and reliable webhost service that will surely answer all your demands.

Passing Informationbetween PagesIn this (Web site designers) chapter, we ll briefly discuss

Thursday, August 23rd, 2007

Passing Informationbetween PagesIn this chapter, we ll briefly discuss some things you need to knowabout passing data between Web pages. Some of this information isnot specific to PHP but is a consequence of the PHP/HTML interac- tion or of the HTTP protocol itself. HTTP Is StatelessThe most important thing to recall about the way the Web works isthat the HTTP protocol itself is stateless. If you are a poetic soul, youmight say that each HTTP request is on its own, with no directionhome, like a complete unknown . . . you know how the rest goes. For the less lyrical among us, this means that each HTTP request inmost cases, this translates to each resource (HTML page, .jpg file, style sheet, and so on) being asked for and delivered is indepen- dent of all the others, knows nothing substantive about the identityof the client, and has no memory. Each request spawns a discreteprocess, which goes about its humble but worthy task of serving upone single solitary file and then is automatically killed off. (But thatsounds so harsh; maybe we can say flits back to the pool of avail- able processes instead.) Even if you design your site with very strict one-way navigation (Page1 leads only to Page 2, which leads only to Page 3, and so on), theHTTP protocol will never know or care that someone browsing Page 2must have come from Page 1. You cannot set the value of a variableon Page 1 and expect it to be imported to Page 2 by the exigencies ofHTML itself. You can use HTML to display a form, and someone canenter some information using it but unless you employ some extrameans to pass the information to another page or program, the variable will simply vanish into the ether as soon as you move toanother page. This is where a form-handling technology like PHP comes in. PHP willcatch the variable tossed from one page to the next and make it avail- able for further use. PHP happens to be unusually good at this type ofdata-passing function, which makes it fast and easy to employ for awide variety of Web site tasks. 77CHAPTER …In This ChapterHTTP is statelessGET argumentsA better use for GET-style URLsPOST argumentsFormatting formvariablesPHP superglobal arraysExtended example: calculator …
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.

118Part IPHP: The BasicsSummaryPHP has a C-like set (Web domain)

Wednesday, August 22nd, 2007

118Part IPHP: The BasicsSummaryPHP has a C-like set of control structures, which branch or loop depending on the value ofBoolean expressions, which in turn can be combined using Boolean operators (and, or, xor, !, &&, ||). The structures ifand switchare used for simple branching; while, do-while, and forare used for looping, and exit()or die()terminates script execution. Most of the power of PHP resides in the large number of built-in functions provided by PHP sbenevolent army of open source developers. Each of these functions should be documented(albeit briefly) in the online manual at http://www.php.net. You can also write your own functions, which are then used in exactly the same way as thebuilt-in functions. Functions are written in a simple C-style syntax, as in the following: function my_function ($arg1, $arg2, ..) { statement1; statement2; .. return($value); } User-defined functions can use arguments of any PHP type and can also return values of anytype. The types of arguments and return values do not need to be declared. In PHP, the ordering of function definitions and function calls makes no difference, as long asevery function that is called is defined exactly once. There is no need for separate functiondeclarations or prototypes. Variables assigned inside a function are local to that function, unless specified otherwise with the globaldeclaration. Local variables may be declared tobe static,which means that they hold onto their values in between function calls. Finally, with our brief treatment of exceptions, we re well on our way to writing thoughtfulfriendly code that uses standardized error handling. …
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision J2ee Web Hosting services.

117Chapter 6Control and Functionscountdown($num_arg - 1); } } (Photo web hosting)

Wednesday, August 22nd, 2007

117Chapter 6Control and Functionscountdown($num_arg - 1); } } countdown(10); This produces the browser output: Counting down from 10Counting down from 9Counting down from 8Counting down from 7Counting down from 6Counting down from 5Counting down from 4Counting down from 3Counting down from 2Counting down from 1As with all recursive functions, it s important to be sure that the function has a base case(a nonrecursive branch) in addition to the recursive case, and that the base case is certain toeventually occur. If the base case is never invoked, the situation is much like a whileloopwhere the test is always true we will have an infinite loop of function calling. In the case ofthe preceding function, we know that the base case will happen, because every invocation ofthe recursive case reduces the countdown number, which must eventually become zero. Ofcourse, this assumes that the input is a positive integer rather than a negative number or adouble. Notice that our greater than zero test guards against infinite recursion even in these cases, whereas a not equal to zero test would not. Similarly, mutually recursive functions(functions that call each other) work without a hitch. For example, the following definitions plus function call: function countdown_first ($num_arg) { if ($num_arg > 0) { print( Counting down (first) from $num_arg
); countdown_second($num_arg - 1); } } function countdown_second ($num_arg) { if ($num_arg > 0) { print( Counting down (second) from $num_arg
); countdown_first($num_arg - 1); } } countdown_first(5); produce the browser output: Counting down (first) from 5Counting down (second) from 4Counting down (first) from 3Counting down (second) from 2Counting down (first) from 108
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.

116Part IPHP: The BasicsIncluding only (Web host forum) onceSometimes you really

Tuesday, August 21st, 2007

116Part IPHP: The BasicsIncluding only onceSometimes you really want a file to be included once, but not more than once. This is truemost often in the case of function definitions. For example, two different function definitionfiles might, in turn, include the same file of utility functions if a top-level page includes bothof these files, the utility functions might be included twice, leading to complaints from PHPthat functions are being defined twice. To the rescue come include_onceand require_once, which act just like their counterpartsexcept that they will not include a file named by a given string if that file has already beenincluded. It s usually better to use the _onceversion, in general, for including function andclass definition files. The include pathWhen you includea filename, PHP searches for a file by that name in the directories speci- fied in the include_path(which is settable in your php.inifile). The default path includesthe same directory as the one the top-level code page is in. See Chapter 30 for details abouthow to add locations to your includepath. In situations where a single instance of PHP serves several virtual sites, it s generally easierand less confusing to PHP to use the $_SERVERsuperglobal array to specify the location of anincludefile: include_once($_SERVER[ DOCUMENT_ROOT ]. /path/to/include_file ); Remember that included (and required) files are parsed by default in HTML mode ratherthan in PHP mode. This means that any included file meant to be interpreted as PHP needsto have the usual PHP tags at the beginning and end. RecursionSome compiled languages, like C and C++, impose somewhat complex ordering constraints onhow functions are defined. To know how to compile a function, the compiler must know aboutall the functions that the function calls, which means the called functions must be definedfirst. So what do you do if two functions each call the other or if one function calls itself? Issues like this led the designers of C to a separation of function declarations (or prototypes) from function definitions (or implementations). The idea is that you use declarations to informthe compiler in advance about the types of arguments and return types of the functions youplan to use, which is enough information for the compiler to handle the actual definitions inany order. In PHP, this problem goes away, and so there is no need for separate function prototypes. Aslong as each function that is called is defined once (and only once) in the current code file orone that is included in the course of the current script s execution, PHP will have no problemresolving function calls, regardless of the interleaving of function calls and definitions. This means that recursive functions(functions that call themselves) are no problem in PHP4. For example, we can define a recursive function and then immediately call it: function countdown ($num_arg) { if ($num_arg > 0) { print( Counting down from $num_arg
); Caution08
Looking for affordable and reliable webhost to host and run your business application? Then look no more and go to servlet web hosting services.

115Chapter 6Control and FunctionsFunction ScopeAlthough the rules about (Web hosting directory)

Monday, August 20th, 2007

115Chapter 6Control and FunctionsFunction ScopeAlthough the rules about the scope of variable names are fairly simple, the scoping rules forfunction names are even simpler. There is just one rule in PHP5: Functions must be definedonce (and only once) somewhere in the script that uses them. (See the following note aboutdifferences between this behavior and PHP3.) The scope of function names is implicitlyglobal, so a function defined in a script is available everywhere in that script. For clarity ssake, however, it is often a good idea to define all your functions first before any code thatcalls those functions. In PHP3, functions could be used only after they were defined. This meant that the safestpractice was to define (or include the definitions of) all functions early in a given script, before actually using any of them. PHP4 and 5 actually precompile scripts before runningthem, and one effect of this precompilation is that it discovers all function definitions beforeactually running the code. This means that functions and code can appear in any order in ascript, as long as all functions are defined once (and only once). Include and requireIt s very common to want to use the same set of functions across a set of Web site pages, andthe usual way to handle this is with either includeor require, both of which import thecontents of some other file into the file being executed. Using either one of these forms isvastly preferable to cloningyour function definitions (that is, repeating them at the beginningof each page that uses them); when you want to modify your functions, you will have to do itonly once. (We covered these forms in Chapter 4, but they are worth reviewing here in thecontext of including function definitions.) For example, at the top of a PHP code file we might have lines like: include basic-functions.inc include advanced-function.inc ; (.. code that uses basic and advanced functions ..) which import two different files of function definitions. (Note that parentheses are optionalwith both include()and require().) As long as the only things in these files are functiondefinitions, the order of their inclusion does not matter. Both includeand requirehave the effect of splicing in the contents of their file into thePHP code at the point that they are called. The only difference between them is how they failif the file cannot be found. The includeconstruct will cause a warning to be printed, but processing of the script will continue; require, on the other hand, will cause a fatal error ifthe file cannot be found. Note that includeand requireare now more similar in their behavior than they used tobe. Prior to PHP 4.0.2, requirehad its file contents spliced in statically, before the actualexecution of the page; whereas the contents from includewere spliced in dynamically as the page executed. Among other things, this led to subtle differences in behavior whenthe include/requireform was in conditional code. Now, however, both includeandrequirehave the same dynamic behavior. This means, for example, that if aninclude/requireform is in a loop executed 10 times, 10 inclusions will be made. NoteNote08
If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.

Best web hosting - 114Part IPHP: The BasicsThe custom function print_header()is designed

Sunday, August 19th, 2007

114Part IPHP: The BasicsThe custom function print_header()is designed to make it easy for us to place a standard- ized, search engine friendly header at the top of each page. However, we ve left the descrip- tion variable undefined, which will not yield an error, but will leave us without a meaningfuldescription for our page. Unfortunately, because the function is essentially called correctlyand PHP is forgiving in nature, we may never know that we ve left off this important detail. Some form of error handling is necessary to point this out, and Exceptionsprovide a handyway of dong so. Consider this revised code: function print_header($title, $keywords, $description) { if(strlen($description) < 40) throw new Exception( A reasonable description length isrequired
); print( ); print( $title ); print( ); print( ); print( ); } try { print_header( My Page , PHP, Programming, Beer , ); } catch (Exception $e) { echo($e->getMessage()); } The first new thing in our revised function is a simple test in line 2 suggesting an appropriateminimum length for the $descriptionvariable. The line immediately following initiates aninstance of the Exceptionclass with the message suggested by the quoted value. A class is one of the recent OOP concepts introduced in PHP4. You can create your own classesand extensions of existing classes, including those for Exception handling. PHP gives youException for free. We ll go into much greater depth on the subject of classes in Chapter 20 andexception handling itself in Chapter 31. Next, instead of simply calling our function, we ve enclosed the function in a new controlstructure, the try…catchblock. If we execute the code as written, PHP first tries to exe- cute the function as described; then it terminates execution almost immediately, because the$descriptionvariable has failed our simple test. At this point, the script can continue exe- cution after the try…catchblock, or it can be terminated with die()or exit(). Multiple exceptions can be defined in a single function. This is good idea because it yieldsmore specific information about what exactly happened. Because execution stops with thefirst exception, only this exception will be caught. Exceptions are a huge topic; they re outlined here so you can start using them immediately. You ll find nods to Exceptions throughout this book, but they are covered in depth in Chapter 31. Cross- ReferenceNote08
Check Tomcat Web Hosting services for best quality webspace to host your web application.

Michigan web site - 113Chapter 6Control and Functions{ print(chr(ord( A ) + $count)); $count

Sunday, August 19th, 2007

113Chapter 6Control and Functions{ print(chr(ord( A ) + $count)); $count = $count + 1; } print(
Now I know $count letters
); } $count = 0; SayMyABCs3(); $count = $count + 1; print( Now I ve made $count function call(s).
); SayMyABCs3(); $count = $count + 1; print( Now I ve made $count function call(s).
); This memory-enhanced version gives us the following output: ABCDEFGHIJNow I know 10 lettersNow I ve made 1 function call(s). KLMNOPQRSTNow I know 20 lettersNow I ve made 2 function call(s). The static keyword allows for an initial assignment, which has an effect only if the functionhas not been called before. The first time SayMyABCs3()executes, the local version of$countis set to zero. The second time the function is called, it has the value it had at the end of the last execution, so we are able to pick up our studies where we left off. Notice thatchanges to $countoutside the function still have no effect on the local value. ExceptionsNew to PHP5 is the Exceptionclass. We ve already seen some fairly primitive error handlingin the form of die(), and you might well imagine the custom error handling possibilitiesimplied by the combination of control structures and basic use of print()or printf() commands (more on this in Chapter 26). However, in prior versions of PHP, a chief complaintwas the lack of standardized means for handling errors, and separating that means from theapplication code itself. Enter Exceptions. Exceptions use the try, catchsyntax similar to Java or Python, although programmersusing those languages will note the absence of finally. Let s start with a simple example that has no error handling at all: function print_header($title, $keywords, $description) { print( ); print( $title ); print( ); print( ); print( ); } print_header( My Page , PHP, Programming, Beer , );
In case you need quality webspace to host and run your web applications, try our personal web hosting services.

112Part IPHP: The Basicsname to mean the same (Business web hosting)

Saturday, August 18th, 2007

112Part IPHP: The Basicsname to mean the same thing as it does in the context outside the function. The syntax of thisdeclaration is simply the word global, followed by a comma-delimited list of the variablesthat should be treated that way, with a terminating semicolon. To see the effect, consider anew version of the previous example. The only difference is that we have declared $counttobe global, and we have removed its initial assignment to zero inside the function: function SayMyABCs2 () { global $count; while ($count < 10) { print(chr(ord( A ) + $count)); $count = $count + 1; } print(
Now I know $count letters
); } $count = 0; SayMyABCs2(); $count = $count + 1; print( Now I ve made $count function call(s).
); SayMyABCs2(); $count = $count + 1; print( Now I ve made $count function call(s).
); Our revised version prints the following browser output: ABCDEFGHIJNow I know 10 lettersNow I ve made 11 function call(s). Now I know 11 lettersNow I ve made 12 function call(s). This is buggy behavior, and the globaldeclaration is to blame. There is now only one $countvariable, and it is being increased both inside and outside the function. When the second call toSayMyABCs()happens, $countis already 11, so the loop that prints letters is never entered. Although this example shows globalto bad advantage, it can be quite useful, especiallybecause (as we ll see in Chapter 7) PHP provides some variable bindings to every page evenbefore any of your own code is executed. It can be helpful to have a way for functions to seethese variables without the bother of passing them in as arguments with each call. Static variablesBy default, functions retain no memory of their own execution, and with each function calllocal variables act as though they have been newly created. The staticdeclaration over- rides this behavior for particular variables, causing them to retain their values in betweencalls to the same function. Using this, we can modify our earlier function SayMyABCs2()togive it some memory: function SayMyABCs3 () { static $count = 0; //assignment only if first time called$limit = $count + 10; while ($count < $limit)
If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.

Web hosting domains - 111Chapter 6Control and FunctionsAs of PHP 4.1, there

Friday, August 17th, 2007

111Chapter 6Control and FunctionsAs of PHP 4.1, there is a small set of global variables that areautomatically visible fromwithin function definitions, in contradiction to the previous paragraph and the following one. These are the superglobal arrays($_POST, $_GET, $_SESSION, and so on), which containkeys and values corresponding to variable bindings from different sources. For more onthese variables and their uses, see Chapter 7. The only variable values that a function has access to are the formal parameter variables(which have the values copied from the actual parameters), plus any variables assignedinside the function. This means that you can use local variables inside a function withoutworrying about their effects on the outside world. For example, consider this function and its subsequent use: function SayMyABCs () { $count = 0; while ($count < 10) { print(chr(ord( A ) + $count)); $count = $count + 1; } print(
Now I know $count letters
); } $count = 0; SayMyABCs(); $count = $count + 1; print( Now I ve made $count function call(s).
); SayMyABCs(); $count = $count + 1; print( Now I ve made $count function call(s).
); The intent of SayMyABCs()is to print a sequence of letters. (The functions chr()and ord() translate between letters and their numeric ASCII codes we use them here just as a trick togenerate letters in sequence.) The output of this code is: ABCDEFGHIJNow I know 10 lettersNow I ve made 1 function call(s). ABCDEFGHIJNow I know 10 lettersNow I ve made 2 function call(s). Both the function definition and the code outside the function make use of variables called$count, but they refer to different variables and do not clash. The default behavior of variables assigned inside functions is that they do not interact withthe outside world; they act as though they are newly created each time the function is called. Both of these behaviors, however, can be overridden with special declarations. Global versus localThe scope of a variable defined inside a function is localby default, meaning that (as weexplained in the previous section) it has no connection with the meaning of any variables out- side the function. Using the globaldeclaration, you can inform PHP that you want a variableNote08
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.