开发者

Php string cast vs strval function which should I use?

What is the difference between doing a string cast and strval in php ?

    开发者_运维技巧
  1. strval($value);

  2. (string)$value;


http://www.php.net/manual/en/language.types.string.php#language.types.string.casting

A value can be converted to a string using the (string) cast or the strval() function.

Looks the same to me.


They are generally interchangeable because PHP uses automatic type conversion and a variable's type is determined by the context in which the variable is used.

Some differences are that strval($var) will return the string value of $var while (string)$var is explicitly converting the "type" of $var during evaluation.

Also, from the manual for strval():

$var may be any scalar type or an object that implements the __toString method. You cannot use strval() on arrays or on objects that do not implement the __toString method.

As mentioned by @Lars (string) is generally faster.


One is a function call, the other one is an internal typecast. Without having checked, I'd guess the latter one is faster by few cycles, but shouldn't really make a difference.


I'm not sure how this applies to various versions of PHP, but when looking at the generated opcodes, both variants generate the same opcodes, and thus perform exactly the same.

Input:

<?php
function s1(int $val) {
    return (string)$val;
}
function s2(int $val) {
    return strval($val);
}

Output as generated by https://3v4l.org/3gFWj/vld#output

Function s1:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/3gFWj
function name:  s1
number of ops:  4
compiled vars:  !0 = $val
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   2     0  E >   RECV                                             !0      
   3     1        MAKE_REF                                         ~1      !0
         2      > RETURN                                                   ~1
   4     3*     > RETURN                                                   null

End of function s1

Function s2:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/3gFWj
function name:  s2
number of ops:  4
compiled vars:  !0 = $val
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   5     0  E >   RECV                                             !0      
   6     1        MAKE_REF                                         ~1      !0
         2      > RETURN                                                   ~1
   7     3*     > RETURN                                                   null

End of function s2


Late to the party, but according to PHPstorm (string) is faster

Php string cast vs strval function which should I use?


There is an advantage of using strval() over (string), Which is we can use strval as a callback function, Something we cannot do with (string).

$arr = [1, 2, 3];
echo(json_encode($arr)); // [1,2,3]
$arr = array_map('strval', $arr);
echo(json_encode($arr)); // ["1","2","3"]
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