Categories: 網頁設計

PHP5與PHP4的區別

一、未100%向下兼容

在php5中儘管大部分PHP4的代碼應該不用修改就能運行,還是應該留意以下不向下兼容的改變:

有了一些新關鍵字。

strrpos() 和 strripos()如今使用整個字符串作爲 needle.

非法使用字符串偏移量會導致 E_ERROR 而不是 E_WARNING.一個非法使用的例子:$str = ‘abc’; unset($str[0]);.

array_merge() 被改成隻接受數組。如果傳遞入非數組變量,對每個此類參數都會發出一條 E_WARNING 信息。要小心因爲你的代碼有可能瘋狂發出 E_WARNING.

PATH_TRANSLATED 服務器變量在 Apache2 SAPI 中不再暗中設定,這和 PHP 4 中的情形相反,如果 Apache 沒産生此值則其被設爲和 SCRIPT_FILENAME 服務器變量一樣的值。此修改是爲了遵守CGI 規範。更多信息參考手册中 $_SERVER[‘PATH_TRANSLATED’] 的說明。此問題也影響到 PHP >= 4.3.2 的版本。

Tokenizer 擴展不再定義T_ML_COMMENT 常量。如果把 error_reporting 設爲 E_ALL,PHP 將産生一條消息。儘管 T_ML_COMMENT 從來都沒用到過,還是在 PHP 4 中定義了。在 PHP 4 和 PHP 5 中 // 和 都被解析爲 T_COMMENT 常量。但是 PHPDoc 風格的注釋 ,自 PHP 5 開始被 PHP 解析,被識別爲 T_DOC_COMMENT.

如果 variables_order 包括“S”,$_SERVER 應該帶有 argc 和 argv 被産生。如果用戶特別配製系統不創建 $_SERVER,那此變量當然就不存在了。改變的地方是不管 variables_order 怎麽設定,在 CLI 版本中 argc 和 argv 總是可用的。本來 CLI 版不是總會産生全局變量 $argc 和 $argv 的。

沒有屬性的對象不再被當成“empty”.

有些情况下類必須在使用前被定義。這僅在使用了一些 PHP 5 的新特性(例如 interfaces)的時候發生。其它情况下行爲都沒變。

get_class(),get_parent_class() 和 get_class_methods() 如今返回的類/方法名和定義時的名字一致(區分大小寫),對于依賴以前行爲(類/方法名總是返回小寫的)的老脚本可能産生問題。一個可能的解决方法是在脚本中搜索所有這些函數幷使用 strtolower()。

區分大小寫的改變也適用于魔術常量 __CLASS__,__METHOD__ 和 __FUNCTION__.其值都會嚴格按照定義時的名字返回(區分大小寫)。

ip2long() 在傳遞入一個非法 IP 作爲參數時返回 FALSE,不再是 -1.

如果有函數定義在包含文件中,則這些函數可以在主文件中使用而與是否在 return() 指令之前還是之後無關。如果文件被包含兩次,PHP 5 會發出致命錯誤,因爲函數已經被定義,而 PHP 4 不管這個。因此推薦使用 include_once() 而不要去檢查文件是否已被包含以及在包含文件中有條件返回。

include_once() 和 require_once() 在 Windows 下先將路徑規格化,因此包含 A.php 和 a.php 只會把文件包含一次。

實例: strrpos() 和 strripos() 如今用整個字符串作爲 needle

 

實例: 沒有屬性的對象不再被當成“empty”

 

實例: 有些情况下類必須在使用之前定義

 

二、CLI 和 CGI

PHP 5 中對 CLI 和 CGI 文件名作了些改變。PHP 5 中,CGI 版本被改名爲 php-cgi.exe(以前是 php.exe),現在主目錄中的是 CLI 版本(之前是 cli/php.exe)。

PHP 5 中引進了一種新模式:php-win.exe.這和 CLI 版本相同,只除了 php-win 不輸出任何內容,因此不會提供控制臺(屏幕上不會閃過“dos 窗口”)。此行爲類似 php-gtk.

PHP 5 中,CLI 版本總會産生全局變量 $argv 和 $argc 而不管 php.ini 是怎麽設的。即使將register_argc_argv設爲 off 也不影響 CLI.

