php · level 1 · unit 3 · lesson 3
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
PHP comes with ready-made tricks for text.
counts the characters in a string. A is one letter, digit, space or symbol, so strlen("Hi!") gives 3. strtoupper turns text into capitals and strtolower into small letters.
trim cuts the spaces off both ends. str_replace("cat", "dog", $text) swaps every cat in $text for a dog: what to find, what to put instead, and where to look.
Each one hands back a new string and leaves your box as it was. Misspell a name and PHP stops with Call to undefined function.
<?php
$bark = "woof";
echo strtoupper($bark) . "\n";
echo strlen($bark) . "\n";
echo trim(" tidy ") . "\n";
echo str_replace("cat", "dog", "I love my cat") . "\n"; strlen is short for string length, and strtoupper for string to upper case.
guess first
Did it shout? What will this show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
<?php
$shout = "help";
strtoupper($shout);
echo $shout . "\n";
your turn
Your turn. Shout a word and count it.
Type any word you like in place of the blank. Run it to shout it and count its characters, then try a word with a space in it.
fix it
Why is it not shouting? Make it shout.
- Read the error:
Call to undefined function strtouper(). Does PHP know that name? - The name is nearly right. It stands for string to upper, and one letter is missing.
- Change
strtoupertostrtoupperon line 3, then press run.
nailed it
your strings can shout, whisper and tidy up. :D
strlencounts characters, spaces included.strtoupper,strtolower,trimandstr_replacechange text.- They hand back a new string, so store it to keep it.
new words in your collection
- character
- strlen
This lesson isn’t saved. A free account remembers every lesson you finish.