Table of Contents
2. Understand the differences between various comparison operators
3. Simplify the else part of the judgment sentence
Home Backend Development PHP Tutorial Top Ten Tips to Improve Your PHP Application Strength_PHP Tutorial

Top Ten Tips to Improve Your PHP Application Strength_PHP Tutorial

Jul 21, 2016 pm 02:56 PM
g php top ten exist college strength Build a website Skill promote document of website use

Agni Website Building Academy Documents On the Smashing Magazine website, Glen Stansberry proposed ten advanced PHP tips that can instantly improve your PHP programming skills, including a "cheat sheet" for SQL injection attacks ”, simplify the else part of the judgment sentence, use regular expressions, ternary operators, Memcached database caching system, etc. only when necessary. The following is the detailed content.

PHP started as a humble programming language in 1995. It has developed rapidly over the years and is now one of the most popular web development languages. Many popular websites are developed using PHP, and the vast majority of programs and websites Projects are written in this popular language.

Because PHP is so popular, it is almost impossible for any website developer not to know PHP. This tutorial is for those who have just completed the initial stage of learning PHP and roll up their sleeves and are ready to work hard. Listed below The ten excellent tips that PHP developers must learn and use every time they program. These tips can speed up your proficiency in PHP, making the code run faster, more concisely, and with better performance.

1. Using SQL Injection Attack Cheat Sheet

SQL injection attack is a very nasty thing, it is a security vulnerability that allows hackers to sneak into your database by exploiting holes in the code. Although this article has nothing to do with MySQL, many PHP programs use the MySQL database, and if you want to write secure code, it is useful to know what situations to avoid.

Furruh Mavituna wrote a very interesting SQL injection attack cheat sheet, including a chapter about the security vulnerabilities of PHP and MySQL. If you can avoid the behaviors described in it, your program will not be vulnerable to attacks. .

2. Understand the differences between various comparison operators

Comparison operators are a very important part of PHP. Unfortunately, many programmers are not familiar with the differences between them. In fact, according to an article published on the IO Reader website, many PHP developers cannot distinguish between PHP operators. The differences between comparison operators, quote a few sentences from the article:

This is extremely important, but many PHP developers simply don’t know the difference between == and ===. Essentially, == determines whether two values ​​are equal, and PHP will try to convert the data before comparing it. The type starts with, for example: 1== '1' (true); === determines the value and data type at the same time, for example: 1==='1' (false). Developers must understand the impact of these operators on commonly used functions as soon as possible. The practicality of formulas such as strpos(), because PHP treats zero as "false", without === we have no way of knowing whether a searched string is at the beginning of a longer string, or does not exist at all. In many cases In other applications, === is useful when returning a value of zero does not necessarily equal false.

You can go to the PHP.net website to see a list of various comparison operators.

3. Simplify the else part of the judgment sentence

I must remind everyone that the third and fourth points may reduce the readability of the code. These two points emphasize speed and performance. If you don’t want to sacrifice the readability of the code, you may want to skip them. .

Anything that can make the code simpler and smaller is usually a good practice. One of them is to extract the content of the else statement. Christian Montoya has a good example of using a shorter else statement to convert characters.

Common else narrative sentences:

{$x = 10;}
The following is the quoted content:
以下为引用的内容:
if( this condition )
{
$x = 5;
}
else
{
$x = 10;
}
if( this condition )

{

$x = 5;
<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

If the default value of $x is 10, we start from 10 without specifying its value in the else part.

以下为引用的内容:
if ($gollum == 'halfling') {
$height --;
}
<🎜> <🎜> <🎜> <🎜>Although there doesn’t seem to be much difference, if there are many else statements in your program, the cumulative effect will be obvious. <🎜> <🎜>4. Drop those parentheses <🎜> <🎜><🎜>Just like simplifying the else statement, if there is only one expression after the control structure, discarding the parentheses before and after the expression can save some characters. Evolt.org has a neat example of how to reduce the use of parentheses. <🎜> <🎜>
The following is the quoted content: <🎜><🎜>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
  • 下一页

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/364244.htmlTechArticle烈火建站学院文档 在 Smashing Magazine 的网站,Glen Stansberry 提出十个进阶 PHP 技巧,可以即时提昇你的 PHP 编程实力,其中包括 SQL 注入攻击的...
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

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.

How to trade in TREZOR and what to note How to trade in TREZOR and what to note Feb 21, 2025 pm 10:30 PM

Transactions on TREZOR Connect the TREZOR device and install the TREZORBridge application. Open the TREZOR suite and select the transaction type (send, receive, or redeemed). Enter transaction details (address, amount, fee, etc.). Check the details and press Confirm on the TREZOR device to authorize the transaction.

The latest top ten digital currency trading app rankings in 2025 The latest top ten digital currency trading app rankings in 2025 Feb 27, 2025 pm 06:24 PM

The top ten digital currency trading apps in 2025 are: Binance, OKX, Gate.io, Bitget, Huobi, KuCoin, BitMart, Bybit, Bitfinex, and Poloniex. These transactions stand out in the market for their trading volume, asset selection, leveraged trading, social trading, compliance, ease of use, low transaction fees and fund security.

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

See all articles