Why isn't my function returning the string?

Steven

I have a function that first looks for premium brands to add to a string. If there is not enough premium brands, it continues and ads non-premium brands (max 4 brands).

I'm able to echo the string inside the function and it shows 4 brands, but I'm not able to return the string. Why is that?

$bnames = addBrandNameToTitle($model);
echo $bnames; // This is empty

function addBrandNameToTitle($model, $brandNames = array(), $ispremium = true){
    if($ispremium):
        foreach ($model->brands as $brand):
            if ($brand->isPremium() && count($brandNames)  < 4):
                array_push($brandNames, $brand->name);
            endif;

            if(count($brandNames)  >= 4){
                return implode(',', $brandNames);
            }
        endforeach;
        // If not enough premium, add non-premium brands
        addBrandNameToTitle($model, $brandNames, false);
    else:
        foreach ($model->brands as $brand):
            if (!$brand->isPremium() && count($brandNames) < 4):
                array_push($brandNames, $brand->name);
            endif;
        endforeach;
        $bnames = implode(',', $brandNames);
        echo $bnames; // <-- This lists 4 brands
        return $bnames; // <-- But this is not returning the string. Why?
    endif;
}
MrZebra

In this section of code, you forgot to return the value

// If not enough premium, add non-premium brands
return addBrandNameToTitle($model, $brandNames, false);  // Add a return

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why isn't my SQL query returning any results in hibernate?

Why isn't my function returning?

Why isn't my binary search returning the targeted value Index

Why isn't my code returning the original message?

Why isn't setInterval() returning my function's return value?

Javascript Functions in React: why isn't my function returning multiple divs?

Why is my pytest fixture with function scope returning an object that isn't resetting it's class variables in a new test?

My function isn't returning the right type, is it possible?

Why isn't Javascript async function immediately returning?

Why is my function returning an empty string?

Returning error message for my function if the argument isn't relevant

Why isn't my DeleteNode() function working?

Why isn't my reverse(); function working?

Why my function is returning empty string in python?

My recursive function isn't returning anything

Why isn't my append function working?

why the getline function isn't recognizing it as a string?

Why isn't ModRewrite returning my regular expression as expected?

Why isn't this anonymous function returning?

Why is my recursive JavaScript function not returning the string?

Why isn't my map() call returning the expected result?

Why Isn't This L-System Code Returning a String?

Why isn't this factorial function returning?

Why isn't my return function not working?

Why isn't my function recognized in the REPL

Why isn't my function returning the correct Key of the values?

Why isn't my output returning as expected?

Why isn't this function returning a value?

Why isn't this processing function returning anything related to the string?