Table of Contents
2. 认识各种比较运算符的分别
3. 简化判断句中的 else 部分
4. 放弃那些括号
Home php教程 php手册 十大技巧提升你的 PHP 运用实力

十大技巧提升你的 PHP 运用实力

Jun 13, 2016 am 11:38 AM
g php top ten exist college strength Build a website Skill promote document of website use

烈火建站学院文档 在 Smashing Magazine 的网站,Glen Stansberry 提出十个进阶 PHP 技巧,可以即时提昇你的 PHP 编程实力,其中包括 SQL 注入攻击的「作弊表」、简化判断句中的 else 部分、在不得已的情况下才使用正规表达式、三元运算子、Memcached 数据库快取系统等等,以下是详细的内容。

PHP 在 1995 年从一个不起眼的编程语言开始,多年来迅速发展,现在已经是其中一种最流行的网络开发语言,许多热门网站均採用 PHP 来开发,绝大多数的程式和网站项目都是由这种流行语言写成。

由於 PHP 如此受欢迎,任何网站开发人员几乎都不能不认识 PHP,这份教程是為那些刚刚完成了 PHP 的学习初阶,捲起衣袖準备大干一番的人,下面列出的十个优秀技巧,是 PHP 开发人员必须学习,并在每次编程时使用它们,这些提示可以加快你们对 PHP 的熟练,使程式码跑得更快捷,更简洁,性能上更优化。

1. 使用 SQL 注入攻击作弊表

SQL 注入攻击是一种非常令人厌恶的东西,它是一种安全漏洞,允许黑客利用程式码中的漏洞潜入你的数据库。虽然本文与 MySQL 无关,不过许多 PHP 程式使用 MySQL 数据库,若果你要写安全的程式码,懂得什麼情况需要避开是很有用的。

Furruh Mavituna 写了一篇很有趣的 SQL 注入攻击作弊表,其中一章节有关 PHP 和 MySQL 的安全漏洞,若果你能避开其中所述的行為,你的程式将不容易受到攻击。

2. 认识各种比较运算符的分别

比较运算符是 PHP 很重要的部份,可惜很多程序员并不熟悉它们之间的分别,事实上,据一篇在 IO Reader 网站上发表的文章,许多 PHP 开发人员不能分辨 PHP 各种比较运算符之间的差异,引用该文的几句话:

这是极為重要的,但是很多 PHP 开发人员根本不知道 == 和 === 有什麼分别,从本质上讲,== 判断两个值是否相等,PHP 在进行比较前会尝试转换数据类型以,例如:1== '1' (true);=== 则同时判断值和数据类型,例如:1==='1'(false),开发人员必须儘快认识这些运算符对常用函式如 strpos() 的实用性,由於 PHP 把零视為「false」,没有 === 的话我们无从知道一个被搜寻字符串是在一个较长字符串的开始位置,还是根本不存在,在很多其他应用裡,当传回值為零未必等於 false 的时候,=== 便十分有用。

你可以到 PHP.net 网站看到各种比较运算符的清单。

3. 简化判断句中的 else 部分

必须提示各位,第三和第四点可能会使程式码的可读性降低,这两点强调速度和效能,若果你不想牺牲程式码的可读性,你可能要跳过它们。

凡是能使程式码更简单和更小的做法,通常都是好的做法,其中之一就是把 else 叙述句的内容抽出来,Christian Montoya 便有一个很好的例子,利用较短的 else 叙述句来转换字符。

常见的 else 叙述句:

以下为引用的内容:
if( this condition )
{
$x = 5;
}
else
{
$x = 10;
}

如果 $x 的预设值是 10,我们便从 10 开始,无须在 else 部分指定它的值。