參見命令行模式。

三、移植配置文件

由于 ISAPI 模塊的名字改了,從 php4xxx 改爲 php5xxx,因此需要對配置文件作些修改。CLI 和 CGI 文件名也改了。更多信息請查看相應章節。

移植 Apache 配置極其簡單。照下面的例子來檢查需要做的修改:

實例: 移植 Apache 配置文件到 PHP 5

# 將下麵這行: LoadModule php4_module /php/sapi/php4apache2.dll # 改成這一行: LoadModule php5_module /php/php5apache2.dll

如果 web 服務器是以 CGI 模式運行 PHP 的,應該注意 CGI 版本的名字從 php.exe 改爲了 php-cgi.exe.在 Apache 中,應該照這樣改:

實例: 移植 Apache 配置文件到 PHP 5,CGI 模式

# 將下麵這行: Action application/x-httpd-php “/php/php.exe” # 改成這一行: Action application/x-httpd-php “/php/php-cgi.exe”

其它的 web 服務器中,需要修改 CGI 或者 ISAPI 模塊的名字。

四、新函數

PHP 5 有了些新函數。下麵是列表:

Arrays:

array_combine() – 用一個數組作爲鍵名,另一個數組作爲值創建一個新數組

array_diff_uassoc() – 計算數組的差別,幷用用戶提供的回調函數作附加的索引檢查

array_udiff() – 用回調函數比較數據來計算數組的差別

array_udiff_assoc() – 計算數組的差別幷作附加的索引檢查。用回調函數來比較數據

array_udiff_uassoc() – 計算數組的差別幷作附加的索引檢查。數據的比較和索引檢查都用回調函數來完成

array_walk_recursive() – 對數組的每個成員遞歸使用用戶函數

array_uintersect_assoc() – 計算數組的交集幷作附加的索引檢查。用回調函數來比較數據

array_uintersect_uassoc() – 計算數組的交集幷作附加的索引檢查。數據和索引都用回調函數來比較

array_uintersect() – 計算數組的交集。用回調函數來比較數據

InterBase:

ibase_affected_rows() – 返回前一個查詢影響到的行的數目

ibase_backup() – 在服務管理器中發起一個後臺任務幷立即返回

ibase_commit_ret() – 提交一個事務但不關閉

ibase_db_info() – 請求有關數據庫的統計信息

ibase_drop_db() – 删除一個數據庫

ibase_errcode() – 返回一個錯誤代碼

ibase_free_event_handler() – 取消一個已注册的事件句柄

ibase_gen_id() – 遞增指定的發生器幷返回其新值

ibase_maintain_db() – 在數據庫服務器上執行一條維護命令

ibase_name_result() – 給結果集指定一個名字

ibase_num_params() – 返回一個準備好的查詢的參數數目

ibase_param_info() – 返回一個準備好的查詢的參數信息

ibase_restore() – 在服務管理器中發起一個還原任務幷立即返回

ibase_rollback_ret() – 回卷一筆事務幷保留事務上下文

ibase_server_info() – 請求有關數據庫服務器的統計信息

ibase_service_attach() – 連接到服務管理器

ibase_service_detach() – 從服務管理器斷開

ibase_set_event_handler() – 注册一個當事件發布時要調用的回調函數

ibase_wait_event() – 等待數據庫發布一條事件

iconv:

iconv_mime_decode() – 解碼 MIME 頭信息字段

iconv_mime_decode_headers() – 一次解碼多個 MIME 頭信息字段

iconv_mime_encode() – 壓縮 MIME 頭信息字段

iconv_strlen() – 返回字符串中的字符計數

iconv_strpos() – 在堆棧中找到第一個出現的子串位置

iconv_strrpos() – 在堆棧中找到最後一個出現的子串位置

iconv_substr() – 從字符串中取出一部分

Streams:

stream_copy_to_stream() – 把一個流的數據複製到另一個流

stream_get_line() – 根據給定的分隔符中流中讀取一行

stream_socket_accept() – 接受一個由 stream_socket_server() 建立的 socket 連接

stream_socket_client() – 打開一個 Internet 或 Unix 域的 socket 連接

stream_socket_get_name() – 獲取本地或遠程的 sockets 名字

