Even though I don't like checking true/false as strings when passing
them form JS to PHP, I still do it that way.
Why is it better to change the booleans to 0 and 1?



On Oct 24, 10:45 am, Sanford Whiteman <[email protected]>
wrote:
> > $cimg   = (isset($_REQUEST['cimg']) ? $_REQUEST['cimg'] : 'true');
>
> $_R  is not good practice. But that's prob'ly not the problem here, as
> long  as  you  are using either GET or POST consistently when testing.
>
> For  one thing, if the var is not passed, you are setting the value to
> the  string  'true', not boolean true. While the two will often end up
> being interchangeable thanks to automatic type coercion, if you really
> want booleans, use booleans from the start.
>
> define("INVALID_BOOL_DEFAULT",true);
> $cimg = isset($_GET['cimg'])
> ? ( $_GET['cimg'] !== "false" and $_GET['cimg'] != false or $_GET['cimg'] === 
> "" ) and ( $_GET['cimg'] === "true" or $_GET['cimg'] == (int)true or 
> INVALID_BOOL_DEFAULT )
> : INVALID_BOOL_DEFAULT;
>
> Set  INVALID_BOOL_DEFAULT  to  the  default  boolean value you want to
> assign if
>
> - no matching varname is passed
> - the var is passed but is empty
> - the var is set to any value other than 0, 1, "false", or "true"
>
> It  is  a  better  idea  to coerce the JS booleans to integers 0 and 1
> before sending them, but this approach allows you to pass JS bools (as
> strings,  of  course) and convert back into PHP bools on the server. I
> don't know what exact problem you were having, however.
>
> -- Sandy

Reply via email to