Check If Variable Empty

check if variable empty

If you want to test whether a variable is really NULL, use the identity operator:

$user_id === NULL  // FALSE == NULL is true, FALSE === NULL is false
is_null($user_id)

If you want to check whether a variable is not set:

!isset($user_id)

Or if the variable is not empty, an empty string, zero, ..:

empty($user_id)

If you want to test whether a variable is not an empty string, ! will also be sufficient:

!$user_id

How to check whether a str(variable) is empty or not?

You could just compare your string to the empty string:

if variable != "":
etc.

But you can abbreviate that as follows:

if variable:
etc.

Explanation: An if actually works by computing a value for the logical expression you give it: True or False. If you simply use a variable name (or a literal string like "hello") instead of a logical test, the rule is: An empty string counts as False, all other strings count as True. Empty lists and the number zero also count as false, and most other things count as true.

how to check if all the variable not empty in PHP

Like this:

$array = [
'first_name' => 'John',
'last_name' => 'Doe',
'email' => '',
];

if(count($array) != count(array_filter($array))){
echo 'some rows are empty';
}

If you want to know which specific fields are empty you can use array_diff_key

$array = [
'first_name' => 'John',
'last_name' => 'Doe',
'email' => '',
];

$filled = array_filter($array);

if(count($array) != count($filled)){
$empty = array_diff_key($array, $filled);
echo 'field(s) '.implode(', ', array_keys($empty)).' are empty';
}

Ouputs:

field(s) email are empty

Obviously you can improve on the error string, or what have you. So in your case I would do something like this

if(count($array) != count($filled)){
$empty = array_keys(array_diff_key($array, $filled));
$last = '';
if(count($empty) > 1 ){
$last = ' and '.array_pop($empty);
}
$fields = implode(', ', $empty).$last;
echo json_encode(array('condition' => 'error','data' => 'Please fill in '.$fields));
}

So it should output something like this:

Please fill in email

Please fill in first_name and email

Please fill in first_name, last_name and email

You could even do:

$empty = array_map(function($item){
return ucwords(str_replace('_', ' ', $item));
}, $empty);

Which would take, first_name and change it to First Name if you want to get fancy.

So... Putting all that togather:

$array = [
'first_name' => '',
'last_name' => '',
'email' => '',
];

$filled = array_filter($array);

if(count($array) != count($filled)){
$empty = array_map(function($item){
return ucwords(str_replace('_', ' ', $item));
},array_keys(array_diff_key($array, $filled)));
$last = '';
if(count($empty) > 1 ){
$last = " and '".array_pop($empty)."'";
}
$fields = "'".implode("', '", $empty)."'".$last;
echo json_encode(['condition' => 'error','data' => 'Please fill in '.$fields]);
}

Outputs:

{"condition":"error","data":"Please fill in 'First Name', 'Last Name' and 'Email'"}

You can see it here live

And now it tells them exactly what fields they need, instead of some generic error.

fish shell. How to check if a variable is set/empty?

  • set -q var (note the missing "$" - this uses the variable name) can be used to check if a variable has been set.

  • set -q var[1] can be used to check whether the first element of a variable has been assigned (i.e. whether it is non-empty as a list).

  • test -n "$var" [fn0] (or [ -n "$var" ]) can be used to check whether a variable expands to a non-empty string (and test -z is the inverse - true if it is empty).

These will be true/false in slightly different circumstances.

When no set var has been performed at all (and it has not been inherited from the parent process), set -q var, set -q var[1] and test -n "$var" will be false, test -z "$var" will be true.

When something like set var has been done (without any additional arguments), set -q var will be true, set -q var[1] will be false.

When something like set var "" has been done, both set versions will be true.

When something like set var "somestring" (or even set var "" "" [fn1]) has been done, the sets will be true and test -z "$var" will be false.


[fn0]: You never want to use test (or [) without quoting the variable. One particularly egregious example is that test -n $var will return true both if the variable contains something and if it is list-empty/unset (no set at all or set var without arguments). This is because fish's test is one of the few parts that follow POSIX, and that demands that test with any one argument be true. Also it does not handle lists properly - test -n $var will have weird results if var has more than one element.

[fn1]: This is because a list will be expanded as a string by joining the elements with spaces, so the list consisting of two empty strings will expand to " " - one space. Since that isn't empty, test -z returns false.

How to find whether or not a variable is empty in Bash

In Bash at least the following command tests if $var is empty:

if [[ -z "$var" ]]; then
# $var is empty, do what you want
fi

The command man test is your friend.

Check if variable is the empty string

'' is an empty character. It does not mean “completely empty” – that is indeed NULL.

To test for it, just check for equality:

if (variable == '') …

However, the error you’re getting,

missing value where TRUE/FALSE needed

means that there’s a missing value in your variable, i.e. NA. if cannot deal with missing values. An NA occurs as a result of many computations which themselves contain an NA value. For instance, comparing NA to any value (even NA itself) again yields NA:

variable = NA
variable == NA
# [1] NA

Since if expects TRUE or FALSE, it cannot deal with NA. If there’s a chance that your values can be NA, you need to check for this explicitly:

if (is.na(variable) || variable == '') …

However, it’s normally a better idea to exclude NA values from your data from the get-go, so that they shouldn’t propagate into a situation like the above.

How to check if a variable is empty in python?

Yes, bool. It's not exactly the same -- '0' is True, but None, False, [], 0, 0.0, and "" are all False.

bool is used implicitly when you evaluate an object in a condition like an if or while statement, conditional expression, or with a boolean operator.

If you wanted to handle strings containing numbers as PHP does, you could do something like:

def empty(value):
try:
value = float(value)
except ValueError:
pass
return bool(value)

Vue v-if statement to check if variable is empty or null

If you want to show the <div> only when it is truthy (not empty/null/etc.), you can simply do:

<div v-if="archiveNote">

This gives the same result as the double bang:

<div v-if="!!archiveNote">

Both of these expressions evaluate all 8 of JavaScript's falsy values to false:

  • false
  • null
  • undefined
  • 0
  • -0
  • NaN
  • ''
  • 0n (BigInt)

and everything else to true. So if your variable evaluates to anything but these it will be truthy, and the v-if will show.

Here's a demo of these and some truthy examples:

new Vue({
el: "#app",
data() {
return {
falsy: {
'null': null,
'undefined': undefined,
'0': 0,
'-0': -0,
'\'\'': '',
'NaN': NaN,
'false': false,
'0n': 0n
},
truthy: {
'[]': [],
'{}': {},
'\'0\'': '0',
'1': 1,
'-1': -1,
'\' \'': ' ',
'\'false\'': 'false',
'5': 5
}
}
}
});
body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
}

#app {
background: #fff;
border-radius: 4px;
padding: 20px;
transition: all 0.2s;
}
#falsy, #truthy {
display: inline-block;
width: 49%;
}
.label {
display: inline-block;
width: 80px;
text-align: right;
}
code {
background: #dddddd;
margin: 0 3px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<div id="falsy">
Falsy:
<div v-for="(test, label) in falsy">
<div class="label">{{ label }}</div>
<code v-if="test">true</code>
<code v-else>false</code>
</div>
</div>

<div id="truthy">
Truthy examples:
<div v-for="(test, label) in truthy">
<div class="label">{{ label }}</div>
<code v-if="test">true</code>
<code v-else>false</code>
</div>
</div>
</div>


Related Topics



Leave a reply



Submit