stream_socket_recvfrom() – 從 socket 獲取數據(不管連接是否已經建立)

stream_socket_sendto() – 向 socket 發送一個消息(不管連接是否已經建立)

stream_socket_server() – 建立一個 Internet 或 Unix 域服務器的 socket

Date/Time:

idate() – 將本地進間格式化爲整數

date_sunset() – 計算所指定日期和地點的日落時間

date_sunrise() – T計算所指定日期和地點的日出時間

time_nanosleep() – 廷遲執行程若干秒和若干納秒

Strings:

str_split() – 把一個字符串分割爲數組

strpbrk() – 在一字符串中搜索給定的字符集合中的任意一個字符

substr_compare() – 以二進制的形式比較兩個字符串,從第一個字符串的 offset 開始,直到到達長度爲 length 時結束,可自定義是否大小寫敏感比較

Other:

convert_uudecode() – 解碼 uuencoded 的字符串

convert_uuencode() – 對字符串進行 uuencode

curl_copy_handle() – 複製一個 cURL 句柄及其所有參數

dba_key_split() – 把一個鍵分隔爲字符串數組

dbase_get_header_info() – 取得 dBase 數據庫的頭部信息

dbx_fetch_row() – 獲取結果集中被設置爲 DBX_RESULT_UNBUFFERED 的行

fbsql_set_password() – 修改指定用戶的密碼

file_put_contents() – 向一個文件內寫入字符串

ftp_alloc() – 爲準備上傳的文件分配空間

get_declared_interfaces() – 以數組的形式返回所有已定義的接品

get_headers() – 獲取服務器響應 HTTP 請求時的所有頭部信息

headers_list() – 返回所有已發送或準備發送響應頭部列表

http_build_query() – 生成一個已經過 URL 編碼的請求字符串

image_type_to_extension() – 根據 getimagesize(), exif_read_data(), exif_thumbnail(), exif_imagetype() 所返回的 image-type 取得文件名後綴

imagefilter() – 對圖像應用濾鏡

imap_getacl() – 獲取指定郵箱的 ACL

ldap_sasl_bind() – 使用 SASL 綁定到 LDAP 目錄

mb_list_encodings() – 以數組的形式返回所支持的全部字符集

pcntl_getpriority() – 獲得任意一個進程的優先級

pcntl_wait() – Waits on or returns the status of a forked child as defined by the waitpid() system call

pg_version() – 返回一個包含客戶端、協議和服務器版本的數組

php_check_syntax() – 檢查指定文件的語法

php_strip_whitespace() – 返回已經去除注釋和空白的源代碼

proc_nice() – 修改當前進程的優前級

pspell_config_data_dir() – 修改語言文件的位置

pspell_config_dict_dir() – 修改主要單詞列表的位置

setrawcookie() – 發送一個沒有經過 url 編碼的 cookie 值

scandir() – 列中指定目錄中的所有子目錄和文件

snmp_read_mib() – 在一個可用的 MIB 樹中讀取和分板一個 MIB 文件

sqlite_fetch_column_types() – 以數組的形式返回一張表中的列類型

注意: Tidy 擴展庫的 API 也作了重大調整

五、新指令

PHP 5 在 php.ini 中引進了一些新指令。列表如下:

mail.force_extra_parameters – 强制指定的參數附加值作爲額外的參數傳遞給 sendmail 庫。這些參數總是會替換掉 mail() 的第 5 個參數,即使在安全模式下

register_long_arrays – 允許/禁止 PHP 注册已過時的 $HTTP_*_VARS 變量

session.hash_function – 選擇一種散列函數(MD5 或 SHA-1)

session.hash_bits_per_character – 定義將二進制散列數據轉換爲可讀格式時每個字符中儲存幾個位(從 4 到 6)

zend.ze1_compatibility_mode – 啓用 Zend Engline 1 代(PHP 4)兼容模式

六、數據庫

關于數據庫(MySQL 和 SQLite)在 PHP 5 中有些改變。

PHP 5 中不再綁定 MySQL 客戶端連接庫,因爲授權和一些其它問題。

有個新擴展庫 MySQLi(改良版 MySQL),設計用來工作于 MySQL 4.1 及更高版本之下。

