-1

我试图在 php 中从 Facebook 获取我的电影的某个数字之后突破一个 foreach 循环。

我认为这是愚蠢的小事。这是我得到的:

  try {
// Proceed knowing you have a logged in user who's authenticated.
$user_movie_info = $facebook->api('/me/movies?fields=name,genre,release_date,picture,likes');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }

//print_r($user_movie_info);

$numer_of_movies_allowed = 2;
$movie_counter = 0;

foreach($user_movie_info['data'] as $movie) {

    if ($movie_counter === $number_of_movies_allowed ) {
        break;
    }

    echo '<img src="' .$movie['picture']['data']['url']. '">'; 
    echo $movie['name'];  
    echo $movie['release_date']; 
    echo $movie['genre']; 
    echo $movie['likes']; 
    echo "</br> </br>";
    $movie_counter ++;  
}

print ($movie_counter);

print ($numer_of_movies_allowed);

它不会在两部电影之后爆发,而是将它们全部展示出来。

如果我使用:

if ($movie_counter == $number_of_movies_allowed )

或者

if ((int)$movie_counter === (int)$number_of_movies_allowed )

根本没有电影放映。

4

4 回答 4

1
$numer_of_movies_allowed = 2;
$movie_counter = 0;

foreach($user_movie_info['data'] as $movie) {

    if ($movie_counter === $number_of_movies_allowed ) {

如果您在初始化时编写正确的变量名可能会有所帮助……$numer_of_movies_allowed$number_of_movies_allowed.

如果你已经将你的 error_reporting 设置为一个合理的开发水平,那么整个问题将是完全没有必要的(因为 PHP 会告诉你 if 条件中的变量没有设置)——现在就做

于 2012-11-20T16:17:58.250 回答
1
if ($movie_counter === $number_of_movies_allowed )

应该:

if ($movie_counter == $number_of_movies_allowed )
于 2012-11-20T15:20:55.833 回答
1

=== 要求两种数据类型相等。您必须将 $movie_counter 解析为 int 或删除一个等号。

尝试

if ($movie_counter == $number_of_movies_allowed ) 

或者

if ((int) $movie_counter === (int)$number_of_movies_allowed ) 
于 2012-11-20T15:21:39.527 回答
0

这是我找到的一张表:

// "===" means that they are identical  
// "==" means that they are equal  
// "!=" means that they aren't equal.

         false   null    array()  0      "0"     0x0     "0x0"   "000"    "0000"
false    ===     ==      ==       ==      ==     ==      !=      !=       !=    
null     ==      ===     ==       ==      !=     ==      !=      !=       !=    
array()  ==      ==      ===      !=      !=     !=      !=      !=       !=    
0        ==      ==      !=       ===     ==     ===     ==      ==       ==    
"0"      ==      !=      !=       ==      ===    ==      ==      ==       ==    
0x0      ==      ==      !=       ===     ==     ===     ==      ==       ==    
"0x0"    !=      !=      !=       ==      ==     ==      ===     ==       ==    
"000"    !=      !=      !=       ==      ==     ==      ==      ===      ==    
"0000"   !=      !=      !=       ==      ==     ==      ==      ==       ===
于 2012-11-20T15:31:20.517 回答