Archive for May, 2007

81Chapter 5Syntax and VariablesBoth of (Sex offenders web site) these statements will

Sunday, May 6th, 2007

81Chapter 5Syntax and VariablesBoth of these statements will cause the given sentence to be displayed, without displayingthe quote signs. (Note for C programmers:Think of the HTTP connection to the user as thestandard output stream for these functions.) You can also give multiple arguments to the unparenthesized version of echo, separated bycommas, as in: echo This will print in the , user s browser window. ; The parenthesized version, however, will not accept multiple arguments: echo ( This will produce a , PARSE ERROR! ); PrintThe command printis very similar to echo, with two important differences: .Unlike echo, printcan accept only one argument. .Unlike echo, printreturns a value, which represents whether the printstatementsucceeded. The value returned by printwill be 1if the printing was successful and 0if unsuccessful. (It is rare that a syntactically correct printstatement will fail, but in theory this return valueprovides a means to test, for example, if the user s browser has closed the connection.) Both echoand printare usually used with string arguments, but PHP s type flexibility meansthat you can throw pretty much any type of argument at them without causing an error. Forexample, the following two lines will print exactly the same thing: print( 3.14159 ); // print a stringprint(3.14159); // print a numberTechnically, what is happening in the second line is that, because printexpects a stringargument, the floating-point version of the number is converted to a string value beforeprintgets hold of it. However, the effect is that both printand echowill reliably print outnumbers as well as string arguments. For the sake of simplicity and uniformity, we will typically use the parenthesized version ofprintin our examples, rather than using echo. In addition to the printing functions discussed here, there are two printing functions usedmostly for debugging: print_r()and var_dump(). The point of these functions is to helpyou visualize what s going on with compound data structures like arrays, so we cover themalong with the details of arrays in Chapter 9. Variables and stringsC programmers are accustomed to using a function called printf, which allows you to splicevalues and expressions into a specially formatted printing string. PHP has analogous func- tions (which we will cover in Chapter 7), but as it turns out we can get much of the samefunctionality just by using print(or echo) with quoted strings. For example, the fragment: $animal = antelope ; $animal_heads = 1; $animal_legs = 4; print( The $animal has $animal_heads head(s).
); print( The $animal has $animal_legs leg(s).
); Cross- Reference07
Note: If you are looking for high quality webhost to host and run your jsp application check Vision florida web design services

Web design company - 80Part IPHP: The BasicsIn addition to single-quotes and

Saturday, May 5th, 2007

80Part IPHP: The BasicsIn addition to single-quotes and double-quotes, there is another way to create strings (calledthe heredocsyntax), which in some ways makes it even easier to splice in the values of vari- ables. We cover it in Chapter 8. Newlines in stringsAlthough PHP offers an escape sequence (n) for newline characters, it is good to know thatyou can literally include new lines in the middle of strings, which PHP also treats as a newlinecharacters. This capability turns out to be convenient when creating HTML strings, becausebrowsers will ignore the line breaks anyway, so we can format our strings with line breaks tomake our PHP code lines short: print( My HTML page is too bigto fit on a single line, but that doesn t mean that Ineed multiple print statements! ); We produced this statement in our text editor by literally hitting the Enter key at the end of thefirst two lines these newlines are preserved in the string, so the single printstatement willproduce three distinct lines of PHP output. (Your mileage may vary depending on your text editor if your editor automatically wraps lines in displaying them, you may see three lines ofcode that are actually one long line.) Of course, the browser program will ignore these newlinesand will make its own decisions about whether and where to break the lines in display, but youwill see the linebreaks if you use View Source in your browser to see the HTML itself. LimitsThere are no artificial limits on string length within the bounds of available memory, youought to be able to make arbitrarily long strings. OutputMost of the constructs in the PHP language execute silently they don t print anything tooutput. The only way that your embedded PHP code will display anything in a user s browserprogram is either by means of statements that print something to output or by calling func- tions that, in turn, call printstatements. Echo and printThe two most basic constructs for printing to output are echoand print. Their language status is somewhat confusing, because they are basic constructs of the PHP language, ratherthan being functions. As a result, they can be used either with parentheses or without them. (Function calls always have the name of the function first, followed by a parenthesized list ofthe arguments to the function.) EchoThe simplest use of echois to print a string as argument, for example: echo This will print in the user s browser window. ; Or equivalently: echo( This will print in the user s browser window. ); Cross- Reference07
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision j2ee hosting services

