php中的连接& 条件。

问题描述 投票:0回答:1

我正在学习PHP,我想在$autocollant_total_ht_custom设置时显示"€"。

这是我写的。

  $euro = " €";

  if (isset($autocollant_total_ht_custom)) {
    $autocollant_total_ht_custom = $autocollant_total_ht_custom . $euro;
  } else echo " ";

然而我的"€"总是显示,即使$autocollant_total_ht_custom没有设置。

我花了75分钟的时间,尽管经过研究,但还是一次又一次的失败。

我还尝试了 !is_null, !is_empty,结果都一样。

我很确定我的逻辑没有错,但方法是错的。

有谁能救救我?

祝大家周六愉快!

Mike.

编辑1:

一个小小的视觉辅助 形象

我的目标是只有在单元格中确实有东西时才显示其内容。默认情况下,我可以看到空单元格中的内容为0。

  if (!$autocollant_total_ht_lot10) {
    $autocollant_total_ht_lot10 = " ";
  } else echo "error ";

  if (!$autocollant_total_ht_lot20) {
    $autocollant_total_ht_lot20 = " ";
  } else echo " ";

  if (!$autocollant_total_ht_lot50) {
    $autocollant_total_ht_lot50 = " ";
  } else echo " ";

  if (!$autocollant_total_ht_custom) {
    $autocollant_total_ht_custom = " ";
  } else echo " ";

我知道我的代码看起来很原始,但它是有效的,而且我没有看到它与我们在最初的问题中试图实现的目标有任何冲突。

然后,按照要求,这是我在表行和表数据中写的内容。

  <tr>
    <td class=table_align_left>A partir de 100</td>
    <td><?php echo $autocollant_prix ?></td>
    <td><?php echo $autocollant_custom?></td>
    <td><?php echo $autocollant_total_ht_custom?> </td>
  </tr>

所以,简而言之,我试图在没有值要显示的情况下不显示任何东西(目前是有效的),然后在变量后面添加一个"€",就是有东西要显示。

编辑2 :

我的原始代码 : my_code

编辑3 :

$autocollant_total_ht_custom已经在这句话的前面有条件地显示出来了。

  } elseif($autocollant_quantité >= 90 && $autocollant_quantité <= 99){
      $autocollant_quantité_lot50 = 2;
  } elseif($autocollant_quantité >= 100 && $autocollant_quantité <= 1000){
      $autocollant_custom = $autocollant_quantité;
  } else echo "entrée invalide";



  $autocollant_total_ht_custom = $autocollant_prix * $autocollant_custom;
  $autocollant_total_ht_lot10 = $autocollant_prix_lot10 * $autocollant_quantité_lot10;
  $autocollant_total_ht_lot20 = $autocollant_prix_lot20 * $autocollant_quantité_lot20;
  $autocollant_total_ht_lot50 = $autocollant_prix_lot50 * $autocollant_quantité_lot50;

  $pointeuse_total_ht = $pointeuse_prix * $pointeuse_quantité;
  $pointeuse_autocollant_offert = $pointeuse_quantité * 10;
  $pointeuse_autocollant_offert_total_ht = $pointeuse_autocollant_offert * $autocollant_prix;
  $pointeuse_autocollant_offert_total_ht = $pointeuse_autocollant_offert * $autocollant_prix;

如果能帮上忙的话,我贴出了我的代码。

Mike.

php if-statement conditional-statements concatenation
1个回答
-1
投票
//$autocollant_total_ht_custom = null;
$autocollant_total_ht_custom = "something that isnt null"; 
//if you switch the variable assignment above you will see it behaves as expected.
 $euro = "€";

  if (isset($autocollant_total_ht_custom)) 
  {         
    echo $autocollant_total_ht_custom = $autocollant_total_ht_custom . " " .$euro;
  } 
   else 
  {  
     //$autocollant_total_ht_custom wouldn't be set at all if we reach this point, this is why im un-sure what your requirements are. Nothing would be echoed.
     echo  $autocollant_total_ht_custom;
  }

也许是这样的?很难理解你的具体要求。

