0

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.

2
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by phorce
0

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();



?>
0

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 :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.