PHP String
PHP string exercises: reverse a string, find its length, count words, convert to uppercase and convert an array to a string.
Write a PHP program to reverse the string
Description: Write a PHP program to reverse a string.
Rules and hints:
- You can use any string related built-in function.
- Use only one variable.
PHP program to reverse a string
<?php
$str = "Tutorials Class";
echo strrev($str);
?>
Output:
ssalC slairotuT
How it works
strrev()reverses the order of all characters, including the space.
Try this too
- Check if a word is a palindrome (same when reversed), like “madam”.
- Reverse the order of the words, and not the letters.
Write a PHP program to find the length of the string
Description: Write a PHP program to find the length of a string.
Rules and hints:
- Use one variable.
- Use a built-in PHP string function.
PHP program to find the length of a string
<?php
$str = "Tutorials Class";
echo strlen($str);
?>
Output:
15
How it works
strlen()returns the number of characters (bytes) of the string.- The space between the two words is counted too, so the length is 15.
For non-English text read strlen() vs mb_strlen().
Write a PHP program to count the words in the string
Description: Write a PHP program to count the number of words in a string.
Rules and hints:
- Use only one variable.
- Use a PHP built-in string function.
PHP program to count the words in a string
<?php
$sample_words = "This is Tutorials Class, learn programming tutorials here.";
echo str_word_count($sample_words);
?>
Output:
8
How it works
str_word_count()counts the words made of letters. Commas and spaces are not counted as words.- The sentence has 8 words.
More about it: How to count the words of a string in PHP.
Write a PHP program to convert a string into uppercase
Description: Write a PHP program to convert all the characters of a string into uppercase.
Rules and hints:
- Use only one variable.
- Use a PHP built-in string function.
PHP program to convert a string into uppercase
<?php
$str = "Tutorials Class";
echo strtoupper($str);
?>
Output:
TUTORIALS CLASS
How it works
strtoupper()returns the string with all letters in capital.- For text in other languages (like accents) use
mb_strtoupper().
Related: PHP String Handling and ucwords().
PHP Array to String Conversion (favourite colours chosen by user)
Description: Write a PHP exercise for array to string conversion.
Rules and hints:
- Create a form that accepts a name and some colors (checkboxes).
- After the submit, the data goes to another page.
- Show the user name and the selected colors as a list.
- The name and at least one color are required.
Step 1: the form (index.html)
<form action="data.php" method="post">
Name: <input type="text" name="username" required><br><br>
Select your favourite colors:<br>
<input type="checkbox" name="colors[]" value="Red"> Red
<input type="checkbox" name="colors[]" value="Green"> Green
<input type="checkbox" name="colors[]" value="Blue"> Blue
<input type="checkbox" name="colors[]" value="Yellow"> Yellow<br><br>
<button type="submit">Send</button>
</form>
The name colors[] (with the square brackets) tells PHP to collect all the selected values in an array.
Step 2: data.php
<?php
$username = trim($_POST["username"] ?? "");
$colors = $_POST["colors"] ?? [];
if ($username === "" || empty($colors)) {
exit("Name and at least one color are required.");
}
echo "Hello " . htmlspecialchars($username) . "<br>";
echo "Your favourite colors: " . htmlspecialchars(implode(", ", $colors));
?>
Output:
Hello Robin
Your favourite colors: Red, Blue
How it works
$_POST["colors"]is an array of the checked boxes.implode(", ", $colors)converts the array to a text. See how to convert an array to a string.htmlspecialchars()protects the page from XSS.