Some questions from the If thought in PHP

Source: Internet
Author: User

A problem was found when writing a script that was not complex. To start with the code, its main function is to use PHP to determine whether to generate a section of Javascript, and use cookies to record state

  

/* PHP Code * *

Header ("Content-type:text/javascript");

if (!havecookie (' cookiename ')) {

.. do something

?>

/* Javascript Code */

if (' undefined ' = = typeof document.cookie[' CookieName ']) {

Setcookie (' CookieName ', 3600);

}

.. do something with Javascript

  

}

?>

The thick-looking code is impeccable, and our dear pony has found the problem. Is that the judgment in Javascript is always true.

if (' undefined ' = = typeof document.cookie[' CookieName ']) {

// ...

}

Because this code is on the PHP side has a premise, is

if (!havecookie (' cookiename ')), it will be displayed on the client. Then, when this condition is not met, this code will naturally not be thrown to the client. This might seem a bit general, so if you put aside the Javascript code, we simply use the PHP code to describe

  

Header ("Content-type:text/javascript");

if (!havecookie (' cookiename ')) {

if (!havecookie (' cookiename ')) {

Setcookie (' cookiename ');

}

}

?>

This makes it a lot clearer, and it's easy to spot the problem-we inadvertently make one more judgment, although that's what Javascript does on the client.

In summary, I think of some nonsense from this code:

1. The longer the code, the higher the efficiency.

2. Write as many judgments as possible without affecting the logic and process

3. Try to put the function of low complexity before judgment

4. Too much judgment is easy to reduce the efficiency of the program, in the judgment of the use of high time complexity of the function should pay special attention to

5. If you find that if nesting too much, you have to reconsider the process and algorithm

6. Robust code is not guaranteed by excessive judgment.

7. After simplifying the code, you will find many problems that have not been discovered.

8. Too much judgment, another angle of understanding, is lack of confidence in the code




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.