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:
Hi,

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:

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

The following simple test shows that a value of zero is considered as empty:
PHP
$start = 0;
if (is_null($start)) print "null: " . $start;
if (empty($start)) print "empty: " . $start;
   
Comments
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"];

:)
   
Comments
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:

<html><head></head><body>
<?php
$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)";

?>
</body></html>

which you can try at https://www.lucpattyn.com/test1.php
and https://www.lucpattyn.com/test1.php?arg=0
and https://www.lucpattyn.com/test1.php?arg=1

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