Introduction

Welcome! Today, we will explore creating a simple address book application using PHP and its associative arrays. This task will help you understand how to manipulate associative arrays in PHP, focusing on adding, retrieving, and deleting entries. By the end of this lesson, you'll have a solid grasp of these fundamental operations.

Introducing Methods to Implement

In this task, we will implement three methods to manage our address book:

  • function addContact($name, $phoneNumber): Adds a new contact. Returns false if the contact already exists; otherwise, adds the contact and returns true. In this task, let's assume phone numbers do not change, so it's not allowed to overwrite the existing contact's number.
  • function getContact($name): Retrieves the phone number for a given name. Returns null if the contact does not exist.
  • function deleteContact($name): Deletes a contact with the given name. Returns true if the existing contact is deleted; false otherwise.

Let's break down each method in detail in the next sections.

Step 1: Implementing 'addContact'

This method adds a new contact to the address book with the given $name and $phoneNumber. If the contact already exists, it returns false. Otherwise, it adds the contact and returns true.

Question: Why do you think we need to check if the contact already exists?

Answer: To avoid duplicating existing entries. If a contact with the same name already exists, we shouldn't allow overwriting its phone number in this method, as it's only for creation.

Here is the method implementation:

In this method:

  • We verify if the contact already exists using isset($this->contacts[$name]).
  • If it exists, we return false.
  • If it doesn't exist, we add it to our associative array and return true.
Step 2: Implementing 'getContact'

This method retrieves the phone number associated with a given $name. If the contact does not exist, it returns null.

Question: Why is returning null useful when a contact doesn't exist?

Answer: Returning null indicates that the contact was not found in the address book, enabling us to handle such cases clearly and allowing the code to make decisions based on the presence or absence of a contact.

Here is the method implementation:

In this method:

  • We check if the contact exists in the associative array using isset.
  • If the name exists, we return the phone number.
  • If the name doesn't exist, we return null.
Step 3: Implementing 'deleteContact'

This method deletes a contact with the given $name. If the contact is successfully deleted, it returns true. If the contact does not exist, it returns false.

Question: What could be a real-world consequence of not checking if the contact exists before deletion?

Answer: Attempting to delete a non-existent contact could result in errors or unintended behavior.

Here is the method implementation:

In this method:

  • We verify if the contact exists using isset.
  • If it exists, we delete it with unset and return true.
  • If the contact doesn't exist, we return false.
Why PHP Associative Arrays Are Efficient for These Tasks

Associative arrays in PHP are particularly efficient for managing an address book due to several reasons:

  • Efficient Lookups: Associative arrays provide average constant time complexity for lookups. This means retrieving a contact's phone number by name is very fast, even with a large number of contacts.
  • Uniqueness: Associative array keys (in this case, contact names) are unique by nature, preventing duplicate entries and maintaining data integrity.
  • Readability: The syntax for accessing associative array entries is straightforward and highly readable, making the code easier to understand and maintain.
  • Flexibility: PHP's associative arrays can hold a variety of data types as values, enabling the easy extension of the address book to store additional information, such as email addresses, if needed in the future.

These characteristics make PHP associative arrays an ideal choice for implementing an address book.

Lesson Summary

In this lesson, we created a simple address book application using PHP's associative arrays. We implemented methods to add, retrieve, and delete contacts. Each step was built upon the previous one, enhancing our understanding of manipulating associative arrays in PHP. Happy coding!

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