...

View Full Version : easy question - checking for empty vars



Coastal Web
01-15-2010, 06:46 AM
Hi guys... I have a question is:

if(trim($someVar) == '')

the same as:

if(empty($someVar))

Usually l check for empty vars with:

if($someVar != '' && !empty($someVar)){
//do something
}

But l was just wonderingif there's a more standard way of doing this.

oesxyl
01-15-2010, 07:53 AM
Hi guys... I have a question is:

if(trim($someVar) == '')

the same as:

if(empty($someVar))

Usually l check for empty vars with:

if($someVar != '' && !empty($someVar)){
//do something
}

But l was just wonderingif there's a more standard way of doing this.
standard is to not check, :)
I don't think it is something standard because depend of what type is $someVar.
In your example you assume there is a string, this is the usual case, but could be a number, array, object or else depend of coding and context.
Since you know what type would be the best things is to check if exists first, what type is or expect next, to avoid previous errors, and then to check acording to that type.

http://www.php.net/manual/en/ref.var.php

is_xxx, intval, isset, empty are few I use.

best regards

Coastal Web
01-15-2010, 08:02 AM
standard is to not check, :)
I don't think it is something standard because depend of what type is $someVar.
In your example you assume there is a string, this is the usual case, but could be a number, array, object or else depend of coding and context.
Since you know what type would be the best things is to check if exists first, what type is or expect next, to avoid previous errors, and then to check acording to that type.

http://www.php.net/manual/en/ref.var.php

is_xxx, intval, isset, empty are few I use.

best regards

Hi oesxyl,

Yes l'm talking specifically about strings...
So what is the best method of checking for empty strings?

Len Whistler
01-15-2010, 08:09 AM
This is the code snippet I use to check strings. In the example it has to contain 10 characters or more.


if (strlen($comments) > 10) {
// do this
}

oesxyl
01-15-2010, 08:53 AM
if(empty(trim($someVar))){
// ...
}

this include the case when $someVar is filled with white spaces, without trim in rest but usualy is better to use trim, :).

or using strlen like Len Whistler


if(strlen(trim($someVar)) !== 0){
// ...
}

best regards

JAY6390
01-15-2010, 01:50 PM
If you know for sure the variable is always being set then use the empty method that oesxyl has above. It's rare that you will get a value in a variable that you don't know what it is by the time it's assigned to the variable so programming carfully will mean that this won't happen in the first case. I like to do the following if I have to

if(isset($var) && !empty(trim($var))) {
... code here for non empty string
}else{
... empty string code here
}
That checks that the var has been assigned a value to begin with too

Coastal Web
01-15-2010, 06:07 PM
You can't empty(trim($var))... PHP won't let you call trim() inside of the empty() function, so it would have to be: trim($var) == ''

JAY6390
01-15-2010, 06:12 PM
Good point. I can't say I've ever seen that before...shows how often I use the empty function ;)

oesxyl
01-15-2010, 06:46 PM
You can't empty(trim($var))... PHP won't let you call trim() inside of the empty() function, so it would have to be: trim($var) == ''
yes, it's my fault, trim is a variable function and is a note to empty about this. That's why is better to test, :)

best regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum