0

I have an variable called $vars which contains an array that is submitted with form:

Array
(
    [name] => Sample Name
    [email] => test@test.com
    [job_role] => 
    [telephone_number] => telephone number
    [comment] => comment
)

How can I check if job_role index is containing a value?


I tried:

if (in_array('', $vars)){
  echo "Job role can't be empty";
} else {
  echo "Job role submitted";
}

But this works on the whole array and not only the job_role index.

Also, I tried:

if (isset($vars['job_role'])){
  echo "match found";
} else {
  echo "nothing there";
}

But it was returning "match found" even when I didn't submit anything...

Why is isset() always returning true?

Greg
  • 503
  • 5
  • 30
  • ehemmm... why downvote? – Greg Apr 17 '18 at 12:30
  • The answer to the question "can anyone help" is "yes", but it is probably not the question you actually want to ask. Questions here benefit from being focussed and specific. – halfer Apr 17 '18 at 13:00
  • Questions that ask "please help me" tend to be looking for highly localized guidance, or in some cases, ongoing or private assistance, which is not suited to our Q&A format. It is also rather vague, and is better replaced with a more specific question. Please read [Why is “Can someone help me?” not an actual question?](//meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question). – halfer Apr 17 '18 at 13:00
  • Can you tell me why the `isset` method in my code didn't work?... I found few question on here, and they all used it but it didn't work for me that's why I posted the question... I see people really didn't like it... By "can anyone help" I meant, why is my code not doing what I expected it to do... Sorry if I didn't word it properly. – Greg Apr 17 '18 at 13:01
  • 2
    @BareFeet `isset()` checks if the value defined (is set), not it is empty. In your code `$vars['job_role']` is probably an empty string, so, it's defined, but it's empty. – Syscall Apr 17 '18 at 13:10
  • 1
    @halfer I'm sorry for making you an effort to edit my question again. The reason why I asked "Can anyone help" was just because I wanted to be polite. After reading the meta post, I understand why I should not do it. I will keep this in mind. Thank you. Also, I reworded my question to make it more clear what I was asking. I hope someone will take it positively now :) – Greg Apr 18 '18 at 08:56
  • Looks good to me, let us know if the duplicate link doesn't answer it. Yes, we prefer a technical style of writing here, where possible. – halfer Apr 18 '18 at 10:54
  • It partly answer my question. It explained how to check if the item in array is empty, however, it didn't explain why `isset()` was returning true. I now know, that it was returning an empty string which was considered as value even tho it was empty. I got an answer here and not in the question marked as duplicate. Something silly that could be easily mist by a beginner. Anyway, I got my answer! Thank you for your interest and few lessons ;) – Greg Apr 18 '18 at 10:58

1 Answers1

3

You can use empty() to check if the entry is not empty:

if (empty($vars['job_role'])) {
    echo "Job role can't be empty";
} else {
    echo "Job role submitted";
}

Because isset() checks if the value defined (is set), not it is empty. In your code $vars['job_role'] is probably an empty string, so, it's defined, but it's empty.

Syscall
  • 19,327
  • 10
  • 37
  • 52