Click here to Skip to main content
15,398,439 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:

How can I set a value to false in PHP?

is_adoption is my value and it can be either true, false or null

I have no problem settings it to true usin adoption=1 in my URL

but when I set it to adoption=0 i expect it to be false but it's ending up with null in my database

What I have tried:

Here is what I am trying:

if (!is_null($_GET["adoption"]) && !empty($_GET["adoption"])) $adoption = $_GET["adoption"]; else $adoption = NULL;
Updated 12-Oct-19 1:43am

The following simple test shows that a value of zero is considered as empty:
$start = 0;
if (is_null($start)) print "null: " . $start;
if (empty($start)) print "empty: " . $start;
Jassim Rahma 12-Oct-19 17:20pm
then what is the solution if I want empty is empty, zero is zero and one is one?
Richard MacCutchan 13-Oct-19 2:52am
There is no "solution", you need to redesign your code to take account of what a zero value indicates.
$adoption = NULL;
if (isset($_GET["adoption"])) $adoption = $_GET["adoption"];

Jassim Rahma 12-Oct-19 17:20pm
Your solutions has the same problem
Luc Pattyn 12-Oct-19 17:37pm
My approach is correct.

I have the following page:

$arg = NULL;
if (isset($_GET["arg"])) $arg = $_GET["arg"];
if ($arg===null) echo "arg is absent";
else echo "arg=$arg";
echo "<br>";
if ($arg) echo "arg is non-zero (this will show only for non-null non-zero)";


which you can try at

It does what it must do. My guess is you are performing bad comparisons on your variable. Read the PHP doc on how null, numbers (zero and non-zero) and other data are converted into true/false!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900