0

我试图让 if 语句回显内联样式。但是我只能让我的条件在任何 1 个数字上返回 true,而不是数组,|| 分开等...

你能解释一下如何制作|| version 和一个数字数组在我的条件下返回 true。

// Works.
$counts = 0 ;
foreach ($posts as $post) : setup_postdata($post); $counts++; 
 if ($counts == 8) {echo 'counts equals your numbers';}
endforeach; 

// This does nothing.
$numbers = array(4,8);
$counts = 0 ;
foreach ($posts as $post) : setup_postdata($post); $counts++; 
 if ($counts == $numbers) {echo 'counts equals your numbers';}
endforeach; 

// Returns true for every iteration.
$counts = 0 ;
foreach ($posts as $post) : setup_postdata($post); $counts++; 
 if ($counts == 4 || 8) {echo 'counts equals your numbers';}
endforeach; 
4

2 回答 2

1

的语法||是:

if ($counts == 4 || $counts == 8) {echo 'counts equals your numbers';}
于 2013-08-22T02:26:02.733 回答
0

使用 PHP 的内置in_array函数:

if ( in_array($counts, $numbers) ) {echo 'counts equals your numbers';}
于 2013-08-22T02:26:09.123 回答