What PHP code is good _php tutorial

Source: Internet
Author: User
Tags echo command naming convention what php
Good PHP code should be structured. Large sections of code should be divided into functions, methods, and those small pieces of code should be annotated, in order to understand their use later. And should be as far as possible to the foreground code such as HTML, CSS, JavaScript and other from the program to separate out. The object-oriented programming features of PHP can help programmers to organize the code in order well.

Good PHP code should be normalized. Whether you are setting a naming convention for variable names and function names, or standardizing on some reusable processes such as database operations and error handling, or simply to rule out how code is indented, these normalization can make your code more readable.

Good PHP code should be self-adapting. PHP has many features, such as magic quotes and short tags, which can be turned on and off to affect the operation of the program. Therefore, a good programmer should add the appropriate statements in his code to enable the program to adapt to the environment.

Good PHP code should be secure. Although PHP is an efficient, flexible language that has no fixed framework, it leaves security issues to programmers. A deep understanding of potential security vulnerabilities, such as cross-site scripting attacks (XSS), cross-site request forgery (CSRF), code injection vulnerabilities, character encoding loops, and so on, is critical for today's professional programmers.

When the candidate answers these questions, I can clearly know whether to hire him or not. Of course, sometimes programmers don't do a good job of explaining the problem, and then we'll have them do some PHP testing. Many of the problems in testing appear to be very simple on the surface, but it also gives candidates a chance to show themselves, because if you look carefully, you can find the problem.

Here is a small piece of "bad" PHP code is a simplified test problem. This question is like asking: How do you optimize this code?

 
  Search results for query: ". $_get[' query '].".

");? >

The main problem with this code is that it displays user-submitted data directly onto the Web page, resulting in an XSS vulnerability. There are many ways to fill this loophole. So what is the code we want?

 
  Search results for query: ". Htmlspecialchars ($_get[' query ']).".

");? >

This is the minimum requirement. The XSS vulnerability was filled with the Htmlspecialchars function, thereby shielding the illegal characters.

 
  Search results for query: ',     htmlspecialchars ($_get[' query '], ent_quotes), '.

'; }

The person who can write this code should be the person I want to hire.

    • Determine whether it is empty before outputting the value of $_get[' query '].
    • The extra parentheses in the echo command are removed.
    • Strings are delimited by single quotation marks, which saves PHP time to search for replaceable variables from a string.
    • Use commas instead of periods to save echo time.
    • The ent_quotes identity is passed to the Htmlspecialchars function, which guarantees that the single quotation marks are also escaped. Although this is the most important, but also a good habit.

http://www.bkjia.com/PHPjc/752482.html www.bkjia.com true http://www.bkjia.com/PHPjc/752482.html techarticle Good PHP code should be structured. Large segments of the code should be divided into a function, method, and those small pieces of code should be annotated, so that later clear ...

  • Related Article

    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.