I have this code:

$cid = intval(substr($gid, 3, 6));

If $gid = "002000001" then it works fine and I get $cid=1. But if $gid="0020004BC" then the letters seem to confuse it and I get $cid=4.

How can I fix this? Is it something to do with intval?

Thanks,

Jon

    Since you are testing for an intval the letter will be ignored and you will see the integer value as defined by your code but if you want the string value you would use strval() Try the below code to see what I mean.

    <?php
    $gid ="0020004BC";
     $cid = intval(substr($gid, 3, 6));
     echo $cid."<br />";
     $cid =strval(substr($gid ,3, 6));
     echo $cid;
    ?> 

      Would I even need strval at all? I am not sure what the point of strval is in this instance.

      Also, another quick question:

      $sid = substr($gid, 1, 2);

      Does this mean it starts at the first letter in the string or the second?

        Well, what exactly are you trying to do?

        Does this mean it starts at the first letter in the string or the second?

        When in doubt, read the PHP Manual, in this case concerning [man]substr/man.

          Got my answer. it should have been 0,3 and not 1,2! Thank you.

            Write a Reply...