What is Recursive in PHP?

Why there is some function is recursive, what is the role of recursive.

For example like in COUNT_RECURSIVE,
When I call the function without recursive, I get only two variables but with recursive 8, that mean recursive count everything inside the arrays?

f

ood = [
    'fruits' =>[
        'orange',
        'banana',
        'apple'
    ],
    'vegetables' => [
        'carrot',
        'onion',
        'potato'
    ]
];

echo count($food, COUNT_RECURSIVE).'<br/>';
echo count($food);

You’ve got it in one! From the manual

If the optional mode parameter is set to COUNT_RECURSIVE (or 1), count() will recursively count the array. This is particularly useful for counting all the elements of a multidimensional array.

Yes, but my question is recursive means to run the code in depth mode to check all the values inside the arrays?

A recursive function is a function that calls itself.Sometimes the easiest way to model a problem is to make a function call itself a technique known as recursive function calling.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.