二、编写漂亮的代码
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语句的解析,往往可以提高代码的运行速度。
下面的一段代码证明了我们的结论。在这段代码中,我们使用了前面介绍的时间测试函数。

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
