In PHP, how do I iterate over strings with examples?

In PHP, you can iterate over strings in various ways. One common method is to convert the string into an array of characters and then loop through it. Below are some examples illustrating how to do this.

Keywords: PHP, strings, iterate, loop, characters.
Description: This section provides examples of how to iterate over strings in PHP, showcasing different techniques and approaches.

Example 1: Using a for loop to iterate over a string:

<?php $string = "Hello World"; for ($i = 0; $i < strlen($string); $i++) { echo $string[$i] . "<br>"; } ?>

Example 2: Using a foreach loop after converting the string to an array:

<?php $string = "Hello World"; $characters = str_split($string); foreach ($characters as $char) { echo $char . "<br>"; } ?>

Example 3: Using the mb_strlen and mb_substr functions for multibyte strings:

<?php $string = "こんにちは世界"; // Hello World in Japanese for ($i = 0; $i < mb_strlen($string); $i++) { echo mb_substr($string, $i, 1) . "<br>"; } ?>

Keywords: PHP strings iterate loop characters.