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.
In this task, we will implement three methods to manage our address book:
function addContact($name, $phoneNumber)
: Adds a new contact. Returnsfalse
if the contact already exists; otherwise, adds the contact and returnstrue
. 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 givenname
. Returnsnull
if the contact does not exist.function deleteContact($name)
: Deletes a contact with the givenname
. Returnstrue
if the existing contact is deleted;false
otherwise.
Let's break down each method in detail in the next sections.
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:
php1<?php 2 3class AddressBook { 4 private $contacts = array(); 5 6 public function addContact($name, $phoneNumber) { 7 if (isset($this->contacts[$name])) { 8 return false; 9 } 10 $this->contacts[$name] = $phoneNumber; 11 return true; 12 } 13 14 // To display the current contacts 15 public function printContacts() { 16 foreach ($this->contacts as $name => $phoneNumber) { 17 echo "$name: $phoneNumber\n"; 18 } 19 } 20} 21 22// Example usage: 23$addressBook = new AddressBook(); 24echo $addressBook->addContact("Alice", "123-456-7890") ? "true\n" : "false\n"; // true 25echo $addressBook->addContact("Alice", "098-765-4321") ? "true\n" : "false\n"; // false 26$addressBook->printContacts(); // Alice: 123-456-7890
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
.
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:
php1<?php 2 3class AddressBook { 4 private $contacts = array(); 5 6 public function addContact($name, $phoneNumber) { 7 if (isset($this->contacts[$name])) { 8 return false; 9 } 10 $this->contacts[$name] = $phoneNumber; 11 return true; 12 } 13 14 public function getContact($name) { 15 if (isset($this->contacts[$name])) { 16 return $this->contacts[$name]; 17 } 18 return null; 19 } 20 21 public function printContacts() { 22 foreach ($this->contacts as $name => $phoneNumber) { 23 echo "$name: $phoneNumber\n"; 24 } 25 } 26} 27 28// Example usage: 29$addressBook = new AddressBook(); 30$addressBook->addContact("Alice", "123-456-7890"); 31$contact = $addressBook->getContact("Alice"); 32if ($contact !== null) { 33 echo $contact . "\n"; // 123-456-7890 34} else { 35 echo "Contact not found\n"; 36} 37 38$contact = $addressBook->getContact("Bob"); 39if ($contact !== null) { 40 echo $contact . "\n"; 41} else { 42 echo "Contact not found\n"; // Contact not found 43}
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
.
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:
php1<?php 2 3class AddressBook { 4 private $contacts = array(); 5 6 public function addContact($name, $phoneNumber) { 7 if (isset($this->contacts[$name])) { 8 return false; 9 } 10 $this->contacts[$name] = $phoneNumber; 11 return true; 12 } 13 14 public function getContact($name) { 15 if (isset($this->contacts[$name])) { 16 return $this->contacts[$name]; 17 } 18 return null; 19 } 20 21 public function deleteContact($name) { 22 if (isset($this->contacts[$name])) { 23 unset($this->contacts[$name]); 24 return true; 25 } 26 return false; 27 } 28 29 public function printContacts() { 30 foreach ($this->contacts as $name => $phoneNumber) { 31 echo "$name: $phoneNumber\n"; 32 } 33 } 34} 35 36// Example usage: 37$addressBook = new AddressBook(); 38$addressBook->addContact("Alice", "123-456-7890"); 39echo $addressBook->deleteContact("Alice") ? "true\n" : "false\n"; // true 40echo $addressBook->deleteContact("Bob") ? "true\n" : "false\n"; // false 41$addressBook->printContacts(); // (empty)
In this method:
- We verify if the contact exists using
isset
. - If it exists, we delete it with
unset
and returntrue
. - If the contact doesn't exist, we return
false
.
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.
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!