但是,要成为一名PHP编程高手却并不容易。并不像很多人想象的那样,只要能够飞快地编写几条简单的代码去解决一个复杂的问题就是PHP编程高手了,真正的PHP高手还需要考虑更多的其它问题。以下三条准则是一名成熟的PHP程序员在编程中应该首先遵循的准则。
1.懒惰是金
2.编写漂亮的代码
3.追求程序的速度,而不是编程的速度
一、懒惰是金
做一个懒惰的程序员吗?这个想法太奇怪了!因为这个世界上最忙碌的人可能就是计算机程序员了。但正是因为程序员太忙了,所以才应该在编程时学会偷懒。
对于一个程序员来说,懒惰的方法有两种:其一,大胆使用现成的别人的程序代码,把这些代码融入到你自己的程序或者项目中去。其二是编写一些有用的代码建立一个函数库,在将来编写程序时可以顺手拈来,省去了许多重复的劳动,自然就可以懒惰一点了。
这两种偷懒的方法都非常适合PHP程序员了。
首先,PHP是在自由开放的环境中诞生和成长的一门语言。在世界各地,有成千上万的程序员,他们一直在为PHP的完美而不断奋斗,他们也愿意和别人分享自己的聪明才智和自己编写的代码。你每天都可以从一些PHP网站、邮件列表、新闻组发现大量的优秀的程序代码。这样说,我并不是鼓励你整天等着让别人为你编写代码,但是你可以“站在伟人的肩膀上”,充分发扬“拿来主义”,聪明地应用别人的程序代码可以节省你大量时间。其次,在PHP中,你可以方便地建立自己的函数库,这样可以在你以后编写程序时省去很多麻烦。
下面笔者为大家介绍几个通用的函数,这些函数有的来自网上的一些开放源代码的项目,有的精选自邮件列表。如果你能把它们加入到你自己的函数库中,迟早你将会发现自己受益无穷。
1.通用数据库处理函数
和其它的CGI函数相比,PHP的优点之一是具有很强大的数据库处理能力。但是,在PHP中,对于不同的数据库都使用一些特定的函数来专门处理,缺少通用的数据库处理函数。这大大降低了程序代码的可移植性,这也为初学编程的朋友带来了很多不便。
在网上,许多程序员都通过封装类解决了这个问题。他们编写了统一的函数用来处理任何流行的数据库——不管是在linux世界深受欢迎的MySQL还是在Windows平台上广泛流行的SqlServer。就笔者个人来说,非常喜欢使用这些函数,因为可以直接使用一些简单的诸如"query"、"next_record"之类的函数,而不需要考虑数据库的连接、数据库句柄这些复杂的东西,更不需要考虑使用的是何种数据库。
如果你需要这些函数,你可以通过访问以下的几个网址而得到:
http://phplib.netuse.de/
http://phpclasses.UpperDesign.com/browse.html/package/20
http://phpdb.linuxbox.com/
2.变量调试函数
PHP程序的调试一直是一件让人头疼的事,它既不像VB等高级语言那样有集成的编译调试环境,也不想Perl那样可以在Linux或者DOS环境下直接运行。其实,我们完全可以通过灵活地使用echo语句来完成对PHP的调试工作。
下面的几个函数可以让你随时查看程序中任何变量的类型及其值。
function ss_array_as_string (&$array, $column = 0) {
$str = "Array(
n";
while(list($var, $val) = each($array)){
for ($i = 0; $i $str .= "    ";
}
$str .= $var. ==> ;
$str .= ss_as_string($val, $column+1)."
n";
}
for ($i = 0; $i $str .= "    ";
}
return $str.);
}
function ss_object_as_string (&$object, $column = 0) {
if (empty($object->classname)) {
return "$object";
}
else {
$str = $object->classname."(
n";
while (list(,$var) = each($object->persistent_slots)) {
for ($i = 0; $i $str .= "    ";
}
global $$var;
$str .= $var. ==> ;
$str .= ss_as_string($$var, column+1)."
n";
}
for ($i = 0; $i $str .= "    ";
}
return $str.);
}
}
function ss_as_string (&$thing, $column = 0) {
if (is_object($thing)) {
return ss_object_as_string($thing, $column);
}
elseif (is_array($thing)) {
return ss_array_as_string($thing, $column);
}
elseif (is_double($thing)) {
return "Double(".$thing.")";
}
elseif (is_long($thing)) {
return "Long(".$thing.")";
}
elseif (is_string($thing)) {
return "String(".$thing.")";
}
else {
return "Unknown(".$thing.")";
}
}
需要的时候,在程序中简单地加入下面的一条代码即可查看程序中的所使用的变量(包括数组和对象)的类型和值:
echo ss_as_string($my_variable);
使用下面的语句,我们可以直接查看程序中所有的变量的值:
echo ss_as_string($GLOBALS);
3. 控制Log信息的函数
调试PHP程序的另外一种重要的方法就是查看Log信息。如果能够方便地控制Log信息的级别以及Log信息的显示内容,将会给程序调试带来更多的便利。下面的几个函数可以方便地实现这个功能。
$ss_log_level = 0;
$ss_log_filename = /tmp/ss-log;
$ss_log_levels = array(
NONE => 0,
ERROR => 1,
INFO => 2,
DEBUG => 3);
function ss_log_set_level ($level = ERROR) {
global $ss_log_level;
$ss_log_level = $level;
}
function ss_log ($level, $message) {
global $ss_log_level, $ss-log-filename;
if ($ss_log_levels[$ss_log_level] // 不显示Log信息
return false;
}
$fd = fopen($ss_log_filename, "a+");
fputs($fd, $level. - [.ss_timestamp_PRetty().] - .$message."n");
fclose($fd);
return true;
}
function ss_log_reset () {
global $ss_log_filename;
@unlink($ss_log_filename);
}
在上面的函数中,有四个Log级别变量。运行PHP程序时,只有当Log的级别低于预设的级别值时,Log信息才可以被记录和显示出来。例如,在程序中加入如下的一条语句:
ss_log_set_level(INFO);
那么,运行PHP程序时,只有ERROR和INFO级别的LOG信息才能被记录和显示出来,DEBUG级的信息则被忽略了。除此之外,我们还可以设定显示的信息内容,其语句如下:
ss_log(ERROR, "testing level ERROR");
ss_log(INFO, "testing level INFO");
ss_log(DEBUG, "testing level DEBUG");
你也可以随时使用下面的语句清空LOG信息:
ss_log_reset();
4.速度测试函数
为了优化代码,我们需要一种可以测试代码运行时间的方法,从而来选择最优的代码。下面的函数可以测试运行代码所需的时间:
function ss_timing_start ($name = default) {
global $ss_timing_start_times;
$ss_timing_start_times[$name] = explode( , microtime());
}
function ss_timing_stop ($name = default) {
global $ss_timing_stop_times;
$ss_timing_stop_times[$name] = explode(, microtime());
}
function ss_timing_current ($name = default) {
global $ss_timing_start_times, $ss_timing_stop_times;
if (!isset($ss_timing_start_times[$name])) {
return 0;
}
if (!isset($ss_timing_stop_times[$name])) {
$stop_time = explode(, microtime());
}
else {
$stop_time = $ss_timing_stop_times[$name];
}
$current = $stop_time[1] - $ss_timing_start_times[$name][1];
$current += $stop_time[0] - $ss_timing_start_times[$name][0];
return $current;
}
现在可以轻松地检查任何一段代码的执行时间了,甚至我们可以同时使用多个计时器,只需在使用上述的几个函数时设定不同的参数作为计时器的名称就可以了。
5.调试和优化数据库的操作
对于数据库来说,运行速度是至关重要的。尽管很多书籍和文章都讲授了一些快速运行数据库的方法,但是所有的方法都必须经过实践的检验。下面我们将把PHPLib函数库中的query()函数和上面介绍的几个函数综合起来编写成新的query()函数,和原先的函数相比,这个函数增加了运行时间的监测功能。
function query($Query_String, $halt_on_error = 1) {
$this->connect();
ss_timing_start();
$this->Query_ID = @mysql_query($Query_String,$this->Link_ID);
ss_timing_stop();
ss_log(INFO, ss_timing_current(). Secs - .$Query_String);
$this->Row = 0;
$this->Errno = mysql_errno();
$this->Error = mysql_error();
if ($halt_on_error && !$this->Query_ID) {
$this->halt("Invalid SQL: ".$Query_String);
}
return $this->Query_ID;
}(未完待续)

