1

So based on the documentation, if I put a string inside intval() it should return an integer of said string. However, when I feed it a variable with a string value of a number, it is spitting out '0' instead of the appropriate number.

This code is what I've whipped up to attempt to convert the page number of the image in the archive section of my site into the title.

<?php
    $filename = basename($_SERVER['REQUEST_URI']);
    function countDigits($str) {
        $numDigits=0;
        for ($i=0; $i < strlen($str); $i++) {
            if (is_numeric($str[$i])) {
                $numDigits++;
            }
        }
        return $numDigits;
    }
    $digits = countDigits($filename);
    $thisPageNum = intval(substr($filename, -$digits, $digits), 10);
    if ($thisPageNum = 0) {
        echo '<title>Unfetched Page #</title>';
    }
    else {
        echo '<title>Archive: Page ' . $thisPageNum . '</title>';
    }
?>

So, attempting to find the problem revealed that intval is returning 0 instead of the appropriate number. For clarification the page name that is being grabbed for this test ends up being: archive.php?p=2 and the digits (1) are properly counted and the substring (2) is correctly fetched. Furthermore, the if statement at the end that checks if $thisPageNum = 0 isn't firing and giving the error message. It's always giving the Else echo (Returning Archive: Page 0).

So I suppose I have three questions here. Am I misunderstanding how intval() works entirely? Is it not a way of doing str2int? Is there a different way of achieving the str2int result I'm requiring for this?

Disclaimer: I've been messing with php for about 24 hours. If you start slipping in a bunch of crazy things like gets please do explain what they are doing as I may not understand and I do not like to use code I do not understand. Thanks, :)

EDIT: I fixed the silly little errors I made by typing this out here. Those two errors were not actually present.

Ipurgepeople
  • 27
  • 1
  • 5
  • possible duplicate of [PHP intval() weird results](http://stackoverflow.com/questions/13776867/php-intval-weird-results) – m59 Dec 02 '13 at 03:31
  • @m59 I read through that but that didn't help me figure out what was going on. – Ipurgepeople Dec 02 '13 at 04:51
  • BTW: The last `if()` is always TRUE, because it contains an assignment, not a comparison! Better use the [Yoda Notation](https://en.wikipedia.org/wiki/Yoda_Conditions). In PHP you have to use `"=="` for a simple value-comparison and `"==="` for a value- AND type-safe comparison. – tron5 Jan 17 '18 at 10:52

2 Answers2

1

You have several bugs in your code:

function countDigits($str) {
    $numDigits=0;
    for ($i=0; $i < strlen($str); $i++) {
        if (is_numeric($str[i])) {//<- should be $i, not i
            $numDigits++;
        }
    }
    return $numDigits;
}

second bug:

$digits = countDigits();//function called without any inputs

I corrected your code and ran it with sample data:

<?php
function countDigits($str) {
    $numDigits=0;
    for ($i=0; $i < strlen($str); $i++) {
        if (is_numeric($str[$i])) {
            $numDigits++;
        }
    }
    return $numDigits;
}

$str = "archive.php?p=2";
$digits = countDigits($str);
$thisPageNum = intval(substr($str, -$digits, $digits), 10);
print $thisPageNum;

Output:

2
user4035
  • 22,508
  • 11
  • 59
  • 94
  • The two bugs are because I wrote this comment out manually, my apologies. I didn't want to copy it from the ssh. Those two errors are not actually present on the page! Furthermore, I checked and it does seem to work when I input the filename directly, but NOT when I use $filename. Is the call I'm using to get the file messing it up somehow? – Ipurgepeople Dec 02 '13 at 04:52
  • So I have learned that everything here can easily be done with `$_GET`. However, I am very curious as to why feeding the intval my variable which contains the string value of `'2'` was not providing the 2 result whereas feeding it a raw `2` was. Any ideas? – Ipurgepeople Dec 02 '13 at 05:14
  • @user3055758 That's really very weird. You must do some debugging. Try to print $filename and see it's value. – user4035 Dec 02 '13 at 09:42
  • I did plenty of debugging. The value of $filename was "archive.php?p=2" like I mentioned. The digits were being counted correctly (1 in this case), and the substring was correctly grabbing just the number. When all that was done the variable$thisPageNum holds a string of 2 before the intval is run. And then the intval is returning 0 instead of an integer of 2. – Ipurgepeople Dec 02 '13 at 14:33
  • @user3055758 Unfortunately, can't tell you anything. It works with manually input string and doesn't work with $_SERVER['REQUEST_URI']. There must be some difference between them, that we overlooked. – user4035 Dec 02 '13 at 15:09
1

I think you're missing the simpler way in general. So I take it you're calling archive.php?p=2 and trying to parse out the value of p?

So, welcome to PHP, and the much easier way of doing this, $_GET.

$thisPageNum = (int)$_GET['p'];

So to explain this, $_GET is a global array that contains the values of all your query string references. (int) is how you typecast in PHP. So $thisPageNum will ALWAYS contain an integer. This should work much better for what you want.

Machavity
  • 30,841
  • 27
  • 92
  • 100
  • Well, I need to get the info about the page first (hence the Request_URI. How would I snag the query int from the variable in this way? – Ipurgepeople Dec 02 '13 at 04:56
  • Never mind! I didn't even HAVE to feed it the variable. Very neat. I think I need to look more into the superglobals. I can't vote you cause I have no rep, but I gave you the answer for giving me the easier method of doing this! – Ipurgepeople Dec 02 '13 at 05:13