4

I am trying to figure out how strlen() finds the length of a number. When I try the following:

echo strlen(00000000000000000000000000000000);

it outputs 1 but when I try

echo strlen(11111111111111111111111111111111111111111111111111111111);

it outputs 19. Sometimes it does work properly like in this example:

echo strlen(19);

strlen() outputs 2. I know I can use the following:

echo strlen('00000000000000000000000000000000');

and it will output 32 but I want to figure out how it works with numbers.

Henders
  • 1,195
  • 1
  • 21
  • 27
Qeaxe
  • 81
  • 8

2 Answers2

3

Because strlen() gives the STRING LENGTH.. 00000000000000000000000000 is not a string until it is written in quotes like "00000000000000000000000000" or '00000000000000000000000000'..

Then why strlen(00000000000000000000000000000000) gives output 1 and strlen(11111111111111111111111111111111111111111111111111111111) gives 19?

Because integer 00000000000000000000000000000000 = 0.. this means the length is 1. Whereas 11111111111111111111111111111111111111111111111111111111 is not equal to 1, but 11111111111111111111111111111111111111111111111111111111. Hence it outputs 32.

For more you may check the documentation of strlen()

Kashan Shah
  • 113
  • 1
  • 8
  • 3
    it should be noted that the bunch of 1s has a length of 19 because it's too large to fit in an int, so it's evaluated as float and then cast to the string "1.1111111111111E+71". – Franz Gleichmann Feb 14 '17 at 07:32
3

Try echo 11111111111111111111 It will echo the 1.1111111111111E+19 which is 19 characters that's why the length is 19 however if you tried echo 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 it will ouput 1.1111111111111E+100 if you tried getting the length of it it will be 20 characters

aqw alhadary
  • 184
  • 1
  • 10