Home Backend Development PHP Tutorial 一家之言的经验之谈php+mysql扎实个人基本功_php技巧

一家之言的经验之谈php+mysql扎实个人基本功_php技巧

May 17, 2016 am 09:37 AM

一. 10句话
1.不要依赖register_global=ON的环境,从你刚懂得配置php运行环境甚至尚不明白register_global的ON/OFF会对自己有什么影响的那天起,就应该勇敢地把它设为OFF.
2.写程序前看看怎么用error_reporting.
3.不懂就问本身没错,但你需要在那之前查查手册。
4.当然,你需要懂得使用手册。手册上找不到答案的时候,应该考虑下网络上的搜索引擎。
5.刚学会php+mysql之后,不要叫嚷着要写论坛,要写XXX。要明白,刚学会写汉字并不表示你有能力写诗。
6.在学web编程的时候,你应该先去认识html这个朋友。
7.有点能力后,试着回答新手的问题,不要看到自己懂的而别人不懂就沾沾自喜,扔下一名“简单,那是基本的东西”就走更要不得。
8.思考是一个好习惯,不动手去写就等于空想,什么也没有。
9.写好一段程序,如果觉得很满意,一周后再看一遍,也许你会认为它应该有所改变
10.有空多看看别人的程序,找出他人的不足或优点,自己掂量。
二. 各取所需

1.善于使用“引用”,它能直接影响到程序的效率。

2.善于用三元运算子,可以让程式较精简有效率。
比如:

PHP代码:--------------------------------------------------------------------------------
if ($data[$i]['nickname'])
{
  $nickname = $data[$i]['nickname'];
}
else
{
  $nickname = $data[$i]['ip'];
}

--------------------------------------------------------------------------------

可以写成:

PHP代码:--------------------------------------------------------------------------------
$nickname = $data[$i]['nickname'] ? $data[$i]['nickname'] : $data[$i]['ip'];

--------------------------------------------------------------------------------


3.善于组织if...else...回圈
比如:

PHP代码:--------------------------------------------------------------------------------
$ext_name = strtolower(str_replace(".", "", strrchr($upfilename, ".")));
if (!empty($type))
{
  if (!strpos($type, $ext_name))
  {
    echo "Please upload the file of $type form.";
    exit();
  }
}

--------------------------------------------------------------------------------

上面的代码你应该写成这样:

PHP代码:--------------------------------------------------------------------------------
$ext_name = strtolower(str_replace(".", "", strrchr($upfilename, ".")));
if (!($type==='') && strpos($type, $ext_name)===false)
{
  echo "Please upload the file of $type form.";
  exit();
}

--------------------------------------------------------------------------------


4.尽量让你的代码清淅些
如果写成这样,是比较让人头痛的:

PHP代码:--------------------------------------------------------------------------------
$foo=$_post["foo"];
  $username=$_post["user"]; 
$group=$_POST["group"];
if ($group=="wheel"){
$username=$username."wheel";
}

--------------------------------------------------------------------------------

同样的代码,这样就比较让人看得舒服了:

PHP代码:--------------------------------------------------------------------------------
$foo     = $_post["foo"];
$username = $_post["username"];
$group   = $_POST["group"];
if ($group=="wheel")
{
  $username = $username."wheel";
}

--------------------------------------------------------------------------------

当然,有一定基础后,你应该要写成这样:

PHP代码:--------------------------------------------------------------------------------
$foo     = &$_POST['foo'];
$username = $_POST["group"]!='wheel' ? $_POST["username"] : $_POST["username"].'wheel';

--------------------------------------------------------------------------------

5.编写规范的mysql 语句。
字段和表名用"`"引起来,避免保留字的影响。
如果看到下面这样的一个sql query,会让人比较头痛:

PHP代码:--------------------------------------------------------------------------------
$query="select `flash_comment`.`content` , `flash_comment`.`nickname` , `flash_comment`.`date` , `flash_comment`.`ip` , `product`.`p_name` , `sgflash`.`fid` from `flash_comment` left join `product` on ( `flash_comment`.`p_no` = `product`.`p_no` ) left join `sgflash` on ( `product`.`p_name` = `sgflash`.`f_name` ) where `flash_comment`.`p_no` != '' order by `flash_comment`.`date`";

--------------------------------------------------------------------------------

同样的一个query,写成这样就令人看得明白得多了:

PHP代码:--------------------------------------------------------------------------------
$query = "Select `flash_comment`.`content` , `flash_comment`.`nickname` , `flash_comment`.`date` , `flash_comment`.`ip` , `product`.`p_name` , `sgflash`.`fid` 
      FROM `flash_comment` 
      LEFT JOIN `product` ON ( `flash_comment`.`p_no` = `product`.`p_no` ) 
      LEFT JOIN `sgflash` ON ( `product`.`p_name` = `sgflash`.`f_name` ) 
      Where `flash_comment`.`p_no` != ''
      orDER BY `flash_comment`.`date`";

--------------------------------------------------------------------------------


//
..... 

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,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

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.

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

See all articles