php中临时变量的使用

临时变量的滥用会导致程序运行效率的降低。何时使用临时变量可基于以下两点考虑:
1、该变量是否至少使用两次。
2、该变量的使用是否会显著提高程序的可读性。
如果一条也不满足,则省略该变量的使用。例如:

<?php
$tmp = date (“F d, h:i a”); /* ie January 3, 2:30 pm */
print $tmp;
?>
就应该改成:
<?php
print date (“F d, h:i a”);
?>

又如:

<?php

// string reverse_characters(string str)
// Reverse all of the characters in a string.
function reverse_characters ($str)
{
return implode (“”, array_reverse (preg_split(“//”, $str)));
}

?>
的可读性不强,可改成:

<?php

// string reverse_characters(string str)
// Reverse all of the characters in a string.
function reverse_characters ($str)
{
$characters = preg_split (“//”, $str);
$characters = array_reverse ($characters);

return implode (“”, $characters);
}

?>


Leave Your Comment

Your email will not be published or shared. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="">

*
请输入图片中的字符以验证你并非垃圾机器人. 点击图片收听验证码的语音版.
点击这里收听此验证码的语音版本