76Part IPHP: The BasicsDon t use doubles (Make a web site) as BooleansNote
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