Home Backend Development PHP Tutorial PHP 高手之路(二)_php基础

PHP 高手之路(二)_php基础

May 17, 2016 am 09:47 AM

二、编写漂亮的代码
  1.将后台程序与前端程序分开
  在编写PHP程序时,有些代码是用来处理一些事务,例如操作数据库、进行数学运算等,而另外的一些代码则只是事务处理的结果显示出来,例如一些使用echo语句将结果以HTML的格式显示在Web浏览器上的PHP代码以及那些直接嵌入PHP程序的HTML代码。首先我们应该清晰地区分这两种代码,把前者称为后台程序,把后者称为前端程序。
  因为PHP是一种嵌入式编程语言,也就是说,所有的PHP代码都可以嵌入到HTML代码之中,这为程序的编写带来了许多便利之处。但是,“物极必反”,如果在一段较长的程序中将PHP代码和HTML代码混合编写,这将使程序杂乱无章,不利于程序的维护和阅读。所以我们需要尽可能地将这些程序中混杂于HTML代码中的PHP代码移植出来,在专门的文件中将这些代码封装成函数,然后在HTML代码中使用include语句来包含这些文件,在适当的位置调用这些函数即可。
  这种做法一方面使HTML代码和PHP代码都简单易读,另一方面因为HTML代码需要不断更新,而这种分离的方法可以确保后台程序不会被破坏。
  同前端程序不同,后台程序更多追求的是稳定、结构化,极少更改,所以应该认真地设计和管理。其实,在设计台程序时,投入大量时间是值得的,“现在栽树,以后乘凉”,在以后的设计工作中将可以轻松地使用现在编写的后台程序。
  2.灵活使用包含文件
  正如前面所说的那样,后台程序应当安排在一系列的包含文件中。包含文件可以通过include语句在需要时动态装入,也可以在php.ini文件中通过使用auto_prepend_file指令预先自动装入。
  如果使用后一种方法的话,虽然取得了一劳永逸的好处,但是也有一些缺点值得我们注意。下面的一段代码向我们展示了解析一个庞大的包含文件需要一定的时间:
    require(timing.inc);
  ss_timing_start();
  include(test.inc);
  ss_timing_stop();
  echo
.ss_timing_current().

  ?>
  在上面的代码中,test.inc是一个1000行的包含文件,运行的结果显示,解析这个包含文件花费了0.6秒钟,对于一个大型网站来说,这个速度并不是可以忽略不记的。
  使用包含文件的另外一个缺点是:如果一个文件中的一个语句发生错误,将会使整个网站的PHP程序都无法运行。所以使用起来也及其小心。
  其实,对包含文件稍做处理,即可以使包含文件只在需要时进行解析。下面的代码使abc.inc文件只在程序需要时才作解析:
    if ( defined( __LIBA_INC) ) return;
  define( __LIBA_INC, 1 );
  /*
  * 代码...
  */
  ?>
  3.使用面向对象的编程方法
  PHP也是一种面向对象的语言,面向对象的编程方法是优秀的程序员们非常推崇的一种软件设计方法,在PHP编程中可以充分发挥面向对象语言的优势,对编程中的对象进行封装。在前面的代码中,我们使用了面向对象的方法,例如在管理数据库时,我们将query()函数封装进数据库类中,这极大地方便了代码的管理,增加了程序的可读性。
  三、追求程序速度,而不是编程的速度
  在网站建设中,程序运行速度和网页下载速度都是关系成败的重要因素。作为一名Web程序员,应该更加注意代码的运行速度。下面介绍的几种方法都在不同程度上提高了代码的运行速度。
  1.使用内嵌的HTML代码,而不是PHP的echo语句。
  因为PHP是一门嵌入式Web编程语言,可以将HTML代码和PHP代码相互嵌入。但是很多程序员担心在HTML代码中过多的使用""嵌入PHP代码会多次调用PHP解释器,从而降低了PHP代码的运行速度,所以宁愿使用PHP的echo语句来输出HTML代码,而不直接使用HTML代码。但事实却恰恰相反。每一个PHP页面只调用一次PHP解释器来解释所有的PHP代码,所以,只在需要时才嵌入PHP代码,而大多数的时候直接使用HTML代码输入结果,不但不会降低程序的运行速度,而且因为减少了对echo语句的解析,往往可以提高代码的运行速度。
  下面的一段代码证明了我们的结论。在这段代码中,我们使用了前面介绍的时间测试函数。

  

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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1248
24
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.

Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

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

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

How does PHP type hinting work, including scalar types, return types, union types, and nullable types? How does PHP type hinting work, including scalar types, return types, union types, and nullable types? Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

See all articles