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.