Question

I have the following snippet in a page running under PHP 5.4.10 (notice the === in comparison).

$list_all_pages = False;
$reqs_per_page = 50;
$start_page = 0;

if (isset($_GET["p"])) {
    echo("Debug: " . $_GET["p"] . "\n");
    if ($_GET["p"] === "all") {
        $list_all_pages = True;
    } else {
        $start_page = intval($_GET["p"]);
        if ($start_page < 1)
            $start_page = 0;
    }
}

The parameter "p" intends to be the number of the results page that will be displayed (starting from zero) or "all" to display all pages. For reasons I don't understand, PHP automatically converts my parameter to an integer and, as result, I can never display all pages I wish.

For example, calling the page with "mypage.php?p=all" causes the debug echo to print "Debug: 0"

What's happening? I think this kind of automatic conversion a bit dangerous... does anybody got this problem before?

Was it helpful?

Solution

The $_GET variable p (ie. ?p=) is reserved in WordPress, as the default way to server up post IDs. While most people like to transform their URLs, the default is www.domain.com/?p=123.

Wordpress specifically looks for this parameter (it is also served as the wp_shortlink), and if it is not numeric is will automatically set the value to 0.

OTHER TIPS

this is enough ,no need to type cast

if ($_GET["p"] == "all") {
        $list_all_pages = True;
    } else {
        $start_page = (int)$_GET["p"];
        if ($start_page < 1)
            $start_page = 0;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top