自 PHP 5 起,SQLite 擴展庫內置在 PHP 中。SQLite 是一個可嵌入 SQL 數據庫引擎,不是客戶端連接庫用來連接大型數據庫服務器(如 MySQL 或 PostgreSQL)的。SQLite 庫直接讀寫磁盤上的數據庫文件。

七、新對象模型

PHP 5 中有個新對象模型(Object Model)。PHP處理對象的方式完全重寫了,允許更佳性能和更多特性。之前版本的 PHP,對象處理方式和原始類型(例如整型和字符串)相同。此方法的缺點是當變量被賦值或作爲參數傳遞給方法時語義上整個對象都被拷貝。在新方法中,對象通過句柄引用,而不是值(可以將句柄當成是對象的標識符)。

很***a href=.itwis.com/html/php’>PHP程序員根本沒意識到舊的對象模型的這種拷貝怪癖,因此大多數PHP應用程序拿來就能運行,或者只做很小的修改。

新對象模型的文檔見“類與對象”.

========================

Difference with  PHP5 and PHP4

One , not 100% backwards compatible

Although most of the php5 in PHP4 code should be able to run without modification , or should be aware of the following changes are not backward compatible :

With some new keywords.

strrpos () and strripos () now use the entire string as a needle.

Illegal use of string offsets causes E_ERROR instead of E_WARNING example of an illegal use : $ str = ‘abc’; unset ($ str [0]);..

array_merge () was changed to accept only arrays. If passed into non- array variable , issue a E_WARNING message for each of these parameters are . Be careful because your code may be issued crazy E_WARNING.

PATH_TRANSLATED server variable in Apache2 SAPI is no longer secretly set , and PHP 4 in this case the contrary, if Apache does not produce this value then the SCRIPT_FILENAME server variable is set and the same value . This change was made to comply with the CGI specification. For more information reference manual $ _SERVER [‘PATH_TRANSLATED’] description . This problem also affects PHP> = version 4.3.2 .

Tokenizer extension is no longer defined T_ML_COMMENT constant. If error_reporting set to E_ALL, PHP will generate a message. Although T_ML_COMMENT never used before, or defined in PHP 4 it. In PHP 4 and PHP 5 / / and are resolved to T_COMMENT constant. But PHPDoc style comments , starting PHP 5 are parsed by PHP , are identified as T_DOC_COMMENT.

If variables_order including “S”, $ _SERVER should be generated with argc and argv . If the user does not create a system specially formulated $ _SERVER, then of course it would not exist. Variables_order change is that no matter how local settings, CLI version argc and argv are always available. CLI version will always produce the global variables $ argc and $ argv ‘s .

Object with no properties is no longer considered “empty”.

In some cases classes must be defined before use. It is only in use for some of the new features of PHP 5 ( such as interfaces) occurs when . Other circumstances have not changed behavior .

get_class (), get_parent_class () and get_class_methods () now returns the class name and the name of the method of definition / agreement ( case-sensitive ) , rely on the previous behavior ( class / method name always returns lowercase ) old scripts may cause problems . One possible solution is to search all of these functions in the script Bing use strtolower ().

Case-sensitive change also applies to the magic constant __CLASS__, __METHOD__ and __FUNCTION__. Whose values ​​are in strict accordance with the definition of the name returned ( case-sensitive ) .

ip2long () when passed into an illegal IP as an argument returns FALSE, instead of -1 .

If there are functions defined in the included file, regardless of whether these functions can be irrelevant before or after return () instruction in the main file . If the file is included twice , PHP 5 issues fatal error because functions were already declared , while PHP 4 does this . Therefore recommended to use include_once () instead of going to check whether the file was already included and conditionally return inside the included file.

include_once () and require_once () in the Windows path first standardized, and therefore contains A.php and a.php the file contains only once .

Examples : strrpos () and strripos () now use the entire string as a needle

 

Example: object with no properties is no longer considered “empty”

 

Example: In some cases classes must be defined before use

 

Two , CLI and CGI

PHP 5, the CLI and CGI files masterpieces for some change. In PHP 5 , CGI version was renamed to php-cgi.exe ( previously php.exe), now home directory is the CLI version (previously cli / php.exe).

