PHP - 如果为空值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6507384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:35:27  来源:igfitidea点击:

PHP - If null value

phpif-statement

提问by Vince Pettit

I have the following coding:

我有以下编码:

<div class="product-top-icons">
  <div class="energy-rating-1"><img src="http://www.justhome.co/skin/frontend/default/just2011/images/assets/<?php echo $_product->getAttributeText('energy_rating_one');?>.jpg"></div>
  <div class="energy-rating-2"><img src="http://www.justhome.co/skin/frontend/default/just2011/images/assets/<?php echo $_product->getAttributeText('energy_rating_two');?>.jpg"></div>
  <div class="energy-rating-3"><img src="http://www.justhome.co/skin/frontend/default/just2011/images/assets/<?php echo $_product->getAttributeText('energy_rating_three');?>.jpg"></div>
  <div class="guarantee-icon"><img src="http://www.justhome.co/skin/frontend/default/just2011/images/assets/<?php echo $_product->getAttributeText('warranty');?>.jpg"></div>
</div>

I would like to add an if statement in there basically to say the following:

我想在那里添加一个 if 语句,基本上是说以下内容:

If the value in the 'energy_rating_one'attribute is null then don't display the division energy-rating-1, if the 'energy_rating_two'attribute is null then don't display the div energy-rating-2and so on...

如果'energy_rating_one'属性中的值为 null 则不显示除法energy-rating-1,如果'energy_rating_two'属性为 null 则不显示除法energy-rating-2等等...

采纳答案by Christophe

something like this:

像这样:

<?php if($_product->getAttributeText('energy_rating_one') !== null): ?>
<div class="energy-rating-1"><img src="http://www.justhome.co/skin/frontend/default/just2011/images/assets/<?php echo $_product->getAttributeText('energy_rating_one');?>.jpg"></div>
<?php endif; ?>

and that for all the others as well.

对所有其他人也是如此。

回答by check123

 <?php
   function echoIfExists($argument) {
      $val = $_product->getAttributeText($argument);
      if($val)
           /*your echo stmt*/
   }

echoIfExists('energy_rating_one');
 /** continue here*/

 ?>

回答by Cups

Make it easy on yourself. Just change the css class rules from energy-rating-1to energy-rating-oneand echo the variable you already have i.e energy-rating-one

让自己轻松一点。只需将 css 类规则从energy-rating-1to更改energy-rating-one并回显您已经拥有的变量即可energy-rating-one

回答by jeanmatthieud

You have a function "int_to_words" to transform "int" value into a text value in this post: http://www.php.net/manual/en/function.strval.php#41988

在这篇文章中,您有一个函数“int_to_words”将“int”值转换为文本值:http://www.php.net/manual/en/function.strval.php#41988

After that, you just have to iterate into all values

之后,您只需遍历所有值

for(i = 0; i < 100; i++) {
    $item = 'energy_rating_'.int_to_words($i);
    if($_product->getAttributeText($item) != null){
      echo "<div class=\"energy_rating_$i\"><img src=\"http://www.justhome.co/skin/frontend/default/just2011/images/assets/".$_product->getAttributeText($item).".jpg\"></div>";
    }
}

回答by izip

Look at short hand if statement:

看看简写的 if 语句:

http://www.scottklarr.com/topic/76/ternary-php-short-hand-ifelse-statement/

http://www.scottklarr.com/topic/76/ternary-php-short-hand-ifelse-statement/

$total==1 ? "is 1 item" : 
      "are ".($total == 0 ? "no items" : "$total items"