110Part IPHP: The BasicsThe default (Java web server) error-reporting setting in

110Part IPHP: The BasicsThe default error-reporting setting in PHP5 reports on every kind of error except runtimenotices, which are the least serious condition that is detected. The reason you see warningsabout too few arguments to a function is that this is treated as a runtime-warning situation(the next most serious category). If you really need function calls that sometimes provide toofew arguments and seeing warnings is unacceptable, you have two options for suppressingthe warnings: .You can temporarily change the value of error reporting in your script, with a statementlike error_reporting(E_ALL-(E_NOTICE+E_WARNING));.This will turn off both run- time notices and runtime warnings from the point where it appears in your script up tothe next error_reporting()statement (if any). (Note that this is dangerous, as lots ofother problems might produce warnings besides the one you re interested in.) .You can suppress errors for any single expression by using the error-control operator@, which you can put in front of any expression to suppress errors from that expressiononly. For example, if the function call my_function()is producing a warning, @my_function()will not. Note that this is dangerous as well because all types oferrors except for parse errors will be suppressed. We don t advise using either of these workarounds, but we provide them because we are suchnonjudgmental people by nature. PHP actually provides ways to write functions that expect vari- able numbers of arguments (see the Variable Numbers of Arguments section in Chapter 26), and using them is a much better idea than shooting the messenger. Rather than decreasing PHP s reportage of errors, we advise increasing it to the maximumlevel possible when you are developing new code. You can do this globally by changing the php.inifile (see Chapter 30) or simply by including the statement error_reporting(E_ALL);at the top of your scripts. Among other things, this increase in reportage willmean that you will be warned about variables you have forgotten to assign, which is one ofthe most frequent causes of time-wasting bugs. Too many argumentsIf you hand too many arguments to a function, the excess arguments will simply be ignored, even when error reporting is set to E_ALL. As we will see in Chapter 26, this tolerance turnsout to be helpful in defining functions that can take a variable number of arguments. Functions and Variable ScopeAs we said in Chapter 5, outside of functions, the rules about variable scope are simple: Assign a variable anywhere in the execution of a PHP code file, and the value will be there for you later in that file s execution. The rules become somewhat more complicated in thebodies of function definitions, but not much. The basic principle governing variables in function bodies is: Each function is its own littleworld. That is, barring some special declarations, the meaning of a variable name inside afunction has nothing to do with the meaning of that name elsewhere. (This is a feature, not abug you want functions to be reusable in different contexts, and so having the behavior beindependent of the context is a good thing. If not for this kind of scoping, you would waste alot of time chasing down bugs caused by using the same variable name in different parts ofyour code.) Tip08
In case you need affordable webhost to host your website, our recommendation is ecommerce web host services.

Leave a Reply