×

php · level 1 · unit 3 · lesson 3

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. 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.