首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

PHP 常用函數完整參考指南

     編輯:PHP 相關知識

PHP 核心函數分類速查

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 (舊版 mysql_* 擴展,已棄用)

注意: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 解析函數

  • SAX 解析器: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: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: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: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()
  • HTTP 與 Cookie: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()
  • 唯一 ID:uniqid()
  • 二進制資料:pack()、unpack()
  • 字元編碼:utf8_encode()、utf8_decode()
  • 本地化:setlocale()、localeconv()、nl_langinfo()
  • libxml 錯誤處理:libxml_use_internal_errors()、libxml_get_errors()、libxml_get_last_error()、libxml_clear_errors()

使用建議

查找函數時,建議優先使用 PHP 官方手冊,該手冊提供完整的參數說明、返回值、範例代碼及版本資訊。現代 PHP 開發應避免使用已棄用的函數(如 mysql_*、ereg_* 等),改用對應的新擴展或物件導向介面。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved