PHP 內建了數百個函數,涵蓋字串處理、陣列操作、檔案系統、資料庫存取、XML 解析、數學運算等領域。本文將常用函數按功能分類整理,方便開發者快速查找與參考。
字串操作是 Web 開發中最頻繁的任務。PHP 提供了豐富的字串函數:
strtoupper()、strtolower()、ucfirst()、ucwords()strpos()、strrpos()、stripos()、strripos()、str_replace()、str_ireplace()、substr_replace()substr()、substr_count()、substr_compare()、explode()、implode()、str_split()、strtok()strcmp()、strcasecmp()、strnatcmp()、strnatcasecmp()、strncmp()、strncasecmp()htmlspecialchars()、htmlentities()、html_entity_decode()、addslashes()、stripslashes()、quotemeta()、urlencode()、urldecode()sprintf()、printf()、vsprintf()、vprintf()、number_format()、money_format()strlen()、strrev()、str_shuffle()、str_rot13()、str_word_count()、similar_text()、soundex()、md5()、sha1()、crc32()陣列是 PHP 中最靈活的資料結構,相關函數極其豐富:
array()、array_merge()、array_merge_recursive()、array_combine()、array_fill()、array_pad()、range()、compact()array_keys()、array_values()、array_flip()、array_change_key_case()、array_key_exists()、in_array()、array_search()sort()、rsort()、asort()、arsort()、ksort()、krsort()、natsort()、natcasesort()、usort()、uasort()、uksort()、array_multisort()array_filter()、array_map()、array_walk()、array_walk_recursive()、array_reduce()count()、sizeof()、array_count_values()、array_sum()、array_product()array_diff()、array_diff_assoc()、array_diff_key()、array_diff_uassoc()、array_diff_ukey()、array_intersect()、array_intersect_assoc()、array_intersect_key()、array_intersect_uassoc()、array_intersect_ukey()array_push()、array_pop()、array_shift()、array_unshift()array_slice()、array_splice()、array_chunk()current()、next()、prev()、reset()、end()、each()、key()、pos()array_unique()、array_reverse()、array_rand()、shuffle()、extract()、list()abs()、ceil()、floor()、round()、fmod()、min()、max()、pow()、sqrt()、exp()、log()、log10()、pi()sin()、cos()、tan()、asin()、acos()、atan()、atan2()、sinh()、cosh()、tanh()、asinh()、acosh()、atanh()decbin()、dechex()、decoct()、bindec()、hexdec()、octdec()、base_convert()rand()、mt_rand()、srand()、mt_srand()、mt_getrandmax()、lcg_value()hypot()、deg2rad()、rad2deg()、is_nan()、is_infinite()、is_finite()time()、microtime()、mktime()、gmmktime()、strtotime()date()、gmdate()、strftime()、gmstrftime()、idate()getdate()、gettimeofday()、localtime()、strptime()、checkdate()date_default_timezone_set()、date_default_timezone_get()jdtounix()、unixtojd()、cal_to_jd()、cal_from_jd()、cal_days_in_month()、cal_info()、easter_date()、easter_days()、frenchtojd()、gregoriantojd()、jdtogregorian()、jdtofrench()、jdtomonthname()、jddayofweek()、jewishtojd()、juliantojd()date_sunrise()、date_sunset()fopen()、fclose()、popen()、pclose()、tmpfile()、tempnam()fread()、fgets()、fgetc()、fgetcsv()、fgetss()、fscanf()、readfile()、file()、file_get_contents()、fpassthru()fwrite()、fputs()、fputcsv()、file_put_contents()fseek()、ftell()、rewind()、feof()、fflush()stat()、fstat()、lstat()、filesize()、filemtime()、fileatime()、filectime()、fileperms()、fileowner()、filegroup()、fileinode()、filetype()、realpath()、pathinfo()、basename()、dirname()mkdir()、rmdir()、opendir()、closedir()、readdir()、rewinddir()、scandir()、glob()、chdir()、getcwd()、chroot()link()、symlink()、readlink()、linkinfo()、chmod()、chown()、chgrp()、umask()file_exists()、is_file()、is_dir()、is_readable()、is_writable()、is_executable()、is_link()、is_uploaded_file()unlink()、rename()、copy()、move_uploaded_file()、parse_ini_file()、set_file_buffer()、flock()、fnmatch()、disk_free_space()、disk_total_space()、clearstatcache()、touch()、ftruncate()注意:mysql_* 系列函數自 PHP 5.5 起棄用,PHP 7.0 移除。請改用 mysqli 或 PDO_MySQL。
mysql_connect()、mysql_pconnect()、mysql_close()、mysql_select_db()mysql_query()、mysql_unbuffered_query()、mysql_real_escape_string()mysql_fetch_array()、mysql_fetch_assoc()、mysql_fetch_row()、mysql_fetch_object()、mysql_fetch_field()、mysql_num_rows()、mysql_num_fields()、mysql_free_result()、mysql_data_seek()、mysql_result()mysql_error()、mysql_errno()mysql_insert_id()、mysql_affected_rows()、mysql_list_dbs()、mysql_list_tables()、mysql_list_fields()、mysql_get_server_info()、mysql_get_client_info()、mysql_get_host_info()、mysql_get_proto_info()、mysql_ping()、mysql_stat()、mysql_thread_id()、mysql_client_encoding()xml_parser_create()、xml_parser_create_ns()、xml_parser_free()、xml_parse()、xml_parse_into_struct()、xml_set_element_handler()、xml_set_character_data_handler()、xml_set_default_handler()、xml_set_processing_instruction_handler()、xml_set_unparsed_entity_decl_handler()、xml_set_notation_decl_handler()、xml_set_external_entity_ref_handler()、xml_set_object()、xml_parser_set_option()、xml_parser_get_option()、xml_get_error_code()、xml_error_string()、xml_get_current_line_number()、xml_get_current_column_number()、xml_get_current_byte_index()simplexml_load_file()、simplexml_load_string()、simplexml_import_dom()、SimpleXMLElement::__construct()、SimpleXMLElement::addChild()、SimpleXMLElement::addAttribute()、SimpleXMLElement::asXML()、SimpleXMLElement::attributes()、SimpleXMLElement::children()、SimpleXMLElement::getName()、SimpleXMLElement::getNamespaces()、SimpleXMLElement::getDocNamespaces()、SimpleXMLElement::registerXPathNamespace()、SimpleXMLElement::xpath()zip_open()、zip_close()、zip_read()、zip_entry_open()、zip_entry_close()、zip_entry_read()、zip_entry_name()、zip_entry_filesize()、zip_entry_compressedsize()、zip_entry_compressionmethod()ftp_connect()、ftp_ssl_connect()、ftp_login()、ftp_close()、ftp_pasv()、ftp_get()、ftp_fget()、ftp_put()、ftp_fput()、ftp_nb_get()、ftp_nb_fget()、ftp_nb_put()、ftp_nb_fput()、ftp_nb_continue()、ftp_delete()、ftp_rename()、ftp_mkdir()、ftp_rmdir()、ftp_chdir()、ftp_cdup()、ftp_pwd()、ftp_nlist()、ftp_rawlist()、ftp_size()、ftp_mdtm()、ftp_chmod()、ftp_site()、ftp_raw()、ftp_exec()、ftp_alloc()、ftp_set_option()、ftp_get_option()、ftp_quit()、ftp_systype()header()、headers_sent()、headers_list()、setcookie()、setrawcookie()mail()error_reporting()、error_log()、error_get_last()、set_error_handler()、restore_error_handler()、set_exception_handler()、restore_exception_handler()、debug_backtrace()、debug_print_backtrace()filter_var()、filter_var_array()、filter_input()、filter_input_array()、filter_list()、filter_id()、filter_has_var()exit()、die()、sleep()、usleep()、time_nanosleep()、time_sleep_until()、ignore_user_abort()、connection_status()、connection_aborted()define()、defined()、constant()highlight_file()、highlight_string()、show_source()、strip_whitespace()eval()get_browser()uniqid()pack()、unpack()utf8_encode()、utf8_decode()setlocale()、localeconv()、nl_langinfo()libxml_use_internal_errors()、libxml_get_errors()、libxml_get_last_error()、libxml_clear_errors()查找函數時,建議優先使用 PHP 官方手冊,該手冊提供完整的參數說明、返回值、範例代碼及版本資訊。現代 PHP 開發應避免使用已棄用的函數(如 mysql_*、ereg_* 等),改用對應的新擴展或物件導向介面。