0

In the following code, $counter variable is typecast to store integers as per this line $counter=(int)fread($fp, 20); then why strlen($counter) is used. I mean, strlen returns length of a string however $counter now is already an integer variable. Same goes for substr($counter, $i,1). The program gives the desired result, it's just that I do understand as explained above.



$counter_file = "./count.txt";
$img_dir="./img";
if(!($fp=fopen($counter_file, "r"))) die ("Could not find $counter_file");
$counter=(int)fread($fp, 20);
fclose($fp);

$counter++;

for ($i=0; $i<strlen($counter); $i++){

 $img_src = $img_dir."/".substr($counter, $i,1).".jpg";
 $img_tag_str .="<img src= \"$img_src\" border=\"0\">";

 }
echo "You are visitor no. $img_tag_str.";

$fp=fopen($counter_file, "w");
fwrite($fp, $counter);
fclose($fp);

Regrds, seni.

1
  • Where is strlen? I don't see it in your code. Looks like something got cut off around your for loop Commented Nov 18, 2010 at 16:23

3 Answers 3

2

If you pass an integer to as strlen method, this integer is cast to a string.

So strlen(23) will give 2; strlen(1234) will give 4.

Why may you need this? To know the number of digits in a number, for example. Even if it's probably not an optimal way to do it.

That's what happens in your sample code. The $counter variable is loaded from a file. A cast to an integer ensures that this is a real number, and not some random stuff¹. Then, a loop is used to go through every digit, and to display an image with this digit. That explains why we need to strlen and substr the integer.


¹ Why random stuff is bad here? Imagine the data in the file is "Abc", when you expect a number. The loop will output something like:

<img src="img/A.jpg" alt="" />
<img src="img/b.jpg" alt="" />
<img src="img/c.jpg" alt="" />

Since you don't have A.jpg, b.jpg or c.jpg, nothing will display.

Sign up to request clarification or add additional context in comments.

4 Comments

+1 - One of the beauties of PHP is that everything is essentially an array of characters, so you can try and use any type for this kind of thing and it will attempt to make it happen. You can also use $counter[2] to return 3 from the number 1234. That's PHP!
@Sohnee: agree. Probably the author of the sample code was not aware of that.
In my opinion, the for loop is completely unnecessary. It iterates through the digits of the number and thus resets the variables $img_src and $img_tag_str x times. The script outputs only the last digit of the number as an image. For example, for the visitor 1234 the 4 would be outputed. A loop isn't necessary for this. I would do it like this: echo 'You are visitor no. <img src="'.$img_dir.'/'.$counter[strlen($counter) -1].'.jpg" border="0" />';
If only the range 1 - 9 is used then I would also check if after the increment $counter > 9 and then reset it to 1. Therefore I won't have to use strlen because I then can ensure that $counter is a digit with a length of 1.
1

$counter is cast to an int so that $counter++ will do a simple integer increment. Functions like strlen($counter) implicitly cast the $counter value to a string before returning the length

2 Comments

Ok that was very good. Would u also explain it in context of substr function in the code...
substr($counter) is the same as strlen($counter) -- and most other functions that expect a string argument -- the value of $counter is implicitly cast to a string
1

Even though $counter is an integer, certain functions that require a string can treat it as one. If $counter = 125, then strlen($counter) sees strlen('125') and gives you the correct result of 3.

The function will cast the value passed to is as a string, but the original is not changed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.