search
HomeBackend DevelopmentPHP Tutorialphp编写大型网站问题集_PHP

php编写大型网站问题集_PHP

Jun 01, 2016 pm 12:32 PM
phpusevariableLargeprogramwritewebsitequestion

PHP以其易用性得到迅速的推广,但易用并不是说就能用好它,实际上许多程序员用它很容易的立一个个WEB应用系统,但又有多少人仔细的考虑过他们的代码,是否容易维护、是否足够健壮、否效率足够高、是否足够安全,当PHP用于建立大型网站时这些就成为很关键的因素。下面我们从较轻微的问题开始讨论,直至一些致命的错误。共分三部分。

第一部分、较轻微的错误

一、Printf(),
  该函数主要用来格式化显示数据。当你要改变某个数据的显示格式时才使用。例如以不同的精度来显示PI(3.1415926)的值。

 /*
 * The three faces of Π
 */
 printf ("Pi is: %.2fn
n", M_PI);
 printf ("Pi is also: %.3fn
n", M_PI);
 printf ("Pi is also: %.4fn
n", M_PI);
?>

  但许多程序员仅仅为显示一些变量值和函数返回值使用该函数。因为Printf()在显示数据前要先格式化该数据以速度较慢,因此,仅为了显示数据时应用print和echo,以提高速度。

二、语意检查
  PHP是一种弱类型语言,也就是说在使用一个变量前不用定义,这样给编程带来了很大的方便和灵活,但你自己必须知道该变量到底应该是哪种类型,因为该变量在运行时仍实际对应着某一种类型(各种类型之间可以自由互相转换),没有类型的变量是不存在的。有可能PHP并不能检查出你的语意错误,但由于变量类型的变化,会导致一些潜在的问题的发生。另外一个值得注意的问题是变量的范围,它也可能会导致一些潜在的问题的发生。

在PHP中有以下几种基本变量:
Boolean, resource, integer, double, string, array and object。

三、临时变量的使用
  临时变量的滥用会导致程序运行效率的降低。何时使用临时变量可基于以下两点考虑:

1、该变量是否至少使用两次。
2、该变量的使用是否会显著提高程序的可读性。

如果一条也不满足,则省略该变量的使用。例如:

 $tmp = date ("F d, h:i a"); /* ie January 3, 2:30 pm */
 print $tmp;
?>

就应该改成:

 print date ("F d, h:i a");
?>

又如:

// string reverse_characters(string str)
// Reverse all of the characters in a string.
function reverse_characters ($str)
{
 return implode ("", array_reverse (preg_split("//", $str)));
}
?>

的可读性不强,可改成:

// string reverse_characters(string str)
// Reverse all of the characters in a string.
function reverse_characters ($str)
{
 $characters = preg_split ("//", $str);
 $characters = array_reverse ($characters);
 return implode ("", $characters);
}
?>

四、客户端和服务器端代码的分离
  客户端和服务器端代码的在PHP程序中实际上就是HTML代码和PHP语言代码,很多人把HTML和PHP语句混合在一个文件里,使得这文件很大,这种风格对程序的维护和再开发很不利,不适合大型站点的开发。一般有两种方法把HTML和PHP语句分开:

1、编写专用API,例如:

index.php ? The Client side




<?php print_header (); ?>
















site.lib ? The server side code

$dbh = mysql_connect ("localhost", "sh", "pass") or die (sprintf ("Cannot connect to MySQL [%s]: %s", mysql_errno (), mysql_error ()));
@mysql_select_db ("MainSite") or die (sprintf ("Cannot select database [%s]: %s",mysql_errno (), mysql_error ()));
$sth = @mysql_query ("SELECT * FROM site", $dbh) or die (sprintf ("Cannot execute query [%s]: %s", mysql_errno (), mysql_error ()));

$site_info = mysql_fetch_object ($sth);

function print_header ()
{
 global $site_info;
 print $site_info->header;
}

function print_body ()
{
 global $site_info;
 print nl2br ($site_info->body);
}

function print_links ()
{
 global $site_info;

 $links = explode ("n", $site_info->links);
 $names = explode ("n", $site_info->link_names);

for ($i = 0; $i {
 print "ttt $names[$i] n
n";
}
}
?>

这种方法使得程序看起来比较简洁,而且执行速度也较快。


2、使用模板的方法

这种方法使得程序看起来更简洁,同样实现上面的功能,可用以下代码:


%%PAGE_TITLE%%


%%PAGE_TITLE%%







%%PAGE_LINKS%% %%PAGE_CONTENT%%




  用占位符代替要动态生成的内容,然后用一解析程序分析该模板文件,把占位符用际的内容替换。种方法使得即使不会使用PHP的页面制作人员也能修改模板文件。这种方法的缺点是执行效率不高,因为要解释模板文件。同时实现起来也比较复杂。

注: www.thewebmasters.net的 FastTemplate class可方便的实现以上功能。

五、不要用过时的函数

作为一种自由软件,PHP发展很快,其中的很多函数都已过时,例如:

while (1):
print "5";
if ($idx++ == 5):
break;
endif;
endwhile;

  虽然还能用但效率肯定不高,而且可能在以后的版本中会禁用,导致程序不能运行。因此要经常对照最新PHP手册检查那些函数已过时及时修正。
Statement
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

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.

See all articles

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

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool