Skip to content

Lesson 2: Built-in Functions

Objective:

The objective of this lesson is to familiarize learners with commonly used built-in functions in PHP. By the end of this lesson, participants should be able to:

  • Explore and understand commonly used PHP functions.
  • Utilize string manipulation functions for text processing.
  • Work with array manipulation functions for efficient data handling.

Description:

2.1 Commonly Used PHP Functions

2.1.1 Introduction to Built-in Functions

Understand the concept of built-in functions in PHP and their importance in simplifying common tasks.

2.1.2 Examples of Common Functions

Explore commonly used functions for tasks such as mathematical operations, date and time manipulation, and type checking.

<?php
    $number = 15.75;

    echo "Absolute Value: " . abs($number) . "<br>"; // Outputs: 15.75
    echo "Rounded Value: " . round($number) . "<br>"; // Outputs: 16
    echo "Square Root: " . sqrt($number) . "<br>"; // Outputs: 3.96484...
?>

2.2 String Manipulation Functions

2.2.1 Concatenation and Length

Learn how to concatenate strings using the . operator and find the length of a string.

<?php
    $string1 = "Hello";
    $string2 = "PHP";

    echo $string1 . " " . $string2 . "<br>"; // Outputs: Hello PHP
    echo "Length of string1: " . strlen($string1); // Outputs: 5
?>

2.2.2 Substring and Replace

Explore functions for extracting substrings and replacing text within a string.

<?php
    $sentence = "PHP is a powerful scripting language.";

    echo "Substring: " . substr($sentence, 0, 3) . "<br>"; // Outputs: PHP
    echo "Replace: " . str_replace("powerful", "versatile", $sentence); // Outputs: PHP is a versatile scripting language.
?>

2.3 Array Manipulation Functions

2.3.1 Adding and Removing Elements

Learn functions for adding and removing elements from an array

<?php
    $colors = ["red", "green", "blue"];

    array_push($colors, "yellow"); // Add element to the end
    array_pop($colors); // Remove element from the end

    print_r($colors); // Outputs: Array ( [0] => red [1] => green [2] => blue )
?>

2.3.2 Sorting and Searching

Explore functions for sorting and searching array

<?php
    $numbers = [4, 2, 8, 1, 6];

    sort($numbers); // Sort in ascending order
    echo "Sorted Numbers: " . implode(", ", $numbers) . "<br>"; // Outputs: 1, 2, 4, 6, 8

    $position = array_search(6, $numbers);
    echo "Position of 6: $position"; // Outputs: 3
?>

2.4 Practice Exercise

Let’s create a practice exercise where we’ll use both string and array manipulation functions to process and display information. In this exercise, we’ll work with a simple array of words and perform operations on them.

Copy and paste the following code into a PHP file:

<!DOCTYPE html>
<html>
<head>
    <title>String and Array Manipulation Exercise</title>
</head>
<body>

    <?php
    // PHP code for the practice exercise
    $words = ["apple", "banana", "cherry", "date", "fig"];

    // Display the original array
    echo "<h2>Original Array:</h2>";
    print_r($words);

    // 1. Concatenate all words into a single string separated by commas
    $concatenatedString = implode(", ", $words);
    echo "<h2>Concatenated String:</h2>";
    echo $concatenatedString;

    // 2. Find and display the length of the concatenated string
    $concatenatedStringLength = strlen($concatenatedString);
    echo "<h2>Length of Concatenated String:</h2>";
    echo $concatenatedStringLength;

    // 3. Extract and display a substring from the concatenated string
    $substring = substr($concatenatedString, 0, 10); // Adjusted length for a valid substring
    echo "<h2>Substring:</h2>";
    echo $substring;

    // 4. Replace a word in the original array and display the modified array
    $words[array_search("banana", $words)] = "grape";
    echo "<h2>Modified Array:</h2>";
    print_r($words);
    ?>

</body>
</html>

Here’s a breakdown of the script:

  1. Create an Array of Words: The script initializes an array named $words containing five fruit names: “apple”, “banana”, “cherry”, “date”, and “fig”.
  2. Display the Original Array: The script uses print_r() to display the contents of the $words array in a readable format, including the array’s keys and values.
  3. Concatenate Words into a String: The script utilizes implode() to concatenate all elements of the $words array into a single string, separating each word with a comma and a space. The resulting concatenated string is stored in the $concatenatedString variable.
  4. Display the Concatenated String: The script uses echo to display the concatenated string, presenting the concatenated words separated by commas.
  5. Calculate String Length: The script calculates the length of the concatenated string using strlen(), storing the length in the $concatenatedStringLength variable.
  6. Display String Length: The script uses echo to display the length of the concatenated string, informing the user of the number of characters in the combined word list.
  7. Extract a Substring: The script extracts a substring from the concatenated string using the substr() function. The substring is specified as the first 10 characters of the $concatenatedString array, capturing the first few words. The extracted substring is stored in the $substring variable.
  8. Display the Substring: The script uses echo to display the extracted substring, presenting the first few concatenated words.
  9. Replace an Element in the Array: The script utilizes array_search() to identify the index of the word “banana” within the $words array. Then, it replaces the element at that index with “grape”, effectively replacing “banana” with “grape” in the original array.
  10. Display the Modified Array: The script uses print_r() to display the updated contents of the $words array, including the replaced word “banana” with “grape”.