Getting the first character of a string with $str[0]

前端 未结 9 1128
余生分开走
余生分开走 2020-11-28 21:04

I want to get the first letter of a string and I\'ve noticed that $str[0] works great. I am just not sure whether this is \'good practice\', as that notation is

9条回答
  •  萌比男神i
    2020-11-28 22:07

    It'll vary depending on resources, but you could run the script bellow and see for yourself ;)

    
    

    Some reference numbers (on an old CoreDuo machine)

    $ php 1.php 
    the average float microtime using "array[]" is 1.914701461792E-6
    the average float microtime using "substr()" is 2.2536706924438E-6
    the average float microtime using "array{}" is 1.821768283844E-6
    
    $ php 1.php 
    the average float microtime using "array[]" is 1.7251944541931E-6
    the average float microtime using "substr()" is 2.0931363105774E-6
    the average float microtime using "array{}" is 1.7225742340088E-6
    
    $ php 1.php 
    the average float microtime using "array[]" is 1.7293763160706E-6
    the average float microtime using "substr()" is 2.1037721633911E-6
    the average float microtime using "array{}" is 1.7249774932861E-6
    

    It seems that using the [] or {} operators is more or less the same.

提交回复
热议问题