Ludovic Renevey
Ludovic Renevey

Reputation: 11

PHP or others languages - understanding of isset() - when or not using it

I have a little problem with the comprehension of isset(). In this example which comes from the book "Beginning PHP 5.3 - Wrox", I do not understand why the use of isset() does not work. Instead, the right way is simple to write:

if ($var1 or $var2) { // then your code}

and not: if ( isset($var1) or isset($var2) ) { // then your code}


If you need more details, please find the code below with which I have this small problem. Thank you for your help.

<?php

if (isset($_POST["submit"]) ) { storeInfos();}
elseif (isset($_GET["forget"]) ) { forgetInfos();}
else { display();}

function storeInfos() {

  if ( isset($_POST["firstname"]) ) { setcookie( "firstname", $_POST["firstname"], time() + 60 * 60 * 24 * 365, "", "", false, true ); }

  if ( isset($_POST["city"]) )  { setcookie( "city", $_POST["city"], time() + 60 * 60 * 24 * 365, "", "", false, true ); }

header( "Location: december6.php" );
};

function forgetInfos () {

if (isset($_COOKIE["firstname"]) ) { setcookie( "firstname", $_POST["firstname"], time() - 36000, "", "", false, true ); }

if (isset($_COOKIE["city"]) ) { setcookie( "city", $_POST["city"], time() - 36000, "", "", false, true ); }

header( "Location: december6.php" ); };

function display() {

$firstname = (isset($_COOKIE["firstname"]) ) ? $_COOKIE["firstname"] : "";
$city = (isset($_COOKIE["city"]) ) ? $_COOKIE["city"] : "";    }?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title>PHP example</title>
</head>

<body>
<h1>Welcome on my new website!</h1>

<?php 

if ( $firstname or $city ) // This line is my problem {

?> <p>Hello <?php echo $firstname ? $firstname : "Visitor" ?> of the town of <?php echo $city ? $city : "Unknown" ?> </p> 

<a href="december6.php?forget=forget">I would like to delete my personal information.</a>

<?php ; }

else { ?>
<form action="december6.php" method="post">

<label for="firstname">Votre prénom: </label> 
<input type="text" name="firstname" id="firstname" value="ex: Peter"/></br>
<label for="city">Votre domicile: </label> <input type="text" name="city" id="city" value="ex: Fribourg"/></br>
<input type="submit" name="submit" id="sumbit" value="Envoyer"/></br>

</form> <?php ; }
} ?>

</body> </html>

Upvotes: 0

Views: 183

Answers (2)

D&#225;vid Horv&#225;th
D&#225;vid Horv&#225;th

Reputation: 4320

The code in an if branch will be evaluated, when the result of the condition expression is not "falsy". Falsy values are: false, 0, "", "0", null, array() (etc.). In a logical expression every base value will be evaluated to false if the value is falsy, and true if it is not falsy: result of "0" && array() is false, because "0" and array() are falsy:

isset() returns with true with falsy values also, except null. "Is set" means: the variable has no value (null), or it is not declared at all. Please, read the manual.

$a = "0";
$b = array();

if ($a && $b) {
    // will be skipped
}

if (isset($a) && isset($b)) {
    // will be executed
}

This is not true: isset($x)===((bool)($x))

Upvotes: 3

Lajos Arpad
Lajos Arpad

Reputation: 76464

isset — Determine if a variable is set and is not NULL

If you want to check the value of something, then you do that without isset. If you want to check whether something exists, you need isset. For instance, let's suppose you have an instance of a class called foo and you want to refer to its bar method:

$foo->bar();

but it will yield an error if $foo does not exist, so you can check whether $foo exists before you call bar:

if (isset($foo)) {
    $foo->bar();
}

Upvotes: 0

Related Questions