0

I am retreiving a feild from a database, into $subcat. If $subcat is null, I want to show the form to add to a category, otherwise I want to display the category. $subcat definitly exists and is correctly retrieved. It is either null or a string.

if ($subcat != null) {
$showCategory = "<p><strong>Auction Category: </strong> ".$tmp['subcat'];
}
else 
{
$showCategory = "<form name=\"categoryForm\">
  <input name=\"radiobutton\" type=\"radio\" value=\"fakeapproved\" />Fake (Approved)<p>
  <input name=\"radiobutton\" type=\"radio\" value=\"fakesuspected\" />Fake (Suspected)<p>
  <input name=\"radiobutton\" type=\"radio\" value=\"keyword\" />Forbidden Keywords<p>
  <input name=\"radiobutton\" type=\"radio\" value=\"parallelimport\" />Parallel Imports
  <input name=\"Submit\" type=\"submit\" value=\"Update\" onclick=\"handleClick(".$pk.");return false\"/>
</form>";
}

However, all that is shown is the form. When $subcat is definitly, without a doubt not null, the form is still shown. I tried swapping the else clauses around, but then the category was displayed, even when $subcat was verifiably null.

1
  • Can you give a vardump of $subcat for the case that $subcat is supposed to be null? Commented Apr 20, 2009 at 10:36

2 Answers 2

2

is null works if the variable is actually NULL. If the variable is the empty string (""), zero, false, or an empty array, is_null evaluates to false. This is somewhat counter-intuitive, so I would always suggest using empty() instead.

empty() returns true for 0, empty string, empty array, boolean false, or NULL. So try:

if(!empty($subcat)){ do something; }

Only use is_null if you are 100% sure you ONLY want to do something if the variable is NULL. Empty is generally better to check whether you're about to call a class method on something unexpected.

Edit: Oh also, sometimes retrieving NULL from a database gives you the string "NULL" which is not the same as an actual NULL value. It depends on the db you're using and how you saved the value to the database in the first place. This is very easy to miss and can take forever to figure out. Try var_dump()'ing your subcat variable to see if it is "NULL" string (4) or literally a NULL value. This could account for unexpected behavior in your other code segment you mentioned in the comments.

Sign up to request clarification or add additional context in comments.

6 Comments

Hmm, $subcat returns string(0) "". I am using == null in another class however, and it works fine, with the same string returned. Odd. I switched to using empty as per your exmaple, however the problem still occurs, and the shorter $showCategory is always selected.
If your subcat is the empty string, empty will always evaluate it to true. It will only be false if there is some value in subcat (an actual subcategory). Have you tried testing with a row from the DB with a non-empty string as subcat? I have no experience using the == operator to evaluate the null keyword but I would not under any circumstance suggest it. There is good reason to believe the empty() and is_null() native functions exist explicitly for that reason.
I am testing on records that have subcat set to 'fake'. I have the category printed at the top of the page. It either shows "" for an empty subcategory, or the text of the subcategory. In both instances showCategory is set to the shorter html.
So if subcat is "" then empty should evaluate to true, meaning use the longer of the two options. If subcat is set to the string 'fake' then empty will evaluate to false, meaning use the shorter form with the category listed. in your above code, you use both $subcat and $tmp['subcat'] - could this be part of your problem? Try var_Dumping both or better yet use one variable instead of two. If your code is always executing the long version of the form, there is probably a logic error somewhere.
Ahh the error was tmp[$subcat], I had not declared it in my tmp array yet. Thank you very much!
|
0

In php you can check for a variable being null using is_null

So use:

if (!is_null($subcat)) {

1 Comment

I am using the same logic in another class and it works perfectly. Why would it not be working here?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.