0

I want to compare an array variable and Normal variable using if condition in PHP.. I have an Array VAriable as .$AllowedEnquiryType[$i] and a variabe as $TIntType how to check these to are equal.

My coding is

for($i=1;$i<=$length;$i++)
    {
        if($AllowedEnquiryType[$i]==$TIntType) 
        {   
             return true;
        }
        else
        {

            return false;
        }
}
2
  • Dont see any problem too. Maybe, that it just compares only the first element? Thats because you return from the method/function/file right at the first iteration. Also usually arrays begin with 0 (not 1) and the last element is $length -1 and not $length. Commented Apr 7, 2011 at 10:47
  • How an array could be equal to a scalar ? Commented Apr 7, 2011 at 11:35

3 Answers 3

2

Well, if you want to check if $AllowedEnquiryType[$i] and $TIntType are equal, you already wrote what to do :

if ($AllowedEnquiryType[$i] == $TIntType) {
    // those are equal
}


Else, if you want to check if all items of $AllowedEnquiryType are equal to $TIntType, you could do something like this :

$allEqual = true;
for($i=1;$i<=$length;$i++) {
    if ($AllowedEnquiryType[$i] != $TIntType) {
        $allEqual = false;
        break;  // no need to check the other items
    }
}

if ($allEqual) {
    // all items in $AllowedEnquiryType are equal to $TIntType
}
Sign up to request clarification or add additional context in comments.

1 Comment

Perhaps add a little break; inside the if-statement... Just to make sure we bail out of the loop if further processing is not required any more.
0

use in_array

link

Comments

0

Yes, you can use if($AllowedEnquiryType[$i]==$TIntType). What exactly are you asking for?

Comments

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.