lundi 1 mars 2021

Operator "===" doesn't evaluate correctly when used in an object as it should php

I am trying to loop through an array assigned to an object. The function entity_products_check() must compare the submitted product to an array of pre-held values from a database. The foreach loop must evaluate to true in the provided test example. However, for reasons I cannot understand the === operator returns empty (i.e. null) and XAMPP evaluates it to false. I do not understand why this happens?

 $entity=array("products"=>array("machine", "lollipop"));
 class Borrowing_Cost
 {
    public array $entity;
    public array $item;
    public array $borrowing;
    public function __construct($entity, $item, $borrowing)
    {

        $this->entity = $entity;
        $this->item = $item;
        $this->borrowing = $borrowing;
    }

    public function entity_products_check($arg){
        $is_item = "";                

        **foreach ($this->entity["products"] as $value){           
            if($value === $arg){
                $is_item = "true";
            } else {
                $is_item = "false";
            }
        }**

        return $is_item;
    }
 }
 $borr = new Borrowing_Cost($entity, $item, $borrowing);
 echo $borr->entity_products_check("machine") . "<br>";

Aucun commentaire:

Enregistrer un commentaire