Web design - 79Chapter 5Syntax and VariablesAnd the browser displays the

Saturday, May 5th, 2007

79Chapter 5Syntax and VariablesAnd the browser displays the preceding output in exactly that order. This is because antelope is spliced into the string $saved_string, before the $animalvariable is reas- signed. In addition to splicing variable values into doubly quoted strings, PHP also replacessome special multiple-character escape sequenceswith their single-character values. Themost commonly used is the end-of-line sequence ( n ) in reading a string like: The first line nnnThe fourth line Variable interpolationWhenever an unescaped $symbol appears in a doubly quoted string, PHP tries to interpretwhat follows as a variable name and splices the current value of that variable into the string. Exactly what kind of substitution occurs depends on how the variable is set: .If the variable is currently set to a string value, that string is interpolated (or spliced) into the doubly quoted string. .If the variable is currently set to a nonstring value, the value is converted to a string, and then that string value is interpolated. .If the variable is not currently set, PHP interpolates nothing (or, equivalently, PHPsplices in the empty string). An example: $this = this ; $that = that ; $the_other = 2.2000000000; print( $this,$not_set,$that+$the_other
); produces the PHP outputthis,,that+2.2
which in turn, when seen in a browser, looks like: this,,that+2.2If you find any part of this example puzzling, it is worth working through exactly what PHPdoes to parse the string in the printstatement. First, notice that the string has four $signs, each of which is interpreted as starting a variable name. These variable names terminate atthe first occurrence of a character that is not legal in a variable name. Legal characters areletters, numbers, and underscores; the illegalterminating characters in the preceding printstring are (in order) a comma, another comma, the plus symbol (+), and a left angle bracket(<). The first two variables are bound to strings ( this and that ), so those strings arespliced in literally. The next variable ($not_set) has never been assigned, so it is omittedentirely from the string under construction. Finally, the last variable ($the_other) is discov- ered to be bound to a double that value is converted to a string ( 2.2 ), which is thenspliced into our constructed string. For more about converting numbers to strings, see the Assignment and Coercion section inChapter 25. As we said earlier in this chapter, all this interpretation of doubly quoted strings happenswhen the string is read,not when it is printed. If we saved the example string in a variableand printed it out later, it would reflect the variable values in the preceding code even if thevariables had been changed in the meantime. Cross- Reference07
Note: If you are looking for high quality webhost to host and run your jsp application check Vision jsp web hosting services

78Part IPHP: The BasicsWe could have used single (Affordable web design)

Saturday, May 5th, 2007

78Part IPHP: The BasicsWe could have used single backslashes to produce the first two backslashes in the output, but the escaping is necessary at the end of the string so that the closing quote will notbeescaped. These two escape sequences (\and ) are the onlyexceptions to the literal-mindedness ofsingly quoted strings. Doubly quoted stringsStrings that are delimited by double quotes (as in this ) are preprocessed in both the following two ways by PHP: .Certain character sequences beginning with backslash () are replaced with specialcharacters. .Variable names (starting with $) are replaced with string representations of their values. The escape-sequence replacements are: .nis replaced by the newline character .ris replaced by the carriage-return character .tis replaced by the tab character .$is replaced by the dollar sign itself ($) . is replaced by a single double-quote ( ) .\is replaced by a single backslash () The first three of these replacements make it easy to visibly include certain whitespace characters in your strings. The $sequence lets you include the $symbol when you want it, without it being interpreted as the start of a variable. The sequence is there so that youcan include a double-quote symbol without terminating your doubly quoted string. Finally, because the character starts all these sequences, you need a way to include that characterliterally, without it starting an escape sequence to do this, you preface it with itself. Just as with singly quoted strings, quotes of the opposite type can be freely included withoutan escape character: $has_apostrophe = There s no problem here ; Single versus double quotation marksPHP does some preprocessing of doubly quoted strings (strings with quotes like this ) before constructing the string value itself. For one thing, variables are replaced by their val- ues (as in the preceding example). To see that this replacement is really about the quotedstring rather than the printconstruct, consider the following code: $animal = antelope ; // first assignment$saved_string = The animal is $animal
; $animal = zebra ; // reassignmentprint( The animal is $animal
); //first display lineprint($saved_string); //second display lineWhat output would you expect here? As it turns out, your browser would display: The animal is zebraThe animal is antelopeNote07
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check Vision mysql hosting services

Simple web server - 77Chapter 5Syntax and VariablesOn the other hand, code

Friday, May 4th, 2007

77Chapter 5Syntax and VariablesOn the other hand, code like this: $authorization = NULL; // code that might or might not set $authorizationif (test_authorization($authorization)) { // code that grants a privilege of some sort} does not cause an unbound-variable warning, assuming that you have written test_ authorization()to handle arguments that might be NULL. It also makes clear to a reader of the code that you intend for the variable to lack a value unless there s a case where it isassigned. StringsStringsare character sequences, as in the following: $string_1 = This is a string in double quotes. ; $string_2 = This is a somewhat longer, singly quoted string ; $string_39 = This string has thirty-nine characters. ; $string_0 = ; // a string with zero charactersStrings can be enclosed in either single or double quotation marks, with different behavior atread time. Singly quoted strings are treated almost literally, whereas doubly quoted stringsreplace variables with their values as well as specially interpreting certain charactersequences. Singly quoted stringsExcept for a couple of specially interpreted character sequences, singly quoted strings readin and store their characters literally. The following code: $literally = My $variable will not print!\n ; print($literally); produces the browser output: My $variable will not print!nSingly quoted strings also respect the general rule that quotes of a different type will notbreak a quoted string. This is legal: $singly_quoted = This quote mark: is no big deal ; To embed a single quote (such as an apostrophe) in a singly quoted string, escape it with abackslash, as in the following: $singly_quoted = This quote mark s no big deal either ; Although in most contexts backslashes are interpreted literally in singly quoted strings, youmay also use two backslashes (\) as an escape sequence for a single (nonescaping) back- slash. This is useful when you want a backslash as the final character in a string, as in: $win_path = C:\InetPub\PHP\ ; print( A Windows-style pathname: $win_path
); which displays asA Windows-style pathname: C:InetPubPHP
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision tomcat hosting services

76Part IPHP: The BasicsDon t use doubles (Make a web site) as BooleansNote

Friday, May 4th, 2007

76Part IPHP: The BasicsDon t use doubles as BooleansNote that, although Rule 1 implies that the double 0.0 converts to a false Boolean value, it isdangerous to use floating-point expressions as Boolean expressions, due to possible roundingerrors. For example: $floatbool = sqrt(2.0) * sqrt(2.0) - 2.0; if ($floatbool) print( Floating-point Booleans are dangerous!
); elseprint( It worked … this time.
); print( The actual value is $floatbool
); The variable $floatboolis set to the result of subtracting two from the square of the squareroot of two the result of this calculation should be equal to zero, which means that$floatboolis false. Instead, the browser output we get is: Floating-point Booleans are dangerous! The actual value is 4.4408920985006E-16The value of $floatboolis very close to 0.0, but it is nonzero and, therefore, unexpectedlytrue. Integers are much safer in a Boolean role as long as their arithmetic happens only withother integers and stays within integral sizes, they should not be subject to rounding errors. NULLThe world of Booleans may seem small, since the Boolean type has only two possible values. The NULL type, however, takes this to the logical extreme: The type NULL has only one possi- ble value, which is the value NULL. To give a variable the NULLvalue, simply assign it like this: $my_var = NULL; The special constant NULLis capitalized by convention, but actually it is case insensitive; youcould just as well have typed: $my_var = null; So what is special about NULL? NULLrepresents the lackof a value. (You can think of it as thenonvalueor the unvalue.) A variable that has been assigned the value NULLis nearly indistin- guishable from a variable that has not been set at all. In particular, a variable that has beenassigned NULLhas the following properties: .It evaluates to FALSEin a Boolean context. .It returns FALSEwhen tested with IsSet(). (No other type has this property.) .PHP will not print warnings if you pass the variable to functions and back again, whereas passing a variable that has never been set will sometimes produce warnings. The NULLvalue is best used for situations where you want a variable not to have a value, inten- tionally, and you want to make it clear to both a reader of your code and to PHP that this iswhat you want. The latter point is particularly relevant when passing variables to functions. For example, the following pseudocode may print a warning (depending on your error-reportingsettings) if the variable $authorizationhas never been assigned before you pass it to yourtest_authorization()function. if (test_authorization($authorization)) { // code that grants a privilege of some sort}
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision shared web hosting services

75Chapter 5Syntax and VariablesNotice (Web design careers) that, just as with

Friday, May 4th, 2007

75Chapter 5Syntax and VariablesNotice that, just as with octal and hexadecimal integers, the read format is irrelevant oncePHP has finished reading in the numbers the preceding variables retain no memory ofwhether they were originally specified in scientific notation. In printing the values, PHP ismaking its own decisions to print the more extreme values in scientific notation, but this hasnothing to do with the original read format. BooleansBooleans are true-or-false values, which are used in control constructs like the testing portionof an ifstatement. As we will see in Chapter 6, Boolean truth values can be combined usinglogical operators to make more complicated Boolean expressions. Boolean constantsPHP provides a couple of constants especially for use as Booleans: TRUEand FALSE, whichcan be used like so: if (TRUE) print( This will always print
); elseprint( This will never print
); Interpreting other types as BooleansHere are the rules for determine the truth of any value not already of the Boolean type: .If the value is a number, it is false if exactly equal to zero and true otherwise. .If the value is a string, it is false if the string is empty (has zero characters) oris thestring 0 , and is true otherwise. .Values of type NULL are always false. .If the value is a compound type (an array or an object), it is false if it contains no othervalues, and it is true otherwise. For an object, containing a valuemeans having a member variable that has been assigned a value. .Valid resources are true (although some functions that return resources when they aresuccessful will return FALSEwhen unsuccessful). For a more complete account of converting values across types, see Chapter 25. ExamplesEach of the following variables has the truth value embedded in its name when it is used in aBoolean context. $true_num = 3 + 0.14159; $true_str = Tried and true $true_array[49] = An array element ; // see next section$false_array = array(); $false_null = NULL; $false_num = 999 999; $false_str = ; // a string zero characters longCross- Reference07
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check Vision professional web hosting services

74Part IPHP: The BasicsNote that the fact (Msn web hosting) that

Thursday, May 3rd, 2007

74Part IPHP: The BasicsNote that the fact that $even_doubleis a round number does not make it an integer. Integers and doubles are stored in different underlying formats, and the result of: $five = $even_double + 3; is a double, not an integer, even if it prints as 5. In almost all situations, however, you shouldfeel free to mix doubles and integers in mathematical expressions, and let PHP sort out thetyping. By default, doubles print with the minimum number of decimal places needed for example, the code: $many = 2.2888800; $many_2 = 2.2111200; $few = $many + $many_2; print( $many + $many_2 = $few
); produces the browser output: 2.28888 + 2.21112 = 4.5If you need finer control of printing, see the printffunction in Chapter 8. Read formatsThe typical read formatfor doubles is -X.Y, where the -optionally specifies a negative num- ber, and both Xand Yare sequences of digits between 0 and 9. The Xpart may be omitted ifthe number is between 1.0 and 1.0, and the Ypart can also be omitted. Leading or trailingzeros have no effect. All the following are legal doubles: $small_positive = 0.12345; $small_negative = -.12345$even_double = 2.00000; $still_double = 2.; In addition, doubles can be specified in scientific notation, by adding the letter eand adesired integral power of 10 to the end of the previous format for example, 2.2e-3wouldcorrespond to 2.2 10-3. The floating-point part of the number need not be restricted to arange between 1.0 and 10.0. All the following are legal: $small_positive = 5.5e-3; print( small_positive is $small_positive
); $large_positive = 2.8e+16; print( large_positive is $large_positive
); $small_negative = -2222e-10; print( small_negative is $small_negative
); $large_negative = -0.00189e6; print( large_negative is $large_negative
); The preceding code produces the following browser output: small_positive is 0.0055large_positive is 2.8E+16small_negative is -2.222E-07large_negative is 1890Cross- Reference07
Note: In case you are looking for affordable and reliable webhost to host and run your business application check Vision ftp web hosting services

Web site traffic - 73Chapter 5Syntax and Variablesmemory use and functionality were

Thursday, May 3rd, 2007

73Chapter 5Syntax and Variablesmemory use and functionality were often agonizing. The PHP designers made what we thinkis a good decision to simplify this by having only two numerical types, corresponding to thelargest of the integral and floating-point types in C. IntegersIntegersare the simplest type they correspond to simple whole numbers, both positive andnegative. Integers can be assigned to variables, or they can be used in expressions, like so: $int_var = 12345; $another_int = -12345 + 12345; // will equal zeroRead formatsIntegers can actually be read in three formats, which correspond to bases: decimal(base 10), octal(base 8) , and hexadecimal(base 16). Decimal format is the default, octal integers arespecified with a leading 0, and hexadecimals have a leading 0x. Any of the formats can be preceded by a -sign to make the integer negative. For example: $integer_10 = 1000; $integer_8 = -01000; $integer_16 = 0×1000; print( integer_10: $integer_10
); print( integer_8: $integer_8
); print( integer_16: $integer_16
); yields the browser output: integer_10: 1000integer_8: -512integer_16: 4096Note that the read format affects only how the integer is converted as it is read the valuestored in $integer_8does not remember that it was originally written in base 8. Internally, ofcourse, these numbers are represented in binary format; we see them in their base 10 conver- sion in the preceding output because that is the default for printing and incorporating intvariables into strings. RangeHow big (or small) can integers get? Because PHP integers correspond to the C longtype, which in turn depends on the word-size of your machine, this is difficult to answer defini- tively. For most common platforms, however, the largest integer is 231 1 (or 2,147,483,647), and the smallest (most negative) integer is (231 1) (or 2,147,483,647). As far as we know, there is no PHP constant (like MAXINTin C) that will tell you the largestinteger on your implementation. If you really need integers even larger or smaller than thepreceding, PHP does have some arbitrary-precision functions see the BC section of the Mathematics chapter (Chapter 27). DoublesDoublesare floating-point numbers, such as: $first_double = 123.456; $second_double = 0.456$even_double = 2.0;
Note: If you are looking for high quality webhost to host and run your jsp application check Vision florida web design services

Mac os x web server - 72Part IPHP: The BasicsThe substrfunction is designed to

Thursday, May 3rd, 2007

72Part IPHP: The BasicsThe substrfunction is designed to take a string of characters as its first input and return asubstring of that string, with the start point and length determined by the next two inputs tothe function. Instead of handing the function a character string, however, we gave it the inte- ger 12345. What happens? As it turns out, there is no error, and we get the browser output: sub is 34Because substrexpects a character string rather than an integer, PHP converts the number12345to the character string 12345 , which substrthen slices and dices. Because of this automatic type conversion, it is very difficult to persuade PHP to give a typeerror in fact, PHP programmers need to exercise a little care sometimes to make sure thattype confusions do not lead to error-free but unintended results. Type SummaryPHP has a total of eight types: integers, doubles, Booleans, strings, arrays, objects, NULL, andresources. .Integersare whole numbers, without a decimal point, like 495. .Doublesare floating-point numbers, like 3.14159 or 49.0. .Booleanshave only two possible values: TRUEand FALSE. .NULLis a special type that only has one value: NULL. .Stringsare sequences of characters, like PHP4.0supportsstringoperations. .Arraysare named and indexed collections of other values. .Objectsare instances of programmer-defined classes, which can package up both otherkinds of values and functions that are specific to the class. .Resourcesare special variables that hold references to resources external to PHP (suchas database connections). Of these, the first five are simple types, and the next two (arrays and objects) are compound the compound types can package up other arbitrary values of arbitrary type, whereas thesimple types cannot. We treat only the simple types in this chapter, since arrays (Chapter 9) and objects (Chapter 20) need chapters all to themselves. Finally, the thorniest details of thetype system, including discussion of the resource type, are deferred to Chapter 25. The Simple TypesThe simple types in PHP (integers, doubles, Booleans, NULL, and strings) should mostly befamiliar to those with programming experience (although we will not assume that experienceand will explain them in detail). The only thing likely to surprise C programmers is how fewtypes there are in PHP. Many programming languages have several different sizes of numerical types, with the largerones allowing a greater range of values, but also taking up more room in memory. For exam- ple, the C language has a shorttype (for relatively small integers), a longtype (for possiblylarger integers), and an inttype (which might be intermediate, but in practice is sometimesidentical either to the shortor longtype). It also has floating-point types, which vary intheir precision. This kind of typing choice made sense in an era when tradeoffs between07
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision best web hosting services