开发者

Why it's 0 but not 255 here?

echo intval(chr(255)开发者_JAVA技巧);

I don't understand...


The chr() function turns a byte into its ASCII equivalent and intval() function gets the integer value of a variable.

If we were to break the statement into two different lines, this would be:

$a = chr(255); // $a is now a string
echo intval($a);

If you check intval()'s documentation you will notice that:

Strings will most likely return 0 although this depends on the leftmost characters of the string. The common rules of integer casting apply.

That's why the result is zero.


The byte 0xFF does not represent a digit in either octal, decimal or hexadecimal what intval is looking for. You probably wanted the ord function.


To output 255, you need:

echo intval(ord(chr(255)));

There are 128 ordinal numbers in ASCII, the 255 comes out to be ÿ so when you convert it to a number with intval, it will be 0.


Because chr delivers a string, in this case with just one character, the character 0xFF, or better known as ÿ.

intval on the other hand does a conversion from a string to an integer based on the content of the string, and not the characters.

  echo intval("33");        // will print 33
  echo intval("10", 8);     // will print 8
  echo intval("0xFF", 16);  // will print 255

  echo intval("m"); // will print zero...
                    //you can't convert letters like that to numbers.


chr(255)

returns a character corresponding to ASCI 255

and intval try to bring out integer part from a variable

since chr(255) returns a non-numeric character so intval get no int value and return 0

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