dongshanni1611 2010-08-18 16:49
浏览 93

变量在IF语句中丢失范围/值?

I have the weirdest problem I cannot figure out, see the following code:

 $frmUsername = $_POST['frmUsername'];
 $frmPassword = $_POST['frmPassword'];

 if($frmUsername == "" || $frmPassword == "") {
  print "frmUsername: " . $frmUsername;
  print "frmPassword: " . $frmPassword;

 } exit();

The result will be:

frmUsername: frmPassword:

But if I do the same thing and move the print statements outside of the IF:

 $frmUsername = $_POST['frmUsername'];
 $frmPassword = $_POST['frmPassword'];

 print "frmUsername: " . $frmUsername;
 print "frmPassword: " . $frmPassword;

 if($frmUsername == "" || $frmPassword == "") {

 } exit();

The result will be:

frmUsername: MYUSERNAMEfrmPassword: MYPASSWORD

So, why is the IF statement thinking frmUsername and frmPassword are blank, event when they're not, example:

 $frmUsername = $_POST['frmUsername'];
 $frmPassword = $_POST['frmPassword'];

 if($frmUsername == "" || $frmPassword == "") {
      print "I think the strings are empty, even when they're not";
 } exit();

The result will be:

I think the strings are empty, even when they're not

Second example:

$frmUsername = $_POST['frmUsername'];
$frmPassword = $_POST['frmPassword'];

if($frmUsername == "" || $frmPassword == "") {
    print "I think the strings are empty, even when they're not: '$frmUsername' '$frmPassword'";
    exit();
}

The result will be:

I think the strings are empty, even when they're not: '' ''

  • 写回答

4条回答 默认 最新

  • dougan6982 2010-08-18 16:51
    关注

    So, why is the IF statement thinking frmUsername and frmPassword are blank?

    Works as designed.

    You are saying:

    if ($frmUsername is empty) OR ($frmPassword is empty), do the following: .....

    if both strings are not empty, the condition won't match.

    What you probably want is

    if($frmUsername != "" && $frmPassword != "")
    

    which will match only when both strings contain a value.

    评论

报告相同问题?

悬赏问题

  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch