[ Pobierz całość w formacie PDF ]
.(One uses a SAXapproach, the other a DOM approach).If you need LDAP, IMAP, or PDF functions,there is a function set for you.Additionally, PHP has an API (application programinterface) for about every relational database on the planet.But really, there s noneed to cover most of these functions in this book.Another thing to keep in mind is that the function set is changing almost daily.PHP 4 is internally structured in a way that makes it extremely easy for program-mers to add additional functions.In fact, if you know your way around C, youcould probably add a new function into PHP in a few hours.So you can expect reg-ular additions to the core function set.Your best friend, as always, is the online PHP manual: http://www.php.net/manual.It s is the only source where you can be sure that the list of func-tions will be more or less up to date.If you want to go directly to the explanationof a function, all you need to do is point your browser at http://www.php.net/function_name.We want to point out one more thing before we get started here.There are sevenapplications in the final two portions of this book.In the course of creating thesefunctions, we made use of a little over 100 of PHP s built-in functions.So whilethere are thousands of built-in functions, you will probably only make regular useof a relatively small number.A pretty neat resource is the function table at:http://www.zugeschaut-Tipund-mitgebaut.de/php/.1113537-4 ch06.f.qc 12/15/00 15:22 Page 112112 Part II: Working with PHPFunction BasicsFunctions all take the same basic form.return_type function_name(argument1, argument2, argument3)First there is the function s name; note that the name of the function is not case-sensitive.However, we don t know of any programmer who ever uses uppercase lettersto refer to a built-in function.Next there is a set of parentheses.Every function will have a set of parenthesesmarking the beginning and end of the arguments.ArgumentsSo what s an argument? An argument is simply a value that the function is expect-ing.Depending on the purpose of the function, it may expect zero, one, two, three,or more arguments, and any of the arguments may be any variable type maybe astring, maybe an integer, or maybe an array.To give you a better idea of what argu-ments are, let s look at a very useful function for string handling.The str_replace() function is extremely helpful.Let s say you had the follow-ing string:$str = My name is Jay. ;Say that in the $str variable you need to replace Jay with John.Within $stryou need to search for Jay and replace it with John.Here, you would expect afunction to take three arguments: the string to be searched for, the replacementstring, and the string to be searched through.It so happens that in PHP, the argu-ments come in this order:str_replace(string to search for, replacement string, string to besearched through);Or to put it in practice:$str = My name is Jay. ;$new_str = str_replace( Jay , John , $str);Keep in mind that certain functions will have optional arguments and a few willtake no arguments at all.The substr() function, for example, has an optional thirdargument.This function returns a portion of a string by its ordinal references.Toget everything from the second character to the next-to-last character, you woulduse the following://note, the first character in the string is 0$new_str = substr ($str_var,1,-1);3537-4 ch06.f.qc 12/15/00 15:22 Page 113Chapter 6: PHP s Built-in Functions 113However, to get everything from the second character on, you would use the fol-lowing:$str = substr ($str_var,1);So in this function the third argument is optional.(We ll point out optional argu-ments as we move through the functions.) The details of working with substr()will be covered later in the chapter.There are also a few occasions when a function will take no arguments at all.Agood example of this is phpinfo().It spits out everything you need to know aboutyour PHP environment without taking any arguments.Another good example istime(), which returns the current Unix timestamp.Return valuesThe final thing you should be aware of is what the function will return.In theabove case, str_replace() will return a string.What you do with this string isyour business.You could assign it a variable or print it out, or do whatever elseseems appropriate.//assign to variable$new_str = str_replace( Jay , John , $str);//print directlyecho str_replace( Jay , John , $str);Note that functions may return arrays, integers, doubles (floating-point num-bers), objects, or sometimes Boolean values.In Chapter 5, you saw a good exampleof a function that returns a Boolean value (that is, TRUE or FALSE).If you want todetermine whether a variable is an array, you can use the is_array() function.if (is_array($var)){//process array}There are also functions that will return a value if there is a value to be returned,and that will return FALSE if there is no value to be returned.A good example ofthis is the mysql_fetch_array() function
[ Pobierz całość w formacie PDF ]