<table style="BORDER-RIGHT: #cccccc 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: #cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellspacing="0" cellpadding="6" width="95%" align="center" border="0"><tbody><tr><td style="WORD-WRAP: break-word" bgcolor="#fdfddf"><font color="#ff0000">以下为引用的内容:</font><br /><pre class="brush:php;toolbar:false">$x = 10;<br />if( this condition )<br />{<br />$x = 5;<br />} 
Copy after login
 <span><span class="google-src-text" style="DIRECTION: ltr; TEXT-ALIGN: left">if( this condition )</span>如果(这种情况)</span>   <span><span class="google-src-text" style="DIRECTION: ltr; TEXT-ALIGN: left">{</span> (</span>   <span><span class="google-src-text" style="DIRECTION: ltr; TEXT-ALIGN: left">$x = 5;</span> $ x = 5 ;</span>   <span><span class="google-src-text" style="DIRECTION: ltr; TEXT-ALIGN: left">}</span> )</span>   <span><span class="google-src-text" style="DIRECTION: ltr; TEXT-ALIGN: left">else</span>其他的</span>   <span><span class="google-src-text" style="DIRECTION: ltr; TEXT-ALIGN: left">{</span> (</span>   <span><span class="google-src-text" style="DIRECTION: ltr; TEXT-ALIGN: left">$x = 10;</span> $ x = 10 ;</span>   <span><span class="google-src-text" style="DIRECTION: ltr; TEXT-ALIGN: left">}</span> )</span>
Copy after login

虽然看起来没有很大分别,不过若果你的程式中有很多 else 叙述句,累加的效果会很明显。

4. 放弃那些括号


就像简化 else 叙述句那样,如果在控制结构后只有一句表达式,放弃表达式前后的括号可以节省一些字符。Evolt.org 便有一个灵巧的例子示范怎样减少使用括号。

以下为引用的内容:
if ($gollum == 'halfling') {
$height --;
}

这是相同的:<br /><table style="BORDER-RIGHT: #cccccc 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: #cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellspacing="0" cellpadding="6" width="95%" align="center" border="0"><tbody><tr><td style="WORD-WRAP: break-word" bgcolor="#fdfddf"><font color="#ff0000">以下为引用的内容:</font><br />if ($gollum == 'halfling') $height --;</td></tr></tbody></table>
Copy after login
这个方法可以在程式中多次使用:<br /><br /><table style="BORDER-RIGHT: #cccccc 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: #cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellspacing="0" cellpadding="6" width="95%" align="center" border="0"><tbody><tr><td style="WORD-WRAP: break-word" bgcolor="#fdfddf"><font color="#ff0000">以下为引用的内容:</font><br />if ($gollum == 'halfling') $height --;<br />else $height ++; <br /><br />if ($frodo != 'dead')<br />echo 'Gosh darnit, roll again Sauron';<br /><br />foreach ($kill as $count)<br />echo 'Legolas strikes again, that makes' . $count . 'for me!';</td></tr></tbody></table><br /><br /><strong><font size="3">5. 取 str_replace(),捨 ereg_replace() 及 preg_replace()</font></strong>
Copy after login

以效率来说,str_replace() 比正规表达式更适合用来取代字符串,据一些研究,str_replace() 的效率比 ereg_replace() 和 preg_replace() 高 61%。

Copy after login
  • 共2页:
  • 上一页
  • 1
  • 2
  • 下一页
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

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.

Undress AI Tool

Undress AI Tool

Undress images for free

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)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

Explain the match expression (PHP 8 ) and how it differs from switch. Explain the match expression (PHP 8 ) and how it differs from switch. Apr 06, 2025 am 12:03 AM

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

gate official website entrance gate exchange official website gate official website entrance gate exchange official website Feb 19, 2025 pm 03:03 PM

The official website of Gate.io can be accessed by clicking on the link or entering the URL in the browser. It is recommended to add the URL to a bookmark or favorite for easy access. If you encounter inaccessible issues, try clearing the browser's cache and cookies. Be careful to prevent phishing, the official website of Gate.io will not take the initiative to ask for personal information. In addition, Gate.io provides mobile applications that can be found through the app provider

BitMEX exchange withdrawal rules and advantages and disadvantages BitMEX exchange withdrawal rules and advantages and disadvantages Feb 21, 2025 pm 10:48 PM

BitMEX exchange currency withdrawal requirements: Two-step verification and identity verification must be completed. The minimum amount of withdrawal varies by currency. The withdrawal process includes logging into the account, entering the withdrawal address, entering the amount and confirming transactions. The advantages of BitMEX withdrawal include fast processing, low handling fees, multiple currency support and strict security measures. However, it also faces shortcomings such as insufficient supervision, risk of hacker attacks, restrictions on withdrawals and account freezes.

See all articles