Home  >  Article  >  Backend Development  >  Some questions that come to mind from php if_PHP Tutorial

Some questions that come to mind from php if_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:53:331000browse

Copy code The 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
}
?>

At first glance, the code seems to be flawless, but our dear Xiao Ma still discovered the problem. That is, the judgment in Javascript is always true

Copy code The code is as follows:

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

Because this code has a premise on the PHP side, which 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. It seems a bit general to say this, so let’s put aside the Javascript code first, let’s simply use the PHP code to describe it

Copy the code The code is as follows:

header("Content-type: text/javascript");
if (!haveCookie('cookieName')) {
if (!haveCookie('cookieName' )) {
setCookie('cookieName');
}
}
?>

This will make it much clearer and it will be easy to find the problem The reason -- we inadvertently made one more judgment, although this was executed by Javascript on the client side.

To summarize, here are some of the nonsense I thought of from this code:

The longer the code, the higher the efficiency is not necessarily higher
Without affecting the logic and process, try to make it as long as possible Write multiple judgments together
Try to put low-complexity functions before judgment
Too many judgments can easily cause the program efficiency to decrease. Pay special attention when using high time complexity functions in judgment
If If you find that if is nested too much, you have to reconsider the process and algorithm
Robust code is not guaranteed by excessive judgment
After simplifying the code, you will find many undiscovered problems
Pass Too many judgments, understood from another perspective, is a lack of confidence in the code
Finally, thank you again Comrade Xiao Ma.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/318665.htmlTechArticleCopy the code as follows: ?php /*PHPcode*/ header("Content-type:text/javascript") ; if(!haveCookie('cookieName')){ //...dosomething ? /*Javascriptcode*/ if('undefined'==typeofdocum...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn