简体   繁体   English

如何用PHP表示一个很大的数字?

[英]How do I represent a very large number in PHP?

Hi I'm trying paginate a data without creating so many functions. 嗨,我正在尝试对数据进行分页而不创建太多功能。 I decided to merge the function (one loading some question and one for loading all the question) that takes all the data from my database. 我决定合并从数据库中获取所有数据的函数(一个加载一个问题,另一个加载所有问题)。

I have this function call for my pagination: 我有此函数用于分页:

$this->get_questions_incrementally(null, $id, $page_no, $items_per_page);

I usually do this when getting all the questions at once: 当我同时收到所有问题时,通常会这样做:

$this->get_questions_incrementally(null, $id, $page_no=0, $items_per_page=999999);

How do I represent $items_per_page to be infinity? 如何将$items_per_page表示$items_per_page无穷大?

Am I doing a very bad idea and is there actually a better solution than this? 我是在做一个非常糟糕的主意,实际上有比这更好的解决方案吗?

My recommendation would be to have a sentinel value. 我的建议是具有定点价值。 You don't really want to fetch an infinite number of rows. 您实际上并不想要获取无限数量的行。 You want ALL the rows. 您需要所有行。

If you have control over the method you're calling, a reasonable thing to do would be to accept $items_per_page = -1 as a sentinel value indicating that you want ALL the results. 如果您可以控制要调用的方法,那么合理的做法是接受$items_per_page = -1作为标记值,​​指示您想要所有结果。

Alternatively, if you don't have control over that code, then as @zerkms suggested, I'd use PHP_INT_MAX. 另外,如果您无法控制该代码,则按照@zerkms的建议,我将使用PHP_INT_MAX。

This could be implemented like so 可以像这样实现

function get_questions_incrementally($a, $id, $page_number, $items_per_page) {
    if ($items_per_page == -1) { 
        $LIMIT = "";
    } else { 
       $LIMIT = " LIMIT $items_per_page"; 
    }
    //blah blah query building
    //at this point $query is ready for attachment of a LIMIT clause
    $query = $query . $LIMIT;
    //run query
}

My preferred way to do this when I have multiple parameters (some of which could be optional) is to create an object with setter functions. 当我有多个参数(其中一些可能是可选参数)时,我更喜欢这样做的方法是使用setter函数创建一个对象。 That way you can just do: 这样,您可以执行以下操作:

$objectname->setId($id);    
$objectname->setAll(true);

In your object you'd could also implement variables such as $current_question or $current_page , and $questions_per_page , and have a function called get_next_question_set() . 在您的对象中,您还可以实现$current_question$current_page$questions_per_page变量,并具有一个名为get_next_question_set()的函数。

However, if you prefer to use functional programming for this and since PHP is loosely typed, you can also just do this: 但是,如果您更喜欢使用函数式编程,并且由于PHP是松散类型的,则也可以这样做:

$this->get_questions_incrementally(null, $id, $page_no=0, $items_per_page="ALL");

and then do a check to see if the value is a string equal to "ALL" or an integer. 然后检查该值是否为等于“ ALL”的字符串或整数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM