1

I have following IF in my code to check if array are empty,

if (!empty($data['id']) && (empty($data['student_no']) || empty($data['batch']) ) ) {
   print_r("inside if ");
}

$data['id'],$data['student_no'],$data['batch'] are arrays.

array values are like below,

1.$data['id'] -> Array ( [0] => 1 [1] => [2] => )

2.$data['student_no'] -> Array ( [0] => [1] => [2] => )

3.$data['batch'] -> Array ( [0] => )

But this does not got to inside the if and print the "inside if" string.

Please tell me what is wrong ?

6
  • 2
    But the student_no and batch are not empty. Commented Mar 28, 2017 at 10:19
  • they're not empty arrays. Commented Mar 28, 2017 at 10:19
  • if you try empty($data['batch'][0]) it should return true Commented Mar 28, 2017 at 10:20
  • Empty array is array with NO values. Array with one empty value is not empty. Commented Mar 28, 2017 at 10:20
  • 1
    change your condition to if (true). That's how you would get inside the if with these arrays. Commented Mar 28, 2017 at 10:21

3 Answers 3

2

You forgot ! in if

!empty($data['student_no']
Sign up to request clarification or add additional context in comments.

2 Comments

This is that rare case, when the answerer knows the task better than the OP*. (* actually not) @Nishant Nair how do you know if they have not forgotten something else? Like a condition to check whether it's a Tuesday today?
OP says: check if array are empty
0

$data['student_no'] and $data['batch'] are not empty but your condition wants one of them empty, so it does not work

Comments

0

Try wrapping another set of brackets in the !empty($data['id'])

if ((!empty($data['id'])) && (empty($data['student_no']) || empty($data['batch']) ) ) {
   print_r("inside if ");
}

1 Comment

More parentheses for the god of parentheses!

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.