1

Here's a simple example:

echo 3==3; // echoes 1

echo 3==2; // should echo 0, yet echoes nothing

I noticed this as I was designing a form which entails a checkbox. When ticked, the checkbox passes a value of 1, when unticked, rather than passing a value of 0 as one would expect, it passes nothing; an undefined boolean so to speak.

I tried solving this with the following code:

$myBool = isset($_POST['myCheckbox']);

However, this doesn't seem to work either.

One solution to passing the checkbox value to $myBool would be to do some if-else conditioning, but I want a more concise solution. Is this possible?

vqdave
  • 2,361
  • 1
  • 18
  • 36
ryanvb92
  • 311
  • 2
  • 13
  • To answer the second part, https://stackoverflow.com/questions/2268887/how-do-i-see-which-checkbox-is-checked – Bradmage Dec 03 '17 at 02:58

4 Answers4

3

In PHP, every decision makes statement returns a boolean value, either true or false. So, echo 3==3; is a true statement, That's why it returns 1 and echo 3==2; is a false statement, that's why it returns nothing.

But if you want to echo out the associated integer of false (which is 0), then you can use typecasting. Like:

echo (int)(3 == 2);

It will print out 0.

vqdave
  • 2,361
  • 1
  • 18
  • 36
Robi
  • 158
  • 1
  • 9
1

The statement 3 == 2 evaluates to false, and as noted in the PHP documentation for echo under the examples section, running echo false won't print anything because it's not technically a function. However, you can use var_dump instead.

echo 3 == 2; // no output
echo false; // no output

var_dump(3 == 2); // outputs bool(false)
var_dump(false); // outputs bool(false)
vqdave
  • 2,361
  • 1
  • 18
  • 36
  • "running `echo false` won't print anything because it's not technically a function": that's not the reason, and that's not what the documentation you linked to says either. – Decent Dabbler Dec 03 '17 at 04:30
  • @DecentDabbler At the top of the echo documentation it says “echo is not actually a function (it is a language construct)” and later says in a comment under the examples section that “because echo does not behave like a function, the following code is invalid” – vqdave Dec 03 '17 at 13:20
  • But that example does not illustrate the same problem as OP's problem at all. That example is about using `echo` in a ternary expression ( `expression to evaluate ? if true expression : if false expression` ). It's a totally different issue. – Decent Dabbler Dec 03 '17 at 18:20
1

The actual reason for your question about why a statement that evaluates to echo false; prints an empty string, is given in the documentation on strings, in the section about casting to string:

Converting to string


A value can be converted to a string using the (string) cast or the strval() function. String conversion is automatically done in the scope of an expression where a string is needed. This happens when using the echo or print functions, or when a variable is compared to a string. The sections on Types and Type Juggling will make the following clearer. See also the settype() function.

A boolean TRUE value is converted to the string "1". Boolean FALSE is converted to "" (the empty string). This allows conversion back and forth between boolean and string values.


The reason that you can't test an unchecked checkbox, the way you tried, is that browsers don't send unchecked checkbox states on form submissions.

Typical solutions to this conundrum are:

  1. Keep track of all available checkboxes in the backend and compare them upon receiving a form submission.
  2. Add a hidden field in your HTML form before the actual checkbox, giving it the same name as the checkbox and a value that represents the unchecked state.

    <input type="hidden" name="checkbox1" value="0">
    <input type="checkbox" name="checkbox1" value="1">
    

    These hidden fields will be submitted upon form submission and will be overwritten by the checkbox value if it is checked1.


    1) The values will actually both be send if the checkbox is checked, but PHP will overwrite any earlier received value with a later received value if they have the same parameter name (unless the parameter name represents an array without explicit key names, like name="checkbox[]" instead of name="checkbox[1]").

Decent Dabbler
  • 22,532
  • 8
  • 74
  • 106
0

Test Code

// http://php.net/manual/en/language.operators.comparison.php

echo 3==3;

echo "<br>---------------<br>";

echo 3==2;

echo "<br>---------------<br>";

var_dump(3==3);

echo "<br>---------------<br>";

var_dump(3==2);

Output

1
---------------

---------------

boolean true


---------------

boolean false

The reason is for no output is you are trying to echo the boolean "false", which prints nothing.

Bradmage
  • 1,233
  • 1
  • 15
  • 41
  • You are basically reiterating OP's own observation without explaining *why* `echo`-ing a boolean `false` prints an empty string, instead of `0`. – Decent Dabbler Dec 03 '17 at 04:10
  • I assumed he thought it was incorrect behavour. With the var dumps you can see it is correctly assigned false, but prints nothing. It is the intended behaviour. – Bradmage Dec 03 '17 at 04:22
  • But your right, I should have also quoted documentation as others had done. – Bradmage Dec 03 '17 at 04:23
  • Okay, I see where you are coming from now. By the way, I don't necessarily think quoting documentation was necessary; I just thought OP could do with a little more explanation as to why `echo false;` behaves the way it does. – Decent Dabbler Dec 03 '17 at 04:34
  • Yeah I can see that. I'll store it in my brain for next time. – Bradmage Dec 03 '17 at 04:37