PHP 5 introduced a new model in : php-win.exe This CLI version of the same , except that php-win does not output anything , it does not provide console ( not flashed “dos window ” on the screen ) . This behavior is similar to php-gtk.

In PHP 5 , CLI version will always produce the global variable $ argv and $ argc regardless of how the set of php.ini . Even if register_argc_argv set off does not affect the CLI.

See the command line mode.

Third, the transplant profile

Since the ISAPI module to change the name from php4xxx to php5xxx, and therefore need to make some changes to the configuration file . CLI and CGI filenames have changed. For more information , please see the appropriate section.

Porting Apache configuration is extremely simple. See the example below to check needs to be done to modify :

Example: Transplant Apache configuration file to PHP 5

# The following line : LoadModule php4_module / php/sapi/php4apache2.dll # change this line : LoadModule php5_module / php/php5apache2.dll

If the web server is running PHP in CGI mode , you should pay attention to the CGI version of the name from php.exe to php-cgi.exe in Apache , you should change as such :

Example: Transplant Apache configuration file to PHP 5, CGI mode

# The following line : Action application / x-httpd-php “/ php / php.exe” # change this line : Action application / x-httpd-php “/ php / php-cgi.exe”

Other web servers , the need to modify the CGI or ISAPI module name .

Fourth, the new function

PHP 5 has some new functions. Here is a list :

Arrays:

array_combine () – with an array as keys , another array as the value of creating a new array

() array_diff_uassoc – the difference between an array of computing , Bing additional index check with the user-supplied callback function

() array_udiff – the difference between using a callback function to calculate the array comparative data

array_udiff_assoc () – Computes the difference of arrays with additional index check Bing . Using a callback function to compare the data

array_udiff_uassoc () – Computes the difference of arrays with additional index check Bing . Comparison of the data and index checks are done using a callback function

array_walk_recursive () – for each member of the array using a user function recursively

array_uintersect_assoc () – the intersection of arrays with additional index of Bing checks . Using a callback function to compare the data

array_uintersect_uassoc () – the intersection of arrays with additional index of Bing checks . Data and indexes are compared by using a callback function

array_uintersect () – to calculate the intersection of arrays . Using a callback function to compare the data

InterBase:

The number before returning a query affected rows – ibase_affected_rows ()

ibase_backup () – initiate a background task in the service manager immediately returned Bing

ibase_commit_ret () – commits a transaction without closing

ibase_db_info () – Request statistics about a database

ibase_drop_db () – delete a database

ibase_errcode () – returns an error code

ibase_free_event_handler () – Cancels a registered event handler

ibase_gen_id () – increments specified generator Bing returns its new value

ibase_maintain_db () – on the database server to perform a maintenance command

ibase_name_result () – assign a name to a result set

ibase_num_params () – Returns the number of parameters in a prepared query

ibase_param_info () – Returns a prepared query parameter information

ibase_restore () – initiate a restore task in the service manager immediately returned Bing

ibase_rollback_ret ​​() – Rewind the context of a transaction Bing reserved Affairs

ibase_server_info () – Request statistics about a database server

ibase_service_attach () – connect to the service manager

ibase_service_detach () – Disconnect from the service manager

ibase_set_event_handler () – Register a callback function to be called when the event is published

ibase_wait_event () – Wait for the database publishing an event

iconv:

iconv_mime_decode () – decode MIME header field

iconv_mime_decode_headers () – once decode multiple MIME header field

iconv_mime_encode () – Compression MIME header field

iconv_strlen () – Returns the string character count

iconv_strpos () – find the first occurrence of the substring position in the stack

iconv_strrpos () – find the position of the last occurrence of a substring in the stack

iconv_substr () – Remove a portion from a string

Streams:

stream_copy_to_stream () – Copy data from one stream to another stream

stream_get_line () – read a line of a given flow in the separator

stream_socket_accept () – accepts consisting stream_socket_server () establish a socket connection

stream_socket_client () – Open an Internet connection or socket Unix domain

stream_socket_get_name () – Get the name of a local or remote sockets

stream_socket_recvfrom () – retrieve data from socket ( regardless of whether the connection has been established )

stream_socket_sendto () – Send a message to the socket ( regardless of whether the connection has been established )

stream_socket_server () – to establish an Internet or Unix domain server socket

