How to check if a param is true or false?
params come in as strings, so you need to compare against "true"
, not true
.
Check if variable is false and not either true or undefined
If the variable is declared then:
if (myvar === false) {
will work fine. ===
won't consider false
to be undefined.
If it is undefined
and undeclared then you should check its type before trying to use it (otherwise you will get a reference error).
if(typeof myvar === 'boolean' && myvar === false) {
That said, you should ensure that the variable is always declared if you plan to try to use it.
var myvar;
// ...
// some code that may or may not assign a value to myvar
// ...
if (myvar === false) {
Checking if a variable is either true or false
How about checking if it is not undefined?
- If it is neither true, nor false it has to be undefined.
- So if it is not undefined, it is set and therefore either true or false.
if (isValid !== undefined) {
// do something
}
Javascript. How to check if a false boolean parameter is passed in a function?
How do I check if the parameter isAlive
was passed, even if it was passed as false?
This is how I did it:
function checkThis(isAlive) {
if (isAlive !== undefined && !isAlive) {
console.log("isAlive is defined AND it is false");
}
}
PHP check if a variable is true or false
I think your $input['appointed_phases1']
contains a string value.
In PHP, any string value that is not empty evaluates to true.
See this question:
How to convert string to boolean php
You can cast the value of $input['appointed_phases1']
to a boolean, or you can compare this value with 'true'
instead of true
.
Bash - Simpler and more legible way to check if variable is true or false
The interpretation of a value depends on the context. So some facts:
When doing some logic:
0
means false,- nonzero means true.
When doing some action:
0
exit status means success,- nonzero exit status means failure.
The if
body executes when the command is successful. The if
body executes when command exits with zero exit status.
The exit status of (( ... ))
arithmetic expansion depends on the logical value of the last expression inside. A value of true means success and a value of false means failure (read that twice).
See man test and inspect STRING equivalent to -n STRING
part. Also research bash builtins, bash shell arithemtic, bash exit status.
Is there a simpler and more legible way to check if a variable is true or false?
In a controlled environment I just use the variable value and execute the shell builtins false
and true
(that return an exit status of nonzero (failure) and zero (success) respectively):
is_on=true
if "$is_on"; then
In not controlled environment it's best to compare the strings to be protected against strange user input:
is_on="$1"
if [[ "$is_on" = 'true' ]]; then
If you wish to handle complicated cases, then write your own logic for it. In bash all variable values are strings, there are no other variable types (well, there are arrays of strings):
shopt +extglob
user_boolean_value_to_my_boolean_convention() {
case "$1" in
# 0 and 0000000 are false
+(0)) echo false; ;;
# any nonzero number or string true is true
+([0-9])|T|t|[Tt][rR][uU][eE]) echo true; return; ;;
# all the rest is false
*) echo false; ;;
esac
}
is_on=$(user_boolean_value_to_my_boolean_convention "$1")
if "$is_on"; then
Check if variable is true, false, set null if nothing
You could use php isset()
function.
i.e. :
if (!isset($foo)) {
echo "foo is not set : ";
var_dump($foo);
echo "<br />";
}
// returns "foo is not set : NULL"
$foo = NULL;
if (!isset($foo)) {
echo "foo = NULL : ";
var_dump($foo);
echo "<br />";
}
// returns "foo = NULL : NULL"
$foo = false;
if (isset($foo)) {
echo "foo = false : ";
var_dump($foo);
echo "<br />";
}
// returns "foo = false : bool(false)"
$foo = true;
if (isset($foo)) {
echo "foo = true : ";
var_dump($foo);
}
// returns "foo = true : bool(true)"
Hope it helps.
How to check if type is Boolean
That's what typeof
is there for. The parentheses are optional since it is an operator.
if (typeof variable == "boolean") {
// variable is a boolean
}
Related Topics
Heroku: Running Imagemagick with Paperclip
How to Login a User with Devise
Passing Error Messages Through Flash
Ruby: How to Iterate Over an Array of Hashes and Return the Values in a Single String
How to Pass a Custom Comparator to "Sort"
Generate All Possibles Combinations of an Array with a Length Within a Given Range
Where to Put Common Code Found in Multiple Models
Ruby on Rails Gem for Google Map Integration
How to Remove All Non - Ascii Characters from a String in Ruby
How to Get All Class Names in a Namespace in Ruby
Is There an Add_Days in Ruby Datetime
How to Debug Http of Ruby Google-Api-Client
How to Fix a Deadlock Caused by Open