7

I have a bit of php code that I'm not understanding why it is acting as it is. I have a variable called contactId that I want to test to see if it is empty. However even if it is empty it evaluates to true. Code is below. Thanks in advance.

print "*".$contactId."*<br/>";
if($contactId != '')
{
 //queryContact($contactId);
 print "Contact Present<br/>";
}

result returned to screen is:

**

Contact Present

greg0ire
23.3k17 gold badges76 silver badges104 bronze badges
asked Nov 7, 2011 at 22:08
1
  • I think it's because the variable is NULL if it's not defined Commented Nov 7, 2011 at 22:11

5 Answers 5

9

If you want to see exactly what your string is, simply use var_dump(), like this, for instance:

var_dump($contactId)

instead of

print "*".$contactId."*<br/>";
answered Nov 7, 2011 at 22:10
Sign up to request clarification or add additional context in comments.

6 Comments

The snippet works fine for me via php cli, would be interesting whats exactly inside the variable.
Thank you very much!!! var_dump showed me what I actually had in the variable. Turns out that I had an xml tag in there that wouldn't print because it was in <> but var_dump showed it. The above code works now!
Cool! Now I guess you won't use the "*" . $var . "*" method again ;)
How does this response answer the OP's question?
@Kingsolmn: Let's sum up the question : "I want to test to see if it is empty". How does my response not answer this question?
|
2

Couple of things you can try:

if (!empty($contactId)) {
 // I have a contact Id
}
// Or
if (strlen($contactId) > 0) {
 // I have a contact id
}

In my experience I have often used the latter of the two solutions because there have been instances where I would expect a variable to have the value of 0, which is valid in some contexts. For example, if I have a drink search site and want to indicate if an ingredient is non-alcoholic I would assign it a value of 0 (i.e. IngredientId = 7, Alcoholic = 0).

answered Nov 7, 2011 at 22:16

Comments

1

Do it with if (isset($contactId)) {}.

avpaderno
30k17 gold badges81 silver badges95 bronze badges
answered Nov 7, 2011 at 22:12

3 Comments

then it means $contacId isnt set yet. just set it before using
if you got '' <- empty string, then isset() return true!
@jcmitch : if this evaluates to false, $contactId is not set so you should get a notice telling you so. Make sure your php setting display_errors is set to 1, and that error_reporting is set to -1
0

You likely want:

if (strlen($contactId))

You'll want to learn the difference between '' and null, and between == and ===. See here: http://php.net/manual/en/language.operators.comparison.php

and here: https://www.php.net/manual/en/language.types.null.php

answered Nov 7, 2011 at 22:14

1 Comment

not really good, cuz if you haven't yet got any value into $contactId, - you'll get notice
0

In future, use if(!empty($str)) { echo "string is not empty"}.

avpaderno
30k17 gold badges81 silver badges95 bronze badges
answered Nov 7, 2011 at 22:15

2 Comments

OP, FYI, This will exclude the possibly legitimate $contactId="0";.
Yes. empty will return true for: "" (an empty string), 0 (0 as an integer), 0.0 (0 as a float), "0" (0 as a string), NULL, FALSE, array() (an empty array), var $var; (a variable declared, but without a value in a class), for your case - $contactId, if it is numeric, then just check it like is_numeric

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.