国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 php手冊 PHP 高手之路(三)

PHP 高手之路(三)

Jun 21, 2016 am 09:15 AM
quot replace

使用str-replace而不是ereg-replace
習慣使用Perl進行編程的程序員更加愿意使用ereg_replace完成字符串替換工作,因為在PHP中ereg_replace的用法和Perl中模式匹配的用法相近。但是,下面的這段代碼證明,使用str_replace 代替 ereg_replace將可以大大提高代碼的運行速度。

測試str_replace和ereg_replace的運行速度

//這段代碼測試str_replace的運行速度


emphasis; ?>

for ($i=0; $i str_replace(i>, b>, $string).
;
}
?>

//這段代碼測試ereg_replace的運行速度




for ($i=0; $i ereg_replace(, , $string).
;
}
?>






//打印結(jié)果


結(jié)論

使用str_replace的時間 -


使用ereg_pattern的時間 -
運行上面的代碼,得到的結(jié)果是:
使用str_replace的時間 - 0.089757
使用ereg_pattern的時間 - 0.248881
從運行的結(jié)果我們可以看出使用str_replace替代ereg_replace作為字符串替換函數(shù),極大地提高了代碼的運行速度。
3.注意字符串的引用
PHP和其它很多編程語言一樣,可以使用雙引號("")來引用字符串,也可以使用單引號()。但是在PHP中,如果使用雙引號來引用字符串,那么PHP解析器將首先分析字符串中有沒有對變量的引用,有變量的話,將對變量進行替換。如果是單引號,則沒有如此復雜??直接將單引號包含起來的所有字符串直接顯示出來。顯然,在PHP編程中,如果使用單引號引用字符串變量要比使用雙引號快速一些。
4.在數(shù)據(jù)庫中避免使用聯(lián)合操作
比起其它的Web編程語言來說,PHP的數(shù)據(jù)庫功能十分強大。但是在PHP中數(shù)據(jù)庫的運行仍然是一件十分費時費力的事情,所以,作為一個Web程序員,要盡量減少數(shù)據(jù)庫的查詢操作,同時應(yīng)該為數(shù)據(jù)庫建立適當?shù)乃饕?。另一件值得注意的事情是在用PHP操作數(shù)據(jù)庫時,盡可能不使用多個數(shù)據(jù)表的聯(lián)合操作,盡管聯(lián)合操作可以增強數(shù)據(jù)庫的查詢功能,但是卻大大增加了服務(wù)器的負擔。
為了說明這個問題,我們可以看看下面的這個簡單的例子。
我們在數(shù)據(jù)庫中創(chuàng)建了兩個數(shù)據(jù)表foo和big_foo。在數(shù)據(jù)表foo中,只有一個字段,包含了從1-1000之間的所有自然數(shù)。數(shù)據(jù)表big_foo同樣只有一個字段,但包含了從1-1,000,000之間的全部自然數(shù)。所以,從大小上說,big_foo等于foo與它自身進行了聯(lián)合操作。
$db->query("select * from foo");
0.032273 secs
$db->next_record();
0.00048999999999999 secs
$db->query("insert into foo values (NULL)");
0.019506 secs
$db->query("select * from foo as a, foo as b");
17.280596 secs
$db->query("select * from foo as a, foo as b where a.id > b.id");
14.645251 secs
$db->query("select * from foo as a, foo as b where a.id = b.id");
0.041269 secs
$db->query("select * from big_foo");
25.393672 secs
從上面操作結(jié)果我們可以發(fā)現(xiàn),對于兩個有1000條記錄的數(shù)據(jù)表進行聯(lián)合,其速度并不比對一個1000000條紀錄的大型數(shù)據(jù)表單獨進行操作快多少。
5.注意include與require的區(qū)別
在PHP變成中,include()與require()的功能相同,但在用法上卻有一些不同,include()是有條件包含函數(shù),而require()則是無條件包含函數(shù)。例如在下面的一個例子中,如果變量$somgthing為真,則將包含文件somefile:
if($something){
include("somefile");
}
但不管$something取何值,下面的代碼將把文件somefile包含進文件里:
if($something){
require("somefile");
}
下面的這個有趣的例子充分說明了這兩個函數(shù)之間的不同。
$i = 1;
while ($i require("somefile.$i");
$i++;
}
在這段代碼中,每一次循環(huán)的時候,程序都將把同一個文件包含進去。很顯然這不是程序員的初衷,從代碼中我們可以看出這段代碼希望在每次循環(huán)時,將不同的文件包含進來。如果要完成這個功能,必須求助函數(shù)include():
$i = 1;
while ($i include("somefile.$i");
$i++;
}
6.注意echo和print的區(qū)別
PHP中echo和print的功能也基本相同,但是兩者之間也有細微差別。在PHP代碼中可以把print作為一個普通函數(shù)來使用,例如執(zhí)行下面的代碼后變量$res的值將為1。
$ret = print "Hello World";
這意味著print可用在一些復雜的表達式中,而echo則不行。同樣,在代碼中echo語句的運行速度要略微快于print語句,因為echo語句不要求返回任何數(shù)值



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1502
276
How to use the REPLACE function to replace a specified part of a string in MySQL How to use the REPLACE function to replace a specified part of a string in MySQL Jul 25, 2023 pm 01:18 PM

