What are some of the jaw-dropping PHP code tricks you've seen?

Source: Internet
Author: User

Reply content:

Load Default Parameters

$params + = Array (
' P1 ' = xx,
' P2 ' = XX
)

   $handle = Popen("Tail-f/var/log/nginx/access.log 2>&1", ' R ');      while(!feof($handle)) {     $buffer = fgets($handle);     Echo "$buffer \ n";     Flush();}Pclose($handle);
A look has become a tutorial on how to optimize the PHP, I also say point.
1. Single quotation marks for performance optimization of strings
I don't think it's necessary. PHP will not be crushed by this small performance drain. And there are some other problems. Let me give you an example.
$STR = "You ' re";
$data = "I heard that {$str} getting married";
?>
I think it would be better to use double quotes at this time. Because of the problem with your string concatenation, PHP is again requested to open the memory space. At the same time can also avoid some wonderful problems. For example, the problem that we had before.
$STR = "You ' re";
$sql = "Update Article set content = ' {$str} ' where id = 1";
Echo $sql; Update article Set content = ' You ' re ' where id = 1
?>
For example, this is happening. will be particularly painful. This is a bitter lesson. I'm still in the pit.

2. Use PHP functions as much as possible, in other words, as much as possible to reduce the number of lines of code, such an advantage is to reduce the amount of code bugs.

3. Require_once and require actually consume about the same, the gap is really not big. Wait for your file to be loaded, load it over here, and then find that it accidentally loads a class and then causes a conflict. You'll know the benefits of require_once.

4. Try not to randomly copy variables because PHP needs to open up additional space, which can take some time. Although these times are few, I feel that this can be completely avoided.

5. Try to use semicolons instead of dot syntax when you echo a thing. My explanation is this, when using dot syntax, PHP needs a memory to open up more space to store this variable and then unify the output. Using commas, the echo behavior is used more than once. In that case. The variable is entered into the buffer only two times. Relatively speaking, the point syntax is a little bit faster .... But these are a little bit of performance loss.

6. The foreach before php7 is actually copying a copy out and then looping. This time I still recommend using Array_map (), Array_walk (), Array_filter () and other functions to handle your problem.

7. $dir = DirName (dirname (dirname (__file__)); This optimization method I wrote is: $dir = Realpath (__dir__. '/.. /.. /'); The last directory to be determined must exist, otherwise it will return false

8. Be sure to understand the difference between = = and = =

9. Do not use __autoload to replace with Spl_autoload_register. You can query the function using the method

10. Do not casually believe that the web of those PHP optimization 50 or something. Because there are some things that may be wrong. Do not pass it on to others without verifying correctness. The information on the internet is messy enough now. I hope you will spread the truth after you have confirmed it.

  
   曾经写过的代码,有类似经历的来站队
I saw this picture in the space just now. Well, I guess that's a sign.
@ Not jaw-dropping, just a little trick
  1. foreach is more efficient and uses foreach instead of while and for loops as much as possible.
  2. Inside the loop, do not declare variables, especially variables like objects.
  3. Do not use functions in loops
    for($x=0; $x < count($array); $x)//count()函数在外面先计算
    Tell you to find the way to Tongue X, to StackOverflow
    Highest voted ' php ' Questions
    There are many tongue I don't know, but I'm sure you can learn a lot and not to waste your life on this kind of problem. 1, AutoLoad
    Spl_autoload_register (function ($classname)
    {
    Require_once ("{$classname}.php")
    });
    2, PHP also has closures
    3, trait like an extractor (I used a few times)
    Trait Sayworld
    {
    Public Function SayHello ()
    {
    Echo ' world! ';
    }
    }
    Class Myhelloworld
    {
    Include the members in the Sayworld
    Use Sayworld;
    }
    $xxoo = new Myhelloworld ();
    The SayHello () function is from the Sayworld component.
    $xxoo->sayhello ();
    4, PHP also has the yield key word
    function Number10 ()
    {
    for ($i = 1; $i yield $i;
    }
    Returns the array 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    5, spaceship operator Orz. Php7
    Is the ternary logic that resembles the return value of Windows API.
    $a $b;
    6. Delimiter
    $name = ' Kitty ';
    Echo

    {$name}




    Eof;
    ?>
    (Note: Some of the content from the network) is probably the use of reflection play out of the pattern bar, but not recommended to write no detailed documentation of the reflection, later people will not understand
    php:variable variables
    Php:magic Methods
    Php:spl_autoload

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.