What are some good PHP performance tips?

前端 未结 13 1762
挽巷
挽巷 2020-12-24 07:55

I\'ve heard of some performance tips for PHP such as using strtr() over str_replace() over preg_replace() depending on the situation.

13条回答
  •  攒了一身酷
    2020-12-24 08:15

    1. Use Native PHP Functions
    2. Use Single Quotes
      Using single quotes ( ‘ ‘ ) is faster than using double quotes( ” ” )
    3. Use = = =
      Use “= = =” instead of “= =”,
    4. Calculate Only Once Calculate and assign the value to the variable if that value is getting used numerous time rather than calculating it again and again where it is being used.

      For example, the following will degrade the performance.

      for( $i=0; i< count($arrA); $i++){
      
       echo count($arrA);
      }
      

      The script below will perform much better.

      $len = count($arrA);
      
      for( $i=0; i< $len; $i++){
      echo $len;
      

      }

    5. Used Switch Cases

    6. Use JSON

    Use JSON instead of XML while working with web services as there are native php function like json_encode( ) and json_decode( ) which are very fast. 7. Use isset Use isset( ) where ever possible instead of using count( ), strlen( ), sizeof( ) to check whether the value returned is greater than 0.

    1. Concatening variables is faster than just putting them in a double-quotation mark string.

提交回复
热议问题