Hello,

I'm trying to create a script that counts the number of people in an array by using a variable called "counter"
I am using functions to display the information, however it won't display..

<?php

function main()
{
	$people = list_people();
	
	echo 'There are currently: ' . $counter . 'Number of people\n\n';
	foreach($people as $person)
	{
		echo $person . '\n\n';
		
	}

}

function list_people()
{
	global $counter;
	
	$people = array ('Phillip', 'Mark', 'Oliver', 'Lucy');
	foreach($people as $p)
	{
		$counter++;	
	}
	
	return $people;
	
}

main();



?>

I can't return the $counter variables, as I'm already returning something else. Please, please help me.

Thanks.

Can I know what is the returned error?

Your code should be like this:

<?php

function main()
{
    $counter = 0;
    $people = list_people();
    foreach($people as $p)
    {
        $counter++;
    }
    echo 'There are currently: ' . $counter . 'Number of people<br>';
    foreach($people as $person)
    {
        echo $person . '<br>';

    }

}

function list_people()
{


    $people = array ('Phillip', 'Mark', 'Oliver', 'Lucy');


    return $people;

}

main();



?>

This will surely fetch you result.

There were no return error.

I have sorted it now though, I saw that I was returning a array anyway so I can just use sizeof(array)
which will return the number anyway.

Thank you for your help guys :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.