如果是 检查一个变量是否被设置为 东西 如果它不是空的,那么它就通过了测试,如果你在这个变量处操作字符串,那么它永远不会是空的,这意味着euro符号会一直显示出来。

如果变量是空的,那么你的条件测试就失败了,点击else,然后回传什么都不是空字符串。

如果你能更新你的答案,将"$autocollant_total_ht_custom "设置为什么,我可以更好地帮助你。

EDIT.在我看来,你可以简化"$autocollant_total_ht_custom "的设置,我可以更好地帮助你。

在我看来,你可以简化你所做的事情 基本上,我们只关心是否设置了什么,是否会回传一个字符串,否则,就没有必要做任何事情,所以你的检查可以简单到

$autocollant_total_ht_lot10 = null;
$autocollant_total_ht_lot11 = "";
$autocollant_total_ht_custom = "1,000"; 
 $euro = "€";

if (isset($autocollant_total_ht_custom)) 
{         
  echo 'ht custom';
  echo TDFromString($autocollant_total_ht_custom, $euro);
} 

//notice this doesnt output anything because it isnt set
if (isset($autocollant_total_ht_lot10, $euro)) 
{         
  echo 'lot 10';
  echo TDFromString($autocollant_total_ht_lot10, $euro);
}

//notice this does because the string, while empty is something that isnt null
if (isset($autocollant_total_ht_lot11)) 
{     
  echo 'lot 11';
  echo TDFromString($autocollant_total_ht_lot11, $euro);
}
//lets set it to null and see what happens
$autocollant_total_ht_lot11 = null;
if (isset($autocollant_total_ht_lot11)) 
{     
  echo 'lot 11 AGAIN';
  echo TDFromString($autocollant_total_ht_lot11,  $euro);
}
//it doesnt get printed!

//create a function that takes the string in question, 
//and for the sake of your use case also the currency to output, 
//that way you could change 'euro' to 'currency' 
//and have the sign change based on what the value of the $currency 
//string is, eg $currency = "£"

function TDFromString($string, $currency)
{
  return '<td>' . $string . ' ' .$currency . '</td>';
}

活生生的例子。https:/3v4l.orgr5pKt

一个更明确的例子。https:/3v4l.orgJtnoF

我增加了一个额外的echo来指示(和换行)哪个变量被打印出来,当然你不需要它!我只想说,函数名是一个很好的例子,因为它不仅返回字符串周围的td,而且还插入了货币,你可能想给它起个更好的名字:)

我只想指出这个函数名是一个很好的例子,因为它不仅在字符串周围返回一个td,而且还插入了货币,你可能需要给它起一个更好的名字 :)

EDIT编辑。

最后编辑一下,在你的问题范围之外,你应该考虑将数据保存在数组中,而不是在数组中工作。

使用前面的例子,我们可以将代码简化为这样 !

    $autocollant_total_ht_lot10 = null;
    $autocollant_total_ht_lot11 = "";
    $autocollant_total_ht_lot12 = "2,0000000";
    $autocollant_total_ht_custom = "1,000"; 
     $euro = "€";
     //create an array, and stick all our strings in it, from now, if we need to do something to one of the strings(or all!), we do it through the array
    $arrayofLots = array($autocollant_total_ht_lot10, $autocollant_total_ht_lot11, $autocollant_total_ht_lot12, $autocollant_total_ht_custom);

   //go over each array 'entry' so the first time is '$autocollant_total_ht_lot10', then '$autocollant_total_ht_lot11' etc
    foreach ($arrayofLots as $lot)
    { 
        //and we've been over this bit :)
        //$lot is a variable we set so we have something to refer to for the individual array entry  we are on, we could just as easily name it anything else
        if (isset($lot)) 
        {         
          echo TDFromString($lot, $euro);
        } 
    }


function TDFromString($string, $currency)
{
  return '<td>' . $string . ' ' .$currency . '</td>';
}

-2
投票

你好,你的问题是什么?看起来你缺少了尾部的括号。

if (isset($autocollant_total_ht_custom)) {
    $autocollant_total_ht_custom = $autocollant_total_ht_custom . $euro;
} else { 
    echo " ";
}
© www.soinside.com 2019 - 2024. All rights reserved.