如何获取已在PHP函数内声明的变量的值并将其返回到外部

时间:2022-06-20 00:06:07
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;
    }

}

How can I get the value of only $count outside of the above function ? calling the above function will definitely return $x_display and I want to also echo the simple count $count somewhere else in the code.

如何在上述函数之外获得$ count的值?调用上面的函数肯定会返回$ x_display,我想在代码中的其他地方回显简单计数$ count。

2 个解决方案

#1


Return it with $x_display. Typically this is done as an array which you can use list() to get each piece:

用$ 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);

Although returning two values is not typically a good practice. Breaking this up into two functions i sprobably the better way to do it:

虽然返回两个值通常不是一个好习惯。将其分解为两个函数我可能是更好的方法:

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);

Do not use the global keyword that someone is going to inevitably suggest. That's a bad programming practice and can lead to be difficult to detect bugs.

不要使用某人不可避免地建议的全局关键字。这是一个糟糕的编程习惯,可能导致难以发现错误。

#2


This is a perfect use for a class in my opinion.

在我看来,这是一个完美的课程用法。

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;
  }
}

Instead of having messy return values, just call the class method which correlates with the return value.

不要使用凌乱的返回值,只需调用与返回值相关的类方法即可。

#1


Return it with $x_display. Typically this is done as an array which you can use list() to get each piece:

用$ 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);

Although returning two values is not typically a good practice. Breaking this up into two functions i sprobably the better way to do it:

虽然返回两个值通常不是一个好习惯。将其分解为两个函数我可能是更好的方法:

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);

Do not use the global keyword that someone is going to inevitably suggest. That's a bad programming practice and can lead to be difficult to detect bugs.

不要使用某人不可避免地建议的全局关键字。这是一个糟糕的编程习惯,可能导致难以发现错误。

#2


This is a perfect use for a class in my opinion.

在我看来,这是一个完美的课程用法。

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;
  }
}

Instead of having messy return values, just call the class method which correlates with the return value.

不要使用凌乱的返回值,只需调用与返回值相关的类方法即可。