MySQL is a commonly used relational database management system that provides a variety of functions to process and operate data. Among them, the REPLACE function is used to replace the specified part of the string. In this article, we will introduce how to use the REPLACE function for string replacement in MySQL and demonstrate its usage through code examples. First, let’s take a look at the syntax of the REPLACE function: REPLACE(str,search_str,replace_str).

What are the string search and replace techniques in Python? What are the string search and replace techniques in Python? Oct 20, 2023 am 11:42 AM

What are the string search and replace techniques in Python? (Specific code example) In Python, strings are a common data type, and we often encounter string search and replace operations in daily programming. This article will introduce some common string search and replacement techniques, accompanied by specific code examples. To find a specific substring in a string, you can use the find() method or index() method of the string. The find() method returns the index of the first occurrence of the substring in the string.

php提交表單通過后,彈出的對話框怎樣在當前頁彈出,該如何解決 php提交表單通過后,彈出的對話框怎樣在當前頁彈出,該如何解決 Jun 13, 2016 am 10:23 AM

php提交表單通過后,彈出的對話框怎樣在當前頁彈出php提交表單通過后,彈出的對話框怎樣在當前頁彈出而不是在空白頁彈出?想實現(xiàn)這樣的效果:而不是空白頁彈出:------解決方案--------------------如果你的驗證用PHP在后端,那么就用Ajax;僅供參考:HTML code

Use the replace() method of the StringBuilder class in Java to replace part of the content in a string Use the replace() method of the StringBuilder class in Java to replace part of the content in a string Jul 24, 2023 pm 10:28 PM

In Java, use the replace() method of the StringBuilder class to replace part of the content in a string. In Java programming, strings are a very important data type, and strings often need to be processed and manipulated. And sometimes we need to replace part of the string to meet our needs. In Java, you can use the replace() method of the StringBuilder class to implement string replacement operations. StringBuilder is a

圖片消失怎么解決 圖片消失怎么解決 Apr 07, 2024 pm 03:02 PM

圖片消失如何解決先是圖片文件上傳$file=$_FILES['userfile']; ?if(is_uploaded_file($file['tmp_name'])){$query=mysql_query("INSERT INTO gdb_banner(image_src ) VALUES ('images/{$file['name'

不用數(shù)據(jù)庫來實現(xiàn)用戶的簡單的下載,代碼如下,但是卻不能下載,請高手找下原因,文件路勁什么的沒有關(guān)問題 不用數(shù)據(jù)庫來實現(xiàn)用戶的簡單的下載,代碼如下,但是卻不能下載,請高手找下原因,文件路勁什么的沒有關(guān)問題 Jun 13, 2016 am 10:15 AM

不用數(shù)據(jù)庫來實現(xiàn)用戶的簡單的下載,代碼如下,但是卻不能下載,請高手找下原因,文件路勁什么的沒問題。

圖片消失怎么解決 圖片消失怎么解決 Jun 13, 2016 am 10:09 AM

圖片消失如何解決先是圖片文件上傳$file=$_FILES['userfile']; ?if(is_uploaded_file($file['tmp_name'])){$query=mysql_query("INSERT INTO gdb_banner(image_src ) VALUES ('images/{$file['name'

為什么小弟我在php上寫的這個代碼,在瀏覽器上什么都不顯示 為什么小弟我在php上寫的這個代碼,在瀏覽器上什么都不顯示 Jun 13, 2016 am 10:24 AM

為什么我在php上寫的這個代碼,在瀏覽器上什么都不顯示啊

See all articles