Setting session cookie parameters in PHP can be achieved through the session_set_cookie_params() function. 1) Use this function to set parameters, such as expiration time, path, domain name, security flag, etc.; 2) Call session_start() to make the parameters take effect; 3) Dynamically adjust parameters according to needs, such as user login status; 4) Pay attention to setting secure and httponly flags to improve security.

The main purpose of using sessions in PHP is to maintain the status of the user between different pages. 1) The session is started through the session_start() function, creating a unique session ID and storing it in the user cookie. 2) Session data is saved on the server, allowing data to be passed between different requests, such as login status and shopping cart content.

How to share a session between subdomains? Implemented by setting session cookies for common domain names. 1. Set the domain of the session cookie to .example.com on the server side. 2. Choose the appropriate session storage method, such as memory, database or distributed cache. 3. Pass the session ID through cookies, and the server retrieves and updates the session data based on the ID.

HTTPS significantly improves the security of sessions by encrypting data transmission, preventing man-in-the-middle attacks and providing authentication. 1) Encrypted data transmission: HTTPS uses SSL/TLS protocol to encrypt data to ensure that the data is not stolen or tampered during transmission. 2) Prevent man-in-the-middle attacks: Through the SSL/TLS handshake process, the client verifies the server certificate to ensure the connection legitimacy. 3) Provide authentication: HTTPS ensures that the connection is a legitimate server and protects data integrity and confidentiality.

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 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 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 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.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 English version
Recommended: Win version, supports code prompts!

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),

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.