Lesson 1
PHP Arrays and Strings Fundamentals
Introduction

Welcome!

Before diving into PHP essentials for your development toolkit, let's start with some foundational PHP features — namely, arrays and strings. These features allow PHP to group multiple elements, such as numbers or characters, under a single entity.

Understanding Arrays and Strings

As our starting point, it's crucial to understand how arrays and strings function in PHP. Arrays in PHP are flexible and can hold different types of data, while strings are sequences of characters that can be effectively manipulated using a variety of helpful functions. Let's see examples:

php
1<?php 2 3// Defining an array and a string 4$myArray = [1, 2, 3, 4]; 5$myString = "hello"; 6 7// Now let's try to change the first element of both these features 8// Changing the first element of the array 9$myArray[0] = 100; 10 11// For strings, we can use string replacement functions to obtain a new string 12$newString = str_replace('h', 'H', $myString); 13 14echo implode(", ", $myArray) . "\n"; // prints 100, 2, 3, 4 15echo $myString . "\n"; // prints hello 16echo $newString . "\n"; // prints Hello 17?>
Diving Into Lists

Imagine having to take an inventory of all flora in a forest without a list at your disposal — seems near impossible, right? That's precisely the purpose arrays serve in PHP. They let us organize data so that each item holds a definite position or an index. The index allows us to access or modify each item individually.

Working with arrays in PHP is as simple as this:

php
1<?php 2 3// Creating an array 4$fruits = ["apple", "banana", "cherry"]; 5 6// Add a new element at the end 7$fruits[] = "date"; // ['apple', 'banana', 'cherry', 'date'] 8echo implode(", ", $fruits) . "\n"; // prints apple, banana, cherry, date 9 10// Inserting an element at a specific position 11array_splice($fruits, 1, 0, "bilberry"); // ['apple', 'bilberry', 'banana', 'cherry', 'date'] 12echo implode(", ", $fruits) . "\n"; // prints apple, bilberry, banana, cherry, date 13 14// Removing a particular element 15unset($fruits[array_search("banana", $fruits)]); // ['apple', 'bilberry', 'cherry', 'date'] 16echo implode(", ", $fruits) . "\n"; // prints apple, bilberry, cherry, date 17 18// Accessing elements using indexing 19$firstFruit = $fruits[0]; // apple 20echo $firstFruit . "\n"; // prints apple 21 22$lastFruit = end($fruits); // date 23echo $lastFruit . "\n"; // prints date 24 25// Converting static array to a dynamic list and vice versa 26$fruitArray = ["kiwi", "lemon", "mango"]; 27$fruitList = array_merge($fruits, $fruitArray); 28echo implode(", ", $fruitList) . "\n"; // prints apple, bilberry, cherry, date, kiwi, lemon, mango 29 30$newFruitArray = array_values($fruitArray); 31echo implode(", ", $newFruitArray) . "\n"; // prints kiwi, lemon, mango 32?>
Understanding Strings

Think of strings as a sequence of letters or characters. So, whether you're writing down a message or noting a paragraph, it all boils down to a string in PHP. Strings are enclosed by quotes.

php
1<?php 2 3// Creating a string 4$greetingString = "Hello, world!"; 5echo $greetingString . "\n"; // prints Hello, world! 6 7// Accessing characters using indexing 8$firstChar = $greetingString[0]; // 'H' 9echo $firstChar . "\n"; // prints H 10 11$lastChar = $greetingString[strlen($greetingString) - 1]; // '!' 12echo $lastChar . "\n"; // prints ! 13 14// Making the entire string lowercase 15$lowercaseGreeting = strtolower($greetingString); // "hello, world!" 16echo $lowercaseGreeting . "\n"; // prints hello, world! 17?>

To enrich our opportunities to work with PHP strings, use functions such as strtolower(), strtoupper(), and trim(). These functions essentially create a new string for us. To efficiently append or modify a string, you can directly concatenate or manipulate it using various string functions in PHP.

Indexing and Common Operations

Both arrays and strings in PHP allow us to access individual elements through indexing. In PHP, we start counting from 0, implying the first element is at index 0, the second at index 1, and so on. Negative indexing is not directly supported, but you can simulate it by adjusting indices based on the length of the collection.

We have many operations we can perform on our arrays and strings. We can slice them, concatenate them, and even find an occurrence of a particular element!

php
1<?php 2 3// Define an array and a string 4$myArray = [1, 2, 3, 4, 5]; 5$myString = "Hello"; 6 7// Slicing: array_slice for arrays and substr for strings 8$sliceArray = array_slice($myArray, 2, 2); // [3, 4] 9echo implode(", ", $sliceArray) . "\n"; // prints 3, 4 10 11$sliceString = substr($myString, 1, 2); // "el" 12echo $sliceString . "\n"; // prints el 13 14// Concatenation: array_merge for arrays and '.' operator for strings 15$concatArray = array_merge($myArray, [6, 7, 8]); // [1, 2, 3, 4, 5, 6, 7, 8] 16echo implode(", ", $concatArray) . "\n"; // prints 1, 2, 3, 4, 5, 6, 7, 8 17 18$concatString = $myString . ", world!"; // "Hello, world!" 19echo $concatString . "\n"; // prints Hello, world! 20 21// Finding the index of an element in an array or a string 22$indexArray = array_search(3, $myArray); // 2 - Index of element '3' 23echo $indexArray . "\n"; // prints 2 24 25$indexString = strpos($myString, 'e'); // 1 - Index of character 'e' 26echo $indexString . "\n"; // prints 1 27 28// Sorting items in array in non-increasing order 29rsort($myArray); // [5, 4, 3, 2, 1] 30echo implode(", ", $myArray) . "\n"; // prints 5, 4, 3, 2, 1 31?>
Lesson Summary and Practice

Give yourself a pat on the back! You've navigated through PHP arrays and strings, learning how to create, access, and manipulate them via various operations.

Up next, reinforce your understanding with plenty of hands-on practice. The comprehension of these concepts, combined with frequent practice, will enable you to tackle more complex problem statements with ease. Happy coding!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.