Skip to content
PHP Logo

How can the code snippet be modified to correctly display the count of different planets?

The issue with the current code snippet is that it is not correctly counting the different planets due to the incorrect usage of the array_unique() function. To fix this, we need to remove the array_unique() function and instead use the count() function on the $planets array to get the count of different planets.

php
$planets = ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune', 'Earth', 'Mars', 'Jupiter'];

$uniquePlanets = count(array_unique($planets));
echo "There are " . $uniquePlanets . " different planets in the array.";