亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

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

PHP 高手之路(三)

Jun 21, 2016 am 09:15 AM
quot replace

使用str-replace而不是ereg-replace
習(xí)慣使用Perl進(jìn)行編程的程序員更加愿意使用ereg_replace完成字符串替換工作,因?yàn)樵赑HP中ereg_replace的用法和Perl中模式匹配的用法相近。但是,下面的這段代碼證明,使用str_replace 代替 ereg_replace將可以大大提高代碼的運(yùn)行速度。

測試str_replace和ereg_replace的運(yùn)行速度

//這段代碼測試str_replace的運(yùn)行速度


emphasis; ?>

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

//這段代碼測試ereg_replace的運(yùn)行速度




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






//打印結(jié)果


結(jié)論

使用str_replace的時(shí)間 -


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

php提交表單通過后,彈出的對話框怎樣在當(dāng)前頁彈出php提交表單通過后,彈出的對話框怎樣在當(dāng)前頁彈出而不是在空白頁彈出?想實(shí)現(xiàn)這樣的效果:而不是空白頁彈出:------解決方案--------------------如果你的驗(yà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ù)庫來實(shí)現(xiàn)用戶的簡單的下載,代碼如下,但是卻不能下載,請高手找下原因,文件路勁什么的沒有關(guān)問題 不用數(shù)據(jù)庫來實(shí)現(xiàn)用戶的簡單的下載,代碼如下,但是卻不能下載,請高手找下原因,文件路勁什么的沒有關(guān)問題 Jun 13, 2016 am 10:15 AM

不用數(shù)據(jù)庫來實(shí)現(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上寫的這個(gè)代碼,在瀏覽器上什么都不顯示 為什么小弟我在php上寫的這個(gè)代碼,在瀏覽器上什么都不顯示 Jun 13, 2016 am 10:24 AM

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

See all articles