我想要获取字符串的第一个字母,我注意到 $str[0]
这种写法很好用。我不确定这是否是'良好实践',因为这种表示通常与数组一起使用。这个特性似乎没有很好的文档说明,所以我来问问你们,能否在所有方面都使用这种方法?
还是说我应该坚持使用经典的 substr($str, 0, 1)
?
另外我也注意到花括号 ($str{0}
) 同样适用。这是怎么回事呢?
可以。字符串可以看作字符数组,访问数组的位置方式是使用[]
运算符。通常情况下,使用$str[0]
(我非常确定比substr()
方法快)没有问题。
这两种方法只有一个警告:它们会获取第一个 字节 而不是第一个字符。如果你使用多字节编码(如UTF-8),这一点很重要。如果你想支持它,请使用mb_substr()
。可以说,现在应该总是假定输入是多字节的,所以这是最好的选择,但它会稍微慢一些。
mb_substr($str, 0, 1, 'utf-8')
来避免截取多字节字符串。 - Vicsubstr($str, 0, 1)
更短、更易记,但它容易让读代码的人感到困惑。 - trante从PHP 5.3.0开始,{}语法已弃用,建议使用方括号。
$_POST['type'][0]
或 substr($_POST['type'], 0, 1)
,您将得到 C
。type
修改为 type[]
,然后将 'Control' 和 'Test' 作为此数组的数据发送,$_POST['type'][0]
现在将返回 Control
而不是 C
,而 substr($_POST['type'], 0, 1)
将会失败。$str[0]
可能存在问题,但这取决于周围的情况。if (true === is_string($_POST['type']))
- Will B.我唯一的疑虑是这种技术在多字节字符串上的适用性,但如果这不是一个问题,那么我认为你已经做得很好了。(如果有疑问,mb_substr()
似乎是一个显而易见的安全选择。)
但是,从宏观的角度来看,我要想知道你需要多经常访问字符串中的第n个字符才会成为一个关键考虑因素。
如果使用多字节(Unicode)字符串,使用str[0]
可能会出现问题。 mb_substr()
是更好的解决方案。例如:
$first_char = mb_substr($title, 0, 1);
这里有一些细节:获取UTF-8字符串的第一个字符
$str = 'abcdef';
echo $str[0]; // a
根据资源不同,情况会有所不同,但您可以运行下面的脚本并自己查看;)
<?php
$tests = 100000;
for ($i = 0; $i < $tests; $i++)
{
$string = md5(rand());
$position = rand(0, 31);
$start1 = microtime(true);
$char1 = $string[$position];
$end1 = microtime(true);
$time1[$i] = $end1 - $start1;
$start2 = microtime(true);
$char2 = substr($string, $position, 1);
$end2 = microtime(true);
$time2[$i] = $end2 - $start2;
$start3 = microtime(true);
$char3 = $string{$position};
$end3 = microtime(true);
$time3[$i] = $end3 - $start3;
}
$avg1 = array_sum($time1) / $tests;
echo 'the average float microtime using "array[]" is '. $avg1 . PHP_EOL;
$avg2 = array_sum($time2) / $tests;
echo 'the average float microtime using "substr()" is '. $avg2 . PHP_EOL;
$avg3 = array_sum($time3) / $tests;
echo 'the average float microtime using "array{}" is '. $avg3 . PHP_EOL;
?>
一些参考数字(在旧的CoreDuo机器上)
$ 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
看起来使用[]
或{}
运算符基本上是一样的。
testA
和testB
意味着您能够检测到例如testB
是一个破坏缓存的事实,而testA
是缓存友好的。当它们都在同一个循环中时,它们被测量为具有完全相同的时间,因为testB
污染了testA
的缓存。 - PypeBros作为一个普通人,我会选择使用$str[0]
。在我看来,一眼就能理解$str[0]
的含义比理解substr($str, 0, 1)
更快。这可能归结为个人喜好。
至于性能,建议进行性能分析。 :) 或者你可以查看PHP源代码...
我之前也使用过这种表示法,没有任何不良副作用和误解。这很有道理——毕竟一个字符串只是字符数组而已。
$str[42]
。为此目的,请将字符串视为字符数组。】(http://php.net/manual/en/language.types.string.php#language.types.string.substr)在内部,PHP字符串是字节数组。 - gattsbr$arr[] = $new_element
)不能用于字符串。因此,我认为将字符串视为字符数组是没有意义的。 - Mark Amery