函数php参数为0

I have a function like this

public function get_page($position) {

    switch ($position) {
        case "current":
            echo "current";
            return $this->current_page;
            break;
        case "first":
            echo "first";
            return $this->current_page >= 3 ? 1 : false;
            break;
        case "last":
            echo "last";
            return $this->current_page <= $this->total_pages() - 2 ? ceil($this->count / $this->nb_items_to_show) : false;
            break;
        case "previous":
            echo "previous";
            return $this->current_page - 1 >= 1 ? $this->current_page - 1 : false;
            break;
        case "next":
            echo "next";
            return $this->current_page + 1 <= $this->total_pages() ? $this->current_page + 1 : false;
            break;
        case is_int($position):
            echo "int";
            if ($position >= 1 && $position <= $this->total_pages()) {
                return $position;
            } else {
                return false;
            }
            break;
        default:
            echo "default";
            return false;
            break;
    }
}

When I call the function, it works for every parameters except 0 and goes in "current" case in the switch.

$paginator->get_page(0);

If I call the function with quotes "0", it works. But the function could be called like this

$paginator->get_page($paginator->get_page("current") - 2);

How can I call the function with parameter 0 ?

is_int returns TRUE or FALSE

case is_int($position): this is the same as case -1: and case 1: but not case 0:

In PHP, TRUE is considered any non-zero value

You're probably looking for case intval($position):

It's also worth noting that when comparing a string to 0, it will always return TRUE, except where the string starts with a numeric, such as 23balloons. This is how PHP works.

So you'll want ALL of your integer conditions to proceed your string conditions for the following reason:

When a string is evaluated in a numeric context, the resulting value and type are determined as follows.

If the string does not contain any of the characters ‘.’, ‘e’, or ‘E’ and the numeric value fits into integer type limits (as defined by PHP_INT_MAX), the string will be evaluated as an integer. In all other cases it will be evaluated as a float.

The value is given by the initial portion of the string. If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero). Valid numeric data is an optional sign, followed by one or more digits (optionally containing a decimal point), followed by an optional exponent. The exponent is an ‘e’ or ‘E’ followed by one or more digits.

More information can be found here.

Add before switch:

if (!is_string($position)) {
goto default;
}

change:

is_int // Find whether the type of a variable is integer

to:

is_numeric // Finds whether a variable is a number or a numeric string