Date / Time:

idate () – Format a local into an integer between

date_sunset () – to calculate the date and place specified in the Sunset

date_sunrise () – T is calculated by a specified date and place of the sunrise time

time_nanosleep () – Several Ting Chi Cheng seconds and perform a number of nanoseconds

Strings:

str_split () – put a string into an array

strpbrk () – in a search string in a given character set of any one character

substr_compare () – in binary form to compare two strings , the first string from offset start until the end when it reaches a length of length , you can customize whether the comparison is case sensitive

Other:

convert_uudecode () – decoding uuencoded string

convert_uuencode () – uuencode strings

curl_copy_handle () – Copy a cURL handle all its parameters

dba_key_split () – put a delimited string array

dbase_get_header_info () – get the header information dBase database

dbx_fetch_row () – Get the result set rows are set to DBX_RESULT_UNBUFFERED

fbsql_set_password () – modify the specified user’s password

file_put_contents () – write a string to a file

ftp_alloc () – allocate space for files uploaded to prepare

get_declared_interfaces () – Returns an array of all defined access products

get_headers () – get the server to respond to all HTTP request headers when

headers_list () – Returns a list of all the response has been sent or is ready to send the head

http_build_query () – has been generating a URL -encoded query string

image_type_to_extension () – According to getimagesize (), exif_read_data (), exif_thumbnail (), exif_imagetype () which returns the image-type get filename suffix

imagefilter () – apply a filter to the image

imap_getacl () – Gets the specified mailbox ACL

ldap_sasl_bind () – Bind to LDAP directory using SASL

mb_list_encodings () – Returns an array of all character sets are supported

pcntl_getpriority () – get any priority of a process

pcntl_wait () – Waits on or returns the status of a forked child as defined by the waitpid () system call

pg_version () – Returns an array of client and server versions of the agreement included

syntax check of the specified file – php_check_syntax ()

php_strip_whitespace () – Returns comment has been removed and a blank source code

proc_nice () – modify the current process of gifted pre-

Location modify the language file – () pspell_config_data_dir

modify the position of the main word list – () pspell_config_dict_dir

setrawcookie () – Send a cookie without url encoded value

scandir () – column specified directories and files in all subdirectories

snmp_read_mib () – available in a MIB tree, and the scoreboard read a MIB file

sqlite_fetch_column_types () – Returns an array of the type of a table column

Note : Tidy extension repository API also made significant adjustments

Fifth, the new directive

PHP 5 introduced some new directive in php.ini . List is as follows :

mail.force_extra_parameters – Forced specified parameters passed to sendmail library added as an additional parameter. These parameters will always replace the mail () in the first five parameters , even in safe mode

register_long_arrays – enable / disable PHP registered obsolete variable $ HTTP_ * _VARS

session.hash_function – Select a hash function (MD5 or SHA-1)

session.hash_bits_per_character – define each character will be stored in several binary bit hash data into a readable format ( from 4-6 )

zend.ze1_compatibility_mode – Enable Zend Engline 1 generations (PHP 4) compatibility mode

Sixth, the database

About Database (MySQL and SQLite) some changes in PHP 5 .

PHP 5 no longer bundles MySQL client library , as authorized , and some other problems.

There under in MySQL 4.1 and later new extensions MySQLi ( improved version of MySQL), is designed to work one .

Since PHP 5 , SQLite extensions built- in PHP. SQLite is an embeddable SQL database engine , not a client library used to connect to a large database server ( such as MySQL or PostgreSQL) ‘s . SQLite library to read and write directly to the database files on disk.

Seven new object model

There is a new PHP 5 object model (Object Model). PHP way of handling objects completely rewritten , allowing for better performance and more features . Prior versions of PHP, objects were handled like primitive types ( such as integers and strings ) the same . The disadvantage of this method is that when a variable is assigned or as the whole object parameter passed to the method are copy semantics. In the new method, the object referenced by the handle , not the value ( as is the identifier can handle objects ) .

It *** a href = .itwis.com / html / php ‘> PHP programmer did not realize this copy quirks old object model, so most will be able to run PHP applications used , or only very minor modifications .

See the document ” object class ” new object model.

yan

Share
Published by
yan
Tags: PHP