如何获取已在 PHP 函数内声明的变量的值以及在外部 return 声明的变量的值

How to get the value of a variable already declared inside a PHP function and return it in the outside

function count($data) {
    $json = file_get_contents( 'http://api.facebook.com/restserver.php?method=links.getStats&format=json&urls=' . $data . '&pretty=1' );
    $json_data = json_decode($json, true);
    $count = $json_data[0]['total_count'];
    if(empty ($count) ) {
        return 'n/a';
    } else {
        if( $count < 1000 ) return $count;
        $x = round($count);
        $x_number_format = number_format($x);
        $x_array = explode(',', $x_number_format);
        $x_parts = array('k', 'm', 'b', 't');
        $x_count_parts = count($x_array) - 1;
        $x_display = $x;
        $x_display = $x_array[0] . ((int) $x_array[1][0] !== 0 ? '.' . $x_array[1][0] : '');
        $x_display .= $x_parts[$x_count_parts - 1];
        return  $x_display;
    }

}

我怎样才能在上面的函数之外得到 $count 的值?调用上面的函数肯定会 return $x_display 我也想在代码的其他地方回显简单的计数 $count

Return 它与 $x_display。通常这是作为一个数组完成的,您可以使用 list() 来获取每个片段:

function count($data) {
    $json = file_get_contents( 'http://api.facebook.com/restserver.php?method=links.getStats&format=json&urls=' . $data . '&pretty=1' );
    $json_data = json_decode($json, true);
    $count = $json_data[0]['total_count'];
    if(empty ($count) ) {
        return 'n/a';
    } else {
        if( $count < 1000 ) return $count;
        $x = round($count);
        $x_number_format = number_format($x);
        $x_array = explode(',', $x_number_format);
        $x_parts = array('k', 'm', 'b', 't');
        $x_count_parts = count($x_array) - 1;
        $x_display = $x;
        $x_display = $x_array[0] . ((int) $x_array[1][0] !== 0 ? '.' . $x_array[1][0] : '');
        $x_display .= $x_parts[$x_count_parts - 1];
        return  array($x_display, $count);
    }

}

list($x_display, $count) = count($data);

虽然返回两个值通常不是一个好的做法。将其分成两个函数可能是更好的方法:

function count($data) {
    $json = file_get_contents( 'http://api.facebook.com/restserver.php?method=links.getStats&format=json&urls=' . $data . '&pretty=1' );
    $json_data = json_decode($json, true);
    return $json_data[0]['total_count'];
}

function getDisplay($count) {
    if(empty ($count) ) {
        return 'n/a';
    } else {
        if( $count < 1000 ) return $count;
        $x = round($count);
        $x_number_format = number_format($x);
        $x_array = explode(',', $x_number_format);
        $x_parts = array('k', 'm', 'b', 't');
        $x_count_parts = count($x_array) - 1;
        $x_display = $x;
        $x_display = $x_array[0] . ((int) $x_array[1][0] !== 0 ? '.' . $x_array[1][0] : '');
        $x_display .= $x_parts[$x_count_parts - 1];
        return  $x_display;
    }
}

$count = count($data);
$x_display = getDisplay($count);

不要不要使用global有人不可避免地建议的关键字。这是一种 糟糕的 编程习惯,可能导致难以检测错误。

在我看来,这是 class 的完美用法。

class Classname {
  protected $count;

  function getXDisplay() {
    $json = file_get_contents( 'http://api.facebook.com/restserver.php?method=links.getStats&format=json&urls=' . $data . '&pretty=1' );
    $json_data = json_decode($json, true);
    $this->count = $count = $json_data[0]['total_count'];
    if(empty ($count) ) {
       return 'n/a'; // should be null ?
    } else {
       if( $count < 1000 ) return $count;
       $x = round($count);
       $x_number_format = number_format($x);
       $x_array = explode(',', $x_number_format);
       $x_parts = array('k', 'm', 'b', 't');
       $x_count_parts = count($x_array) - 1;
       $x_display = $x;
       $x_display = $x_array[0] . ((int) $x_array[1][0] !== 0 ? '.' . $x_array[1][0] : '');
       $x_display .= $x_parts[$x_count_parts - 1];
       return  $x_display;
    }
  }

  function getCount() {
    if (is_null($this->count)) $this->getXDisplay();
    return $this->count;
  }
}

不要使用混乱的 return 值,只需调用与 return 值相关的 class 方法。