i am wondering when should i use 'isset', for example if(isset($_COOKIE['ID']) ... if($_COOKIE['ID']) ... will give me the same result so why and when should i put isset? same thing about having a NULL or not. both returns false in a 'if' statement and both are equal to "" but the difference is when i put 'isset', then the empty cell returns true while the NULL returns false. how does it help me?! and again, where should i leave an empty cell and where do i put a NULL my last question is about die(); i put it in every query - "query... or die();" i understand why but are there other cases other than queries that can fail and i should put die? ty.
if ($_COOKIE['x']) Does work, but it leaves an E_NOTICE if the variable is not defined. That means each time you do this, and the variable is not defined, PHP will open the error log, and write to it, and close it, and throw an error. Therefore it's a bad practice to use this method. isset() will avoid that if the variable is not set. You can see these E_NOTICES if you turn on full error reporting. You can do that by placing this line at the top of your code: error_reporting(E_ALL); PHP: isset() returns false if a variable has the value NULL. $foo = null; if (isset($foo)) { echo 'Is set'; } else { echo 'Is not set'; } PHP: The above would output "ist not set", even though the variable is defined. If you want to check for null values, use is_null() instead. Read this for more info: www.php.net/isset EDIT: When programming, you should always put the error reporting to E_ALL, and make sure no errors are shown, under any circumstances. That's the best practice.
There's a difference isset means a variable is defined or not while ($_cookie['x']) will check its null or not
let me see if i got this isset thing straight. i had a code which looked like this $level = $_GET["level"]; if ($level != "") { ....... } else {......} PHP: now i had to change it so i won't get any errors and it looks like this if (isset($_GET["level"])) {$level = $_GET["level"];} if (isset($level)) {....} else {....} PHP:
I'd do it this way: if (!empty($_GET['level'])) // empty() does not throw errors either: www.php.net/empty { .... } PHP: There's no need to assign the $_GET['xxx'] variables to a local variable. I would only do that if you need the variable multiple times, and you're too lazy to type out $_GET... A second way would be: if (!isset($_GET['level'])) { $_GET['level'] = null; // Give it a default value. } if ($_GET['level']) { ... } PHP: And a third way: $level = isset($_GET['level']) ? $_GET['level'] : null; if ($level) { ... } PHP: If the variable has been defined with the variable null, you can skip isset(), and use the method above. It won't trigger errors.
"I would only do that if you need the variable multiple times" that's exactly the reason, and i like when it looks simple without all this $_GET... (just noticed i didn't have to check isset twice but nvm) thanks for the detailed explanation.