PHP if 语句返回相同的值


PHP if statement returns same value

当我比较这些变量时,我的代码总是返回true。我做错了什么?

<?php
    $postuser = (integer)bp_activity_user_id();  //echos 1int(0)
    $posteduser = (integer)bp_activity_comment_user_id();  //echos 3int(0)
    if ( $postuser === $posteduser) {
       echo 'true';
    } else {
       echo 'false'; 
    }
?>

您需要使用 RETURN 值的函数,而不是输出它。

从我找到的文档中找到了这是什么,

bp_activity_user_id() X-参考 输出活动用户 ID。

bp_get_activity_user_id() X-参考 返回活动用户 ID。

返回:int 活动用户 ID。

您正在使用的函数回显变量,NOT 返回它,因此您无法使用该函数设置变量。此函数相同。

bp_activity_comment_user_id() X-参考 输出当前显示的活动注释的作者的 ID。

bp_get_activity_comment_user_id() X-参考 返回当前显示的活动注释的作者的 ID。

return: int|bool $user_id 所显示作者的user_id

若要在赋值中使用,函数必须返回一个值。这就是为什么你的值总是 (int)0:你使用的函数没有返回值。因此,它返回 null,该空值被强制转换为 0。

<?php
  $postuser = bp_get_activity_user_id();  
  $posteduser = bp_get_activity_comment_user_id(); 
//no need to cast: these functions return integers

     if ( $postuser === $posteduser) {
        echo 'true';
        } else {
        echo 'False'; 

只需使用 intval 和 == 我认为它应该可以工作并评估它很好

<?php     
      $postuser = intval(bp_activity_user_id());  //echos 1int(0)
      $posteduser = intval(bp_activity_comment_user_id());  //echos 3int(0)
      if ( $postuser == $posteduser) {
          echo 'true';
       } else {
            echo 'False'; 
       }
?>

你的 prbolems 可能是错误的类型转换:

(integer)更改为(int)

$postuser = (int)bp_activity_user_id();  //echos 1int(0)
$posteduser = (int)bp_activity_comment_user_id();

http://php.net/manual/en/language.types.integer.php