Using the Power of In-Built Controls

Welcome back! You've journeyed through crafting custom functions, mastering parameter handling, tracking variable scopes, and even exploring recursion. Today, we are going to tap into the powerful built-in functions that PHP offers. These functions are like advanced tools that help us execute common tasks effectively and efficiently. Are you excited to see what these functions can do for us? Let’s dive in!

What You'll Learn

In this unit, we will focus on how built-in functions in PHP can significantly simplify our code. By the end of this unit, you'll understand how to:

  • Use array-related functions to manipulate arrays.
  • Count elements, shift elements, and reverse arrays.
  • Find maximum values in associative arrays.

To give you a preview, consider this detailed example:

Defining and Printing the Array
<?php
// Defining array of planets
$planets = array("Mercury", "Venus", "Earth", "Mars");

// Function to display a full array 
print_r($planets);
?>

Here, the print_r function is employed to display the contents of the array, providing a quick and readable overview of the planets array.

Output:

Array
(
    [0] => Mercury
    [1] => Venus
    [2] => Earth
    [3] => Mars
)
Removing the Last Element
<?php
// Removing the last element
array_pop($planets);
echo "After removing the last planet:\n";
print_r($planets);
?>

The array_pop function removes the last element from the array as displayed in the output.

Output:

After removing the last planet:
Array
(
    [0] => Mercury
    [1] => Venus
    [2] => Earth
)
Removing the First Element
<?php
// Removing the first element
array_shift($planets);
echo "After removing the first planet:\n";
print_r($planets);
?>

On the other hand the array_shift function is used to eliminate the first element from the array.

Output:

After removing the first planet:
Array
(
    [0] => Venus
    [1] => Earth
)
Reversing the Array
Counting the Elements
<?php
// Counting the number of planets
echo "Number of planets in our mission plan: " . count($planets) . "\n";
?>

The count function calculates the total number of elements within the planets array.

Output:

Number of planets in our mission plan: 2
Finding Maximum Value
<?php
// Defining associative array of planets and distances
$distances = array("Mercury" => 35.9, "Venus" => 67.2, "Earth" => 93.0);

// Finding the farthest planet 
echo "Farthest planet distance: " . max($distances) . "\n";
?>

The max function determines the maximum value from the associative array $distances, identifying the farthest planet.

Output:

Farthest planet distance: 93
Why It Matters

Utilizing built-in functions not only makes your code more readable but also more efficient. Here’s why these functions are essential:

  • Efficiency: Built-in functions are optimized and faster compared to custom implementations.
  • Simplicity: They simplify tasks that would otherwise require multiple lines of code.
  • Maintainability: Code that uses built-in functions is easier to maintain and understand.

By the end of this unit, you will be equipped with the knowledge to handle common tasks more quickly and with less code. Think about how much easier it will be to manage arrays or find specific values with functions specifically designed for those tasks!

Ready to harness the power of PHP’s built-in functions and make your code more efficient? Let's move on to the practice section and apply these powerful tools together.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal