【问题标题】:assigning values to a variable in if condition in php在php中的if条件中为变量赋值
【发布时间】:2013-12-05 12:16:48
【问题描述】:

谁能解释在 php.ini 中如何为变量赋值?我有以下代码不能按预期工作(由我)。

if ( $account = $this->getClientAccount($request) )
{
     echo $account;
}

如果我使用此代码帐户是 1,则只有 ONE。怎么会??? getClientAccount 返回字符串(用户名),如果未找到用户,则返回 false。当发现用户时返回 1。 如果我将赋值移到 if 语句上方,它工作正常。将函数返回的数组赋值给 if 语句中的变量时,我遇到了类似的问题。

编辑

好吧,伙计们,我想我发现了一些东西,至少对我们所有人来说都是一些东西。 看这段代码:http://codepad.org/QWjwl3vQ 我想你会明白发生了什么。试试 test1() 和 test2()

【问题讨论】:

  • if 语句条件为什么要用分号?
  • 已编辑。删除了那个分号。它实际上不在我的代码中
  • 如果可能的话,展示你的功能。 getClientAccount,让大家有更好的想法。
  • 注意我的话:如果我将赋值移到 if 语句上面,它可以正常工作,正如预期的那样
  • 请查看编辑。

标签: php if-statement variable-assignment


【解决方案1】:
$staff_id_id = 0;
$salary = new salary();

$result = $salary -> getSalary($id);

// print_r($result);
$inv = mysqli_fetch_array($result, MYSQLI_ASSOC);

$staff_id = isset($inv['staff_id']) ? $inv['staff_id'] : '';
$present_days = isset($inv['present_days']) ? $inv['present_days'] : '30';
$total_days = isset($inv['total_days']) ? $inv['total_days'] : '30';
$advance_deducted = isset($inv['advance_deducted']) ? $inv['advance_deducted'] : '1';
$installment_deducted = isset($inv['installment_deducted']) ? $inv['installment_deducted'] : '0';

// print_r($inv);


$staff_name1 = isset($inv['staff_id']) ? $inv['staff_id'] : '';

if ($staff_name1 == $staff_id) {
  echo $staff_id++;
  //print_r($staff_id);
  //die();
  // echo $staff_name1;
}


if ($staff_id > 0) {
  echo "this user had already assign Vouchers";
}

else {



  $salary = new salary();

  $result = $salary -> getSalary($id);

  // print_r($result);
  $inv = mysqli_fetch_array($result, MYSQLI_ASSOC);



  $staff_id = isset($inv['staff_id']) ? $inv['staff_id'] : '';
  //print_r($staff_id);

  $date = isset($inv['date']) ? $inv['date'] : '';

  $advance_deducted = isset($inv['advance_deducted']) ? $inv['advance_deducted'] : '1';

  $installment_deducted = isset($inv['installment_deducted']) ? $inv['installment_deducted'] : '0';

  $installment_amount = isset($inv['installment_amount']) ? $inv['installment_amount'] : '';

  $paid_amount = isset($inv['paid_amount']) ? $inv['paid_amount'] : '';

  $total_days = isset($inv['total_days']) ? $inv['total_days'] : '30';

  $present_days = isset($inv['present_days']) ? $inv['present_days'] : '30';

  $amount_left = (isset($inv['amount_left']) && $inv['amount_left'] != NULL) ? $inv['amount_left'] : '0';

  $salary_id = isset($inv['id']) ? $inv['id'] : '';

【讨论】:

    【解决方案2】:

    在编辑和检查您的代码后,布尔值似乎覆盖了实际值。要摆脱这个问题,您需要在每个条件上使用圆括号来正确分配称为Operator Precedence

    检查您的code,我已对其进行编辑以按您的预期工作。

    【讨论】:

    • 你成功了。非常感谢。但我不明白为什么没有额外的大括号就不能工作
    • 欢迎您!!查看我在答案中添加的运算符优先级参考以获取详细信息。
    【解决方案3】:

    几乎没有语法错误。您需要删除已经提到的分号,您需要更改为:

    $account == $this->getClientAccount($request)
    

    要检查一个条件,此时您正尝试将 $account 分配给 $this->getClientAccount($request) 导致 if 语句。 if语句用于检查不赋值的条件。

    $account 也必须有一个值,否则如果函数返回一个值,您的条件将始终保持为假。

    【讨论】:

      【解决方案4】:

      这根本不是一个真正的问题,尝试直接回答是没有意义的,尤其是如果您不熟悉 PHP 语法。

      必须质疑的是 OP 的 前提,而不是他发布的代码。

      这段代码会直接输出getClientAccount方法的结果(除非是0或空数组)。

      因此,OP 必须再次检查“代码中实际存在的内容”一次或验证 getClientAccount 函数的实际结果。

      【讨论】:

      • 您的新问题与您提出的完全不同。 在提出问题之前你必须先下定决心。
      • 完全一样。请仔细阅读!
      • 如果您仍然认为没有区别,那么您没有得到您接受的答案
      【解决方案5】:

      从 if 条件中删除分号,如下所示

      if
      (
          $account = $this->getClientAccount($request)
      )
      {
          echo $account;
      }
      

      在您的情况下,php 将在从 getClientAccount() 函数获取返回值后检查 $account 变量的值。如果 $account 值最终意味着 true,那么它将执行您的 echo 语句。

      如果您对 php 布尔值有任何困惑,那么您可以尝试另一种方式:

      $account = $this->getClientAccount($request);
      if(strlen($account)>0){
          echo $account;
      }
      

      【讨论】:

        【解决方案6】:

        第一段代码语法上是错误的。

        if
            (
                $account = $this->getClientAccount($request); // <--- This semicolon here must be removed
            )
        {
             echo $account;
        }
        

        【讨论】:

          猜你喜欢
          • 2011-02-15
          • 2013-07-14
          • 2012-08-23
          • 2014-03-11
          • 2013-12-05
          • 1970-01-01
          • 2011-07-24
          • 1970-01-01
          • 2021-08-13
          相关资源
          最近更新 更多