Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 30 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
30
Dung lượng
369,19 KB
Nội dung
Appendix B: PHP Quick Reference 691 Functions You can create and call functions using the following syntax: function funcname() // defines the function { // line of php code; // line of php code; } funcname(); // calls the function to execute Values can be passed in and returned from functions: function add($value1, $value2) // add two numbers together { $value3 = $value1 + $value2; return $value3; } $val = funcname(1, 1); // $val = 2 Classes You can define new objects and use its methods using the following syntax: class ClassName() // class definition { public $var1; // property with public access private $var2; // property with private access // constructor method public function __construct() { // code to initialize object goes here } // public method public function setFoo($value) { // properties and methods are accessed inside the class // using $this- > $this- > var2 = $value; } // private method private function bar() { // more code goes here } // destructor method public function __destruct() { // clean up code goes here } } bapp02.indd 691bapp02.indd 691 12/10/08 5:35:01 PM12/10/08 5:35:01 PM Appendix B: PHP Quick Reference 692 $c = new ClassName(); // create a new instance of the object $c- > var1 = 42; // properties and methods are accessed using - > $c- > setFoo(‘Hello World’); Namespaces Namespaces are one of the more recent additions to PHP ’ s syntax (added in version 5.3) and help prevent name clashing. Namespaces can contain class, constant, and function definitions. Namespaces are declared with the namespace keyword at the beginning of a file: namespace MyProject\Foo\Bar; function fizz() { // } When using functions and classes that are defined within a namespace, they can be referenced by their full name: $var = MyProject::Foo::Bar::fizz(); Namespaces can be imported with the use keyword: use MyProject\Foo\Bar; $var = fizz(); Namespaces can be aliased with use / as : use MyProject\Foo\Bar as my; $var = my\fizz(); Using MySQL This is the basic sequence for connecting to a MySQL database, executing a SELECT query and displaying the results: // connect to MySQL $db = mysql_connect(‘localhost’, ‘username’, ‘password’) or die (‘Unable to connect. Check your connection parameters.’); // select the correct database mysql_select_db(‘database’, $db) or die(mysql_error($db)); // query the database $query = ‘SELECT column1, column2 FROM table ORDER BY column1 ASC’; $result = mysql_query($query, $db) or die (mysql_error($db)); bapp02.indd 692bapp02.indd 692 12/10/08 5:35:01 PM12/10/08 5:35:01 PM Appendix B: PHP Quick Reference 693 // check if any rows were returned if (mysql_num_rows($result) > 0) { // cycle through the returned records while ($row = mysql_fetch_assoc($result)) { echo ‘column1: ‘ . $row[‘column1’] . ‘ < br/ > ’; echo ‘column2: ‘ . $row[‘column2’] . ‘ < br/ > ’; } } // free the result resource mysql_free_result($result); // disconnect from MySQL mysql_close($db); bapp02.indd 693bapp02.indd 693 12/10/08 5:35:01 PM12/10/08 5:35:01 PM badvert.indd 811badvert.indd 811 12/12/08 10:51:35 AM12/12/08 10:51:35 AM C PHP 6 Functions For your convenience, we have listed many (but not all) of PHP ’ s functions in this appendix to serve as a quick reference. PHP has numerous other functions available to you, and you can find a complete listing in the manual at its web site, www.php.net/manual/en/funcref.php . Each table presented here lists the function ’ s signature in a format similar to that used in the official PHP documentation and a brief description of what the function does. Functions that are deprecated and should no longer be used are not listed here. Please note that some functions are designed to work only on Linux, and some only on Windows. If you encounter otherwise unexplained errors in your code, we recommend checking the function ’ s documentation to ensure that it is available and fully compatible with your platform. Apache/ PHP Functions PHP Function Description bool apache_child_terminate(void) Stop the Apache server from running after the PHP script has been executed. array apache_get_modules(void) Return an array of loaded Apache modules. string apache_get_version(void) Return the version of Apache that is currently running. string apache_getenv(string $variable[, bool $walk_to_top]) Return an Apache subprocess environment variable as specified by $variable . object apache_lookup_uri(string $filename) Return information about the URI in $filename as an object. bapp03.indd 695bapp03.indd 695 12/10/08 5:33:40 PM12/10/08 5:33:40 PM Appendix C: PHP6 Functions 696 PHP Function Description string apache_note(string $notename[, string $ value]) Return or set values in the Apache notes tables. array apache_request_headers(void) Return all HTTP headers as an associative array. array apache_response_headers(void) Return all HTTP response headers as an associative array. bool apache_setenv(string $variable, $value[, bool $walk_to_top]) Set an Apache subprocess environment variable. int ascii2ebcdic(string $string) Convert an ASCII - coded string to EBCDIC (only available on EBCDIC - based operating systems). int ebcdic2ascii(string $string) Convert an EBCDIC - coded string to ASCII (only available on EBCDIC - based operating systems). array getallheaders() Return all HTTP request headers as an associative array. bool virtual(string $filename) Perform an Apache subrequest, useful for including the output of CGI scripts or .shtml files. Array Functions Function Description array array([mixed $ ]) Create an array of values. array array_change_key_case(array $array[, int $case]) Convert the keys in an array to either all uppercase or all lowercase. The default is lowercase. array array_chunk(array $array, int $size[, bool $keep_keys]) Split an array into specifically sized chunks. array array_combine(array $keys, array $values) Combine two arrays with equal number of keys and values, using the values from one array as keys and the other ’ s as values. array array_count_values(array $array) Return an associative array of values as keys and their count as values. bapp03.indd 696bapp03.indd 696 12/10/08 5:33:41 PM12/10/08 5:33:41 PM Appendix C: PHP6 Functions 697 Function Description array array_diff(array $array1, array $array2[, array $ ]) Return the values from the first array that do not appear in subsequent arrays. Opposite of array_intersect() . array array_diff_assoc(array $array1, array $array2[, array $ ]) Return the values from the first array that do not appear in subsequent arrays, taking key values into account. array array_diff_key(array $array1, array $array2[, array $ ]) Return the keys from the first array that do not appear in subsequent arrays. array array_diff_uassoc(array $array1, array $array2[, array $ ], string $function_name) Return the keys from the first array that do not appear in subsequent arrays. Use supplied callback function to compare keys, instead of PHP ’ s internal algorithm. array array_diff_ukey(array $array1, array $array2[, array $ ], string $function_name) Return the keys from the first array that do not appear in subsequent arrays. Use supplied callback function to compare keys, instead of PHP ’ s internal algorithm. array array_fill(int $start, int $count, mixed $value) Return an array filled with $value . array array_fill_keys(array $keys, mixed $value) Return an array filled with $ value, using values of the $keys array as keys. array array_filter(array $array[, string $function_name]) Return an array of values filtered by $function_name . If the callback function returns true, then the current value from $array is returned into the result array. Array keys are preserved. array array_flip(array $array) Flip an array ’ s values and keys and return the result as an array. array array_intersect(array $array1, array $array2[, array $ ]) Return the values from the first array that appear in subsequent arrays. Opposite of array_diff() . array array_intersect_assoc(array $array1, array $array2[, array $ ]) Return the values from the first array that appear in subsequent arrays. Unlike array_ intersect() , this function takes key values into account. array array_intersect_key(array $array1, array $array2[, array $ ]) Return the keys from the first array that appear in subsequent arrays. bapp03.indd 697bapp03.indd 697 12/10/08 5:33:41 PM12/10/08 5:33:41 PM Appendix C: PHP6 Functions 698 Function Description array array_intersect_uassoc(array $array1, array $array2[, array $ ], string $function_name) Return the keys from the first array that appear in subsequent arrays. Use supplied callback function to compare keys, instead of PHP ’ s internal algorithm. array array_intersect_ukey(array $array1, array $array2[, array $ ], string $function_name) Return the keys from the first array that appear in subsequent arrays. Use supplied callback function to compare keys, instead of PHP ’ s internal algorithm. bool array_key_exists(mixed $key, array $array) Verify whether a key exists in an array. array array_keys(array $array [, mixed $search [, bool $strict]]) Return keys of array $array as an array. If $search is provided, then only those keys containing those values will be returned. array array_map(string $function_ name, array $array1[, array $ ]) Return an array containing elements from the supplied arrays that fit the applied criterion. array array_merge(array $array1[, $array2[, array $ ]]) Merge arrays together and return the results as an array. If two arrays have the same associative keys, then the later array will overwrite an earlier key. If two arrays have the same numeric keys, then the array will be appended instead of overwritten. array array_merge_recursive(array $array1[, arrary $ ]) Similar to array_merge() , but the values of the arrays are appended. bool array_multisort(array $array [, mixed $parameter[, mixed $ ]]) Sort either a complex multidimensional array or several arrays at one time. Numeric keys will be reindexed, but associative keys will be maintained. array array_pad(array $array, int $pad_size, mixed $pad_value) Return a copy of an array padded to size $pad_ size with $pad_value . mixed array_pop(array & $array) Shorten an array by popping and returning its last value. Opposite of array_push(.) . number array_product(array $array) Return the product of an array ’ s values. int array_push(array & $array, mixed $variable[, mixed $ ]) Extend an array by pushing variables on to its end, and return the new size of the array. Opposite of array_pop() . mixed array_rand(array $array[, int $number]) Return a random key from an array (an array of random keys is returned if more than one value is requested). bapp03.indd 698bapp03.indd 698 12/10/08 5:33:42 PM12/10/08 5:33:42 PM Appendix C: PHP6 Functions 699 Function Description mixed array_reduce(array $array, string $function_name) Reduce an array to a single function, using a supplied callback function. array array_reverse(array $array[, bool $preserve_keys]) Return an array with its elements in reverse order. mixed array_search(mixed $search, array $array[, bool $strict]) Search an array for the given value, and return the key if it is found. mixed array_shift(array & $array) Similar to array_pop() , except that this shortens an array by returning its first value. Opposite of array_unshift() . array array_slice(array $array, int $offset[, int $length[, bool $preserve_keys]]) Return a subset of the original array. array array_splice(array & $array, int $offset[, int $length[, mixed $new_values]]) Remove a section of an array and replace it with new values. number array_sum(array $array) Calculate the sum of the values in an array. array array_udiff(array $array1, array $array2[, array $ ], string $function_name) Return the values from the first array that do not appear in subsequent arrays, using the provided callback function to perform the comparison. array array_udiff_assoc(arrays $array1, array $array2[, array $ ], string $function_name) Return values from the first array that do not appear in subsequent arrays, using the provided callback function to perform the comparison. Unlike array_udiff() , the array ’ s keys are used in the comparison. array array_udiff_assoc(arrays $array1, array $array2[, array $ ], string $value_compare, string $key_compare) Return the values from the first array that do not appear in subsequent arrays, using the provided callback functions to perform the comparison ( $data_compare is used to compare values, and $key_compare is used to compare keys). array array_uintersect(array $array1, array $array2[, array $ ], string $function_name) Return the intersection of arrays through a user - defined callback function. array array_uintersect_assoc(array $array1, array $array2[, array $ ], string $function_name) Return the intersection of arrays with additional index checks, using the provided callback function to perform the comparison. bapp03.indd 699bapp03.indd 699 12/10/08 5:33:42 PM12/10/08 5:33:42 PM Appendix C: PHP6 Functions 700 Function Description array array_uintersect_uassoc(array $array1, array $array2[, array $ ], string $value_compare, string $key_compare) Return the intersection of arrays with additional index checks, using the provided callback functions to perform the comparison ( $data_compare is used to compare values, and $key_compare is used to compare keys). array array_unique(array $array) Return a copy of an array excluding any duplicate values. mixed array_unshift(array & $array, mixed $variable[, mixed $ ]) Similar to array_push () except that this adds values to the beginning of an array. Opposite of array_unshift() . array array_values(array $array) Return a numerically indexed array of the values from an array. bool array_walk(array $array, string $function_name[, mixed $parameter]) Apply a named function to every value in an array. bool array_walk_recursive(array $array, string $function_name[, mixed $parameter]) Apply a named function recursively to every value in an array. bool arsort(array & $array[, int $sort_flags]) Sort an array in descending order, while maintaining the key/value relationship. bool asort(array & $array[, int $sort_flags]) Sort an array in ascending order, while maintaining the key/value relationship. array compact(mixed $variable[, mixed $ ]) Merge variables into an associative array. Opposite of extract() . int count(mixed $array[, int $mode]) Return the number of values in an array or the number of properties in an object. mixed current(array & $array) Return the current value in an array. array each(array & $array) Return the current key and value pair of an array, and advance the array ’ s internal pointer. mixed end(array & $array) Advance an array ’ s internal pointer to the end of an array, and return the array ’ s last value. int extract(array $array[, int $extract_type[, string $prefix]]) Import values from an associative array into the symbol table. The $extract_type option provides directions if there is a conflict. bool in_array(mixed $search, array $haystack[, bool $strict]) Return whether a specified value exists in an array. mixed key(array & $array) Return the key for the current value in an array. bapp03.indd 700bapp03.indd 700 12/10/08 5:33:43 PM12/10/08 5:33:43 PM [...]... absolute pathname bool rename(string $old_name, string $new_name[, resource $context]) Rename a file bool rewind(resource $handle) Move the pointer to the beginning of a file stream void rewinddir([resource $directory]) Reset the directory stream to the beginning of the directory bool rmdir(string $directory[, resource $context]) Delete a directory array scandir(string $directory[, int $sort_order[, resource... Return an array containing information about the last error that occurred bool error_log(string $message[, int $message_type[, string $destination[, string $extra_ headers]]]) Write an error message to the web server ’s log, send an e-mail, or post to a file, depending on $message_type and $destination int error_reporting([int $level]) Set the error_reporting directive at run time for the duration of the . myfizz(); Using MySQL This is the basic sequence for connecting to a MySQL database, executing a SELECT query and displaying the results: // connect to MySQL $db = mysql_ connect(‘localhost’,. correct database mysql_ select_db(‘database’, $db) or die (mysql_ error($db)); // query the database $query = ‘SELECT column1, column2 FROM table ORDER BY column1 ASC’; $result = mysql_ query($query,. $db) or die (mysql_ error($db)); bapp02.indd 692bapp02.indd 692 12/10/08 5:35:01 PM12/10/08 5:35:01 PM Appendix B: PHP Quick Reference 693 // check if any rows were returned if (mysql_ num_rows($result)