Richard Korts
asked on
Cannot understand error
Terribly embarrassed about this, it has to be obvious, but I cannot see it:
Here is code snippet:
$spok = false;
if (isset($_SESSION['uid'])) {
$qrys = "SELECT * from steps where uid = " . $_SESSION['uid'];
$ress = mysqli_query($link, $qrys);
$ns = mysqli_num_rows($ress);
if ($ns != 0) {
$stps = mysqli_fetch_array($ress,MYSQLI_ASSOC);
if ($stps['initial'] == "Y") {
$spok = true;
}
}
}
if (! isset($spok)) {
$spok = false;
}
echo "spok = " . $spok . "<br>";
The echo shows spok =
No value. How can it not be either 0 or 1?
Here is code snippet:
$spok = false;
if (isset($_SESSION['uid'])) {
$qrys = "SELECT * from steps where uid = " . $_SESSION['uid'];
$ress = mysqli_query($link, $qrys);
$ns = mysqli_num_rows($ress);
if ($ns != 0) {
$stps = mysqli_fetch_array($ress,MYSQLI_ASSOC);
if ($stps['initial'] == "Y") {
$spok = true;
}
}
}
if (! isset($spok)) {
$spok = false;
}
echo "spok = " . $spok . "<br>";
The echo shows spok =
No value. How can it not be either 0 or 1?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
True/False is PHP is not an interger.
https://www.php.net/manual/en/language.types.boolean.php
Try this code
If you need boolean value converted to integer, use type cast like
https://www.php.net/manual/en/language.types.boolean.php
Try this code
boby@sok-01:~$ cat 1.php
<?php
$spok = False;
echo "spok = " . $spok . "\n";
var_dump($spok);
boby@sok-01:~$ php 1.php
spok =
bool(false)
boby@sok-01:~$
If you need boolean value converted to integer, use type cast like
echo "spok = " . (int) $spok . "\n";
ASKER
I am totally confused.
First, I have hundreds of programs using true & false, lower case, all work fine.
You answers assume that true is equal to 1 and false is null (or blank).
That says my assumption of false being 0 is wrong, I am almost sure I have used that assumption many times, although I almost always test a variable for true or false with if($var) or if(!$var).
In this case I'm just using the echo as a debug to find something else out.
So, if I read you correctly.
true is 1 if printed.
false is null or blank
Thank you,
Richard
First, I have hundreds of programs using true & false, lower case, all work fine.
You answers assume that true is equal to 1 and false is null (or blank).
That says my assumption of false being 0 is wrong, I am almost sure I have used that assumption many times, although I almost always test a variable for true or false with if($var) or if(!$var).
In this case I'm just using the echo as a debug to find something else out.
So, if I read you correctly.
true is 1 if printed.
false is null or blank
Thank you,
Richard
The bottom line is when you try and output true/false via an echo, you will not have output. If you do a variable dump, you will.
For debugging, you could just do
For debugging, you could just do
if (! isset($spok)) {
echo "False";
} else {
echo "True";
}
Eg:
$spok = FALSE;