在php中的if条件中为变量赋值

问题描述 投票:-1回答:6

任何人都可以解释如何在php中为变量分配值。我有以下代码,不能按预期工作(由我)。

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

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

编辑

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

php if-statement variable-assignment
6个回答
0
投票

在编辑和检查代码之后,似乎布尔值越过了对实际值的写入。要摆脱这个东西,你需要在每个条件上使用圆括号进行正确的分配,称为Operator Precedence

检查我编辑过的code,以便按预期工作。


0
投票

第一个代码块在语法上是错误的。

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

0
投票

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

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

此代码将明确输出getClientAccount方法的结果(除非它为0或空数组)。

因此,OP必须再次检查“实际代码中有什么”,或者验证getClientAccount函数的实际结果。


0
投票
$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'] : '';

-1
投票

从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;
}

-1
投票

语法错误很少。您需要删除已经提到的半冒号,您需要更改为:

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

要检查条件,当您尝试将$ account分配给$ this-> getClientAccount($ request)时,会产生if语句。如果语句用于检查不分配值的条件。

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

© www.soinside.com 2019 - 2024. All rights reserved.