Archive for July, 2007

79Chapter 5Syntax and VariablesAnd the browser displays (X web hosting) the

Friday, July 27th, 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
From our experience, we are can tell you that you can find a reliable and cheap webhost service at Java Web Hosting services.

78Part IPHP: The BasicsWe could have used single (Web site builder)

Thursday, July 26th, 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
Check Tomcat Web Hosting services for best quality webspace to host your web application.

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

Thursday, July 26th, 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
We highly recommend you visit web and email hosting services if you need stable and cheap web hosting platform for your web applications.

Cpanel web hosting - 76Part IPHP: The BasicsDon t use doubles as BooleansNote

Wednesday, July 25th, 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}
Check Tomcat Web Hosting services for best quality webspace to host your web application.

75Chapter 5Syntax and (Web server setup) VariablesNotice that, just as with

Wednesday, July 25th, 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
If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.

Adult web hosting - 74Part IPHP: The BasicsNote that the fact that

Tuesday, July 24th, 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
Go visit our java server pages services for a reliable, lowcost webhost to satisfy all your needs.

73Chapter 5Syntax and Variablesmemory use and functionality were (Web server address)

Tuesday, July 24th, 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;
In case you need affordable webhost to host your website, our recommendation is ecommerce web host services.

72Part IPHP: (Ecommerce web host) The BasicsThe substrfunction is designed to

Monday, July 23rd, 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
From our experience, we can recommend PHP5 Web Hosting services, if you need affordable webhost to host and run your web application.

Web hosting solutions - 71Chapter 5Syntax and VariablesThis is identical to calling

Monday, July 23rd, 2007

71Chapter 5Syntax and VariablesThis is identical to calling error_reporting()on the integer value of E_ALL, but is betterbecause the actual value of E_ALLmay change from one version of PHP to the next. It s also possible to create your own constants using the define()form, although this ismore unusual than referring to built-in constants. The code: define(MY_ANSWER, 42); would cause MY_ANSWERto evaluate to 42 everywhere it appears in your code. There is noway to change this assignment after it has been made, and like variables, constants that arenot part of PHP itself do not persist across pages unless they are explicitly passed to a newpage. Ultimately, you probably will not need to define constants very often, if ever. When cre- ated constants are used, they are generally most usefully defined in an external include fileand might be used for such information as a sales-tax rate or perhaps an exchange rate. Types in PHP: Don t Worry, Be HappyAll programming languages have some kind of type system, which specifies the different kindsof values that can appear in programs. These different types often correspond to different bit- level representations in computer memory, although in many cases programmers are insu- lated from having to think about (or being able to mess with) representations in terms of bits. PHP s type system is simple, streamlined, and flexible, and it insulates the programmer fromlow-level details. PHP makes it easy not to worry too much about typing of variables and val- ues, both because it does not require variables to be typed and because it handles a lot oftype conversions for you. No variable type declarationsAs you saw in Chapter 4, the type of a variable does not need to be declared in advance. Instead, the programmer can jump right ahead to assignment and let PHP take care of figuring out the type of the expression assigned: $first_number = 55.5; $second_number = Not a number at all ; Automatic type conversionPHP does a good job of automatically converting types when necessary. Like most other modern programming languages, PHP will do the right thing when, for example, doing mathwith mixed numerical types. The result of the expression$pi = 3 + 0.14159is a floating-point (double) number, with the integer 3implicitly converted into floating pointbefore the addition is performed. Types assigned by contextPHP goes further than most languages in performing automatic type conversions. Consider: $sub = substr(12345, 2, 2); print( sub is $sub
);
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.

Frontpage web hosting - 70Part IPHP: The Basicsaccomplish this, and the different

Monday, July 23rd, 2007

70Part IPHP: The Basicsaccomplish this, and the different techniques are a lot of what the rest of this book is about. For example, you can pass information from page to page using GETand POSTvariables(Chapter 7), store information persistently in a database (all of Part II of this book), associateit with a user s session using PHP s session mechanism (Chapter 24), or store it on a user shard disk via a cookie (Chapter 24). Functions and variable scopeExcept inside the body of a function, variable scope in PHP is quite simple: Within any givenexecution of a PHP file, just assign a variable, and its value will be there for you later. Wehaven t yet covered how to define your own functions, but it s worth a look-ahead note: Variables assigned within a function are localto that function, and unless you make a specialdeclaration in a function, that function won t have access to the global variables defined outside the function, even when they are defined in the same file. (We will discuss the scopeof variables in functions in depth when we cover function definitions in Chapter 6.) You can switch modes if you wantOne scoping question that we had the first time we saw PHP code was: Does variable scopepersist across tags? For example, we have a single file that looks like: ); ?> Should we expect our assignment to $usernameto survive through the second of the twoPHP-tagged areas? The answer is yes variables persist throughout a thread of PHP execu- tion (in other words, through the whole process of producing a Web page in response to auser s request). This is a single manifestation of a general PHP rule, which is that the onlyeffect of the tags is to let the PHP engine know whether you want your code to be interpretedas PHP or passed through untouched as HTML. You should feel free to use the tags to switchback and forth between modes whenever it is convenient. ConstantsIn addition to variables, which may be reassigned, PHP offers constants, which have a singlevalue throughout their lifetime. Constants do not have a $before their names, and by conven- tion the names of constants usually are in uppercase letters. Constants can contain onlyscalar values (numbers and string). Constants have global scope, so they are accessibleeverywhere in your scripts after they have been defined even inside functions. For example, the built-in PHP constant E_ALLrepresents a number that indicates to theerror_reporting()function that all errors and warnings should be reported. A call toerror_reporting()might look like this: error_reporting(E_ALL);
We highly recommend you visit web and email hosting services if you need stable and cheap web hosting platform for your web applications.