1

This is the function:

function mostFrequent($x) {
    $counted = array_count_values($x);
    arsort($counted);
    return(key($counted));    
}

This is the call in the

tag:

<?php
    $x=mostFrequent(array('cheese','wine','cheese','bread','cheese','bread'));
    echo "$x";
?>

This should work right? Also, how do I avoid having to use the temp $x for the echo and just echo the result of the function call directly?

2 Answers 2

5

Your solution seems reasonable.

<?php
    echo mostFrequent(array('cheese','wine','cheese','bread','cheese','bread'));
?>
Sign up to request clarification or add additional context in comments.

3 Comments

OK, I'm dumb, an earlier parameter for the first function for the assignment had %n instead of $n. /facepalm.
On another note, how do I add a period to that echo? Do I have to make a second echo?
NVM on that as well it's echo a().'.';
1
echo mostFrequent(array('cheese','wine','cheese','bread','cheese','bread'));     

however, it seems wrong way and using a "temp" variable would be better.
Eventually you will learn that it is better to separate business logic from presentation logic.
So, the business of your business logic turns out to be exactly collecting these "temp" variables for the later use in the presentation logic.

also note that variables in PHP require no quotes to address. you are confusing them with strings.

echo $x;

is the proper syntax.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.