Fixing some methods of my list implementation
Posted: Mon Apr 05, 2010 3:51 pm
Hello,
I've recently implemented a class which represents a list. Internally, the list is an array.
Most of its methods work correctly. Only two are making me trouble.
The first method is the contains method. It checks if the list contains the element given to it through a parameter.
It returns true, if the list contains the element.
Here is the method:
The size method works correctly. $this->list is the array which represents the list.
I use this on a site where I type in the element in an input field and then post it.
Then I call following piece of code:
However, this always executed the else part so far, even when I inputted an element which is in the list. Only when I left the input field empty the if part was executed.
Any idea on what is wrong?
I've recently implemented a class which represents a list. Internally, the list is an array.
Most of its methods work correctly. Only two are making me trouble.
The first method is the contains method. It checks if the list contains the element given to it through a parameter.
It returns true, if the list contains the element.
Here is the method:
Code: Select all
public function contains($element){
for($i=0;$i<$this->size();$i++){
if($element == $this->list[i]){
return true;
}
}
}I use this on a site where I type in the element in an input field and then post it.
Then I call following piece of code:
Code: Select all
if($l->contains($_POST['Element'])){
echo 'Element is in the list';
}else{
echo 'Element is not in the list';
}Any idea on what is wrong?