PHP Program to Find the Youngest Person in a List

By Shahnail Khan Last updated : April 11, 2024

Let's say you have a list of ages of people, and you want to find out who is the youngest among them. In simpler terms, you have an array containing ages, and you need to determine the age and position of the youngest person in the group.

Finding the Youngest Person in a List

To find the youngest person in a list, you can use the array_search() function which will search for the minimum age from the given ages of the person. The function returns the key (position) of the first occurrence of the minimum age in the array.

PHP Program to Find the Youngest Person in a List

The following PHP source code has the ages of the person and finds the youngest person having the minimum age among the given ages.

<?php
// List containing ages of person
$ages = [25, 18, 22, 30, 21];

// Find the minimum age using the
// min() function
$youngestAge = min($ages);

// Now perform the array search using the
// array_search() method
$position = array_search($youngestAge, $ages);

// Printing the result
if ($position !== false) {
    echo "The youngest person is $youngestAge years old, and their position is: $position";
} else {
    echo "No age information found.";
}

?>

Output

The output of the above program is:

The youngest person is 18 years old, and their position is: 1

In this case, it will display: "The youngest person is 18 years old, and their position is: 1" because 18 is the minimum age, and it appears at index 1 in the array.

To understand the above example, you should have the knowledge of the following PHP Topics:

More PHP Array Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.