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.