0
0
PHPprogramming~15 mins

String length and counting in PHP - Mini Project: Build & Apply

Choose your learning style9 modes available
Counting Characters in a String
📖 Scenario: You are working on a simple text analyzer tool that helps writers check the length of their sentences and count specific characters.
🎯 Goal: Build a PHP program that stores a sentence, sets a character to count, calculates the sentence length, counts how many times the character appears, and then prints both results.
📋 What You'll Learn
Create a string variable with a specific sentence.
Create a variable to hold a single character to count.
Calculate the length of the string using the correct PHP function.
Count how many times the character appears in the string.
Print the length and the count with clear messages.
💡 Why This Matters
🌍 Real World
Counting characters and measuring string length is useful in text processing, validating input, and analyzing data in many software applications.
💼 Career
Understanding string functions in PHP is important for web developers who handle user input, form validation, and content management.
Progress0 / 4 steps
1
Create the sentence string
Create a string variable called sentence and set it to the exact value "Learning PHP is fun and useful!".
PHP
Need a hint?

Use double quotes to create the string exactly as shown.

2
Set the character to count
Create a variable called charToCount and set it to the single character 'u' (including single quotes).
PHP
Need a hint?

Remember to use single quotes for a single character in PHP.

3
Calculate length and count character
Create a variable called length and set it to the length of sentence using the strlen() function. Then create a variable called countChar and set it to the number of times charToCount appears in sentence using the substr_count() function.
PHP
Need a hint?

Use strlen() for length and substr_count() to count characters.

4
Print the results
Use two echo statements to print the length and the count. First print "Length of sentence: " followed by length and a newline. Then print "Count of 'u': " followed by countChar and a newline.
PHP
Need a hint?

Use echo with concatenation and include newline characters \n.