Some questions that are thought of by PHP if

Source: Internet
Author: User
Copy CodeThe code is as follows:


/* 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 coarse look of the code is impeccable, and our dear pony has found the problem. That is, the judgment in Javascript is always true.

Copy the Code code as follows:


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


Because this code is on the PHP side has a premise, is
if (!havecookie (' cookiename ')), it is displayed on the client. Then, when this condition is not met, the code will naturally not be thrown to the client. This may seem a little general, so let's put the Javascript code aside, and we'll simply use PHP code to express

Copy the Code code as follows:


Header ("Content-type:text/javascript");
if (!havecookie (' cookiename ')) {
if (!havecookie (' cookiename ')) {
Setcookie (' cookiename ');
}
}
?>


It's a lot clearer, and it's easy to spot the problem--we've made more judgments in a casual way, although this is what Javascript does on the client side.
Summing up, I think of some nonsense from this code:
The longer the code, the higher the efficiency will be.
Try to write multiple judgments together without affecting logic and processes
Try to judge the low-complexity function before
Too many judgments tend to reduce program efficiency, especially when using functions with high time complexity in judgment.
If you find that if the if is nested too much, you have to reconsider the process and the algorithm
Robust code is not guaranteed by excessive judgment.
When you simplify your code, you'll find a lot of problems that haven't been discovered yet.
Too much judgment another angle to understand is lack of confidence in the code
Finally, thank Comrade Xiao Ma again.

The above describes the PHP if think of some of the issues, including aspects of the content, I hope the PHP tutorial interested in a friend helpful.

  • 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.