日期:2011-05-23  浏览次数:20485 次

 ∈率瞪希疟局腥魏我淮Τ鱿值钠葱创砦蠖蓟岬贾峦奈侍狻H绻没Щ骋勺约旱某绦蛑杏衅葱创砦蟮幕埃桓龇浅:玫募觳榉椒ň褪窃诒淞康那昂笙允疚淖中畔ⅰ@纾?


   print “The Combined Result is: |”.$Combined_Result.“|”;

按照上述方法替换输出语句的脚本重新执行之后的结果如下:

   The Combined Result is: ||

 其中,在两个管道符号“|”之间没有任何内容。按照这种方法,我们就可以在程序中找到出现问题的变量。

下面,我们再举一个更加复杂的例子进行说明。
现在,很多网络应用都需要对用户的身份进行验证。一个最简单的实现脚本可以如下:
$#@60;?
 $Password=“Secret”;
 $Name=“admin”;
 function VerifyPassword ($UserPassword, $UserName){
  if ($Password=$UserPassword && $Name=$UserName){
   return 1;
  }
  else { return 0;}

 }
 if (VerifyPassword (“foo1ish”,“admin”)){
  print “The Password is correct”;
 }
 else {
  print “I’m sorry, the password is incorrect”;
 }
?$#@62;

 虽然在上述脚本中调用VerifyPassword函数时输入的是错误的密码,但是程序运行之后仍然会产生如下结果:

   The Password is correct

  问题可能会出现在任何地方,下面就让我们使用排除法一一检查。首先,我们很难确定脚本最后的“if”条件语句是否正确。虽然看上去好象是没有问题,但是为了确保程序正确无误,我们不能放过任何一个环节。因此,我们将该条件语句注释掉,同时按照前面介绍的方法对VerifyPassword()函数进行输出。具体如下:

$#@60;?
 $Password=“Secret”;
 $Name=“admin”;
 function VerifyPassword ($UserPassword, $UserName){
  if ($Password=$UserPassword && $Name=$UserName){
   return 1;
  }
  else { return 0;}
 }
 print “The result of VerifyPassword() is:”;
 print VerifyPassword (“foo1ish”, “admin”);
 /*if (VerifyPassword (“foo1ish”, “admin”)){
  print “The Password is correct”;
 }
 else {
  print “I’m sorry, the password is incorrect”;
 }*/< ?$#@62;

  因为我们使用了错误的密码,所以结果应当为0。但是程序运行之后,我们发现实际结果如下:

   The result of VerifyPassword() is: 1

  这样,我们就知道问题是出现在VerifyPassword()函数的身上。检查了一遍函数之后,我们怀疑问题可能出现在“if”语句中。所以我们屏蔽到VerifyPassword()函数中的条件语句,并进行如下输出:

 print “UserPassword =$#@62; $UserPassword, Password =$#@62; $Password,”;
 print “Password==UserPassword =$#@62; ”.(int)($Password==$UserPassword).“$#@60;BR$#@62;”;
 print “UserName =$#@62; $UserName, Name =$#@62; $Name, ”;
 print “Name==UserName =$#@62;”.(int)($Name==$UserName).“$#@60;BR$#@62;”;

(说明:我们使用(int)($Password==$UserPassword)语句将比较结果转化为整数0或1)

程序修改之后的实际输出结果如下:

UserPassword =$#@62; foo1ish, Password =$#@62; , Password==UserPassword =$#@62; 0
UserName =$#@62; admin, Name =$#@62; , Name==UserName =$#@62; 1
I’m sorry, the password is incorrect

  这里,我们可以清楚的看出Password和Name两个变量都是空值,也就难怪判断语句不起作用了。

  那么为什么$Password为空值呢?我们在程序的开头部分就已经明确的对$Password变量进行了赋值,但是因为某种原其变量值无法带入到VerifyPassword()函数中。回想一下PHP语言关于变量作用域的规定,我们可以立刻找到出现问题的原因,那就是如果要想在函数中使用变量,必须将该变量声明为全局变量。知道了错误的根源之后,我们在VerifyPassword()函数的第一行加入以下语句,然后重新运行程序:

global $Password, $Name;

程序运行结果如下:

UserPassword =$#@62; foo1ish, Password =$#@62; Secret, Password==UserPassword =$#@62; 0
UserName =$#@62; admin, Name =$#@62; admin, Name==UserName =$#@62; 1
The Password is correct

  为什么?按理说我们应当得到密码不正确的错误提示。再一次仔细检查遍程序之后,终于发现原来我们把逻辑运算符“==”误用成“=”,这样就把$UserPassword变量的值赋给了$Password变量。把最后的这两处错误改正过来之后,完成的程序如下:

$#@60;?
 $Password=“Secret”;
 $Name=“admin”;
 function VerifyPassword ($UserPassword, $UserName){
  global $Password, $Name;
  if ($Password==$UserPassword && $Name==$UserName){
   return 1;
  }
  else { return 0;}

 }
 if (VerifyPassword (“foo1ish”, “admin”)){
  print “The Password is correct”;
 }
 else {
  print “I’m sorry, the password is incorrect”;
 }
?$#@62;

执行程序得到如下结果:

  I’m sorry, the password is incorrect.

输入正确密码重新运行时,得到以下结果:

  The Password is correct

这样,我们就成功的找到并解决了问题。希望大家能够从以上的介绍中领悟到查找错误时的一些方法和思路。
友情链接: 爱易网 云虚拟主机技术 云服务器技术 程序设计技术 开发网站 APP开发教程
Copyright © 2013-2024 爱易网页 当前在线:852人  网站在11时55分29秒内访问总人数:127911人 当前 19.03%  粤ICP备18100884号-2
下页8