Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's how I'd have written it:</p> <pre><code>if(isset($_COOKIE['id'])) { $cookid = $_COOKIE['id']; } else { $cookid = isset($_GET["id"]) ? $_GET["id"] : "115"; setcookie("id", $cookid, time() + 31536000); } </code></pre> <p>First of all, according to some conventions you should test for the positive case (<code>isset(...)</code>), not the negative (<code>!isset()</code>). I think it makes for more readable code, so I've switched it around.</p> <p>Secondly, I've used the <a href="http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary" rel="nofollow">ternary operator</a> (<code>condition ? expr1 : expr2</code>) to eliminate an if/else block, which is acceptable because you were just using the if/else block to decide which of two values to assign to the variable. The ternary operator should be used with caution, though, because if overused it can make code less readable.</p> <p>Thirdly, it's my preference to use curly braces even for one-line if/else blocks, but at the very least I think you should use curly braces for the <code>else</code> if you use them for the <code>if</code>.</p> <p>And lastly, just a readability note: Try to be consistent with your whitespace. On line four you don't have any spaces around the <code>=</code>, but on lines six and ten you have a space after it, but not before. For readability it's almost always preferable to have whitespace on both sides of an operator.</p> <p>Oh, and <code>$_GET["id"]</code> is correct; <code>$_GET[id]</code> is not. It (correctly) throws a warning if you have your error reporting level high enough (and you should usually develop with <code>error_reporting(E_ALL);</code> so you see them).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload