Question

I'm currently building a website and I come across the case where I can do:

if (myString)

if (myArray),

and it seems to "return" true whenever there is data inside the variable. At least, that's what I think.

E.g.

$testVar = "test";
if ($testVar)
    echo $testVar;
else
    echo "Empty";

When i assert $testVar = "", then it echos "Empty".

I'm wondering if this is a defined feature of PHP, that any type will return true if it is not null or empty, as in other languages you need to do if($testVar = "") or so on.

Additionally, if this does indeed return true on all types if the variable is not empty, and I also want to check if the variable exists, would:

if (isset($testVar) && $testVar) be okay to use (in terms of practices)

I have searched for questions but can't find an answer to this exact question. To summarize:
Can any type return a bool, provided that it is not empty?

Thanks.

Was it helpful?

Solution

These types do not return true, but they are, instead, cast to true. PHP is a weak typed language, so it will automatically try to convert a variable to the correct type when required. In most instances, this means that a non-empty variable will return true.

This resource here will give you more information. Take a look at the "Converting to boolean" section, specifically.

http://www.php.net/manual/en/language.types.boolean.php

OTHER TIPS

You can not check if a string is empty that way. Consider this:

$test = "0";
if ($test)
    echo $test;
else
    echo "Empty";

The code above prints "Empty", because "0" is a falsy value. See Booleans, section "Converting to boolean".

So the answer is: All types can be converted to booleans, but the result might not be what you want.

The variables don't "return" a bool, but any variable can evaluate to either true or false.

Best practice is to be strict on your comparisons and not just do if($var)

For detailed comparison information, see: http://us3.php.net/manual/en/types.comparisons.php

conditional always return bool if you need to check any empty values you need to use equalsto operator for check values

if($testVar == "") // check is $testVar is empty or not return bool-true/false

best to use functions like empty()

if(empty($testVar))

In this example when you are testing string variables:

$testVar = "test";
if ($testVar)
    echo $testVar;
else
    echo "Empty";

What you are asserting is if variable $testVar is set (not if it's empty or not). The variable themselves don't have a return type, but it's in the context they are used in a control flow operator such as an if statement.

i.e. if ($testVar) is same as if (isset($testVar)) (when $testVar is a string)

However, there are other cases like this:

$testVar = "0";
if ($testVar)
    echo $testVar;
else
    echo "Empty";

In this case, you will get "Empty" because $testVar is evaluated as a int 0.

However; if you had this:

$testVar = " 0"; // notice the space in front of 0
if ($testVar)
    echo $testVar;
else
    echo "Empty";

This will echo back the $testVar because the variable is both set and has a string value.


When you want to check for empty string, you have several options:

  • if (empty($testVar))
  • if (strlen($testVar)) or if (strlen($testVar) > 0) (both same)

etc...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top