為什麼我的 php 代碼繞過了一些 if 語句? (Why is my php code bypassing a few if statements?)


問題描述

為什麼我的 php 代碼繞過了一些 if 語句? (Why is my php code bypassing a few if statements?)

As I was working with my program, I had come up with an unknown error. What happens is that my code is bypassing two if statements. Here's my code:

<?
session_start();
if ($_POST){
  $ao = trim($_POST['amountof']);
  $continue = true;
  if (strlen($ao) < 1){  ####First If-Statement Bypassed####
    $continue = false;
    $_SESSION['error'] = "Please enter in a value!";
  }
  if (strlen($ao) > 20){  ####Second If-Statement Bypassed####
    $continue = false;
    $_SESSION['error'] = "Your value has exceeded 20!";
  }
  if (!is_numeric($ao)){
    $continue = false;
    $_SESSION['error'] = "Value is not numeric!";
  }
}

?>
<form method="post">
<?
if (isset($_SESSION['error'])){
  print "<span style=\"color: red; font-weight: bold;\">" . $_SESSION['error'] . "                </span><br />";
}
?>
How many letters are you planning to process?<br />
<span style="font-size:.8em; color: grey;">You may not exceed 20</span><br />
<input type="text" name="amountof" style="width:25px;" maxlength="2"/>
<input type="submit" value="Continue" />
</form>

<? unset($_SESSION['error']); ?>

The if statements that are bypassed are marked in the code. Thank you!


參考解法

方法 1:

Try that:

<?php

 session_start();
 $_SESSION['error']='';

  if (isset($_POST['amountof'])){
  $ao = trim($_POST['amountof']);
  $continue = true;
  if (strlen($ao) < 1){ 
    $continue = false;
    $_SESSION['error'].= "Please enter in a value!<br/>";
  }

  if (!is_numeric($ao)){
    $continue = false;
    $_SESSION['error'].= "Value is not numeric!<br/>";
  }
  elseif ($ao > 20){ 
    $continue = false;
    $_SESSION['error'].= "Your value has exceeded 20!";
  }
}
?>
<form method="post">
<?php
 if (!empty($_SESSION['error'])){

 //......

or simplified(note that the user may type negative values or 0 what currently will pass the test)

if (!is_numeric($ao) || $ao<1 || $ao>20){
        $continue = false;
        $_SESSION['error']= "Please enter in a numeric value between 1 and 20";
      }

方法 2:

Without seeing some sample data, I believe you are simply overwriting the error variable, not bypassing the if statements. Let's run through the code with the case where nothing is entered.

The code inside the first if statement runs, since the strlen($ao) would be less than one. $_SESSION['error'] would be "Please enter in a value!"

Now the code in the second if statement doesn't run, since strlen($ao) is obviously not over 20. 

The code in the third if statement runs, since an empty string is not numeric. Since you are overwriting $_SESSION['error'], the variable would simply be "Value is not numeric!". Thus it would appear the first if statement is bypassed.

Try concatenating the error string instead of overwriting.

EDIT: By using the code snippet posted by Dr Molle just before I answered.

方法 3:

You need to use 

if(isset($_POST)) {

on line two, instead of what you're using, because $_POST behaves like an array.

(by slipperyDr.MolleJosh SDavid)

參考文件

  1. Why is my php code bypassing a few if statements? (CC BY-SA 3.0/4.0)

#if-statement #PHP






相關問題

Python 和 if 語句 (Python and if statement)

Ruby 一種在條件下執行函數的巧妙方法 (Ruby a clever way to execute a function on a condition)

為什麼我的 php 代碼繞過了一些 if 語句? (Why is my php code bypassing a few if statements?)

為什麼“如果”不是C中的表達式 (Why isn't "if" an expression in C)

如何對此查詢進行選擇案例? (How can I do select case to this query?)

我應該使用方法還是常量標誌? (Should I use methods or constant flags?)

PHP - 使用哪個條件測試? (PHP - Which conditional test to use?)

如果日期較新,則將日期從一個數據幀替換為另一個數據幀 (Replace date from one dataframe to another if it's newer)

BASH:在 for 循環中使用 continue (BASH: Using a continue in a for loop)

有沒有辦法從 Tableau 中的 regexp_match 語句中排除某些關鍵字? (Is there a way to exclude certain keywords from a regexp_match statement in Tableau?)

Excel 如果單元格為空白單元格總數的空白單元格總數 (Excel If cell is blank sum number of blank cells for a total)

使用另一個數據框的條件創建一個新列 (Create a new column with a condition of another dataframe)







留言討論