×

php · level 2 · 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

Ask a string what is inside it, and where.

gives true when the second string appears anywhere in the first. str_starts_with and str_ends_with check only the start or the end. All three care about capital letters.

tells you where. It gives a , counting from 0 like an array index, or false when the text is not there. Careful: a match at the very start gives 0, so always compare with !== false.

cuts out 5 characters starting at position 0. A negative start counts from the end, so substr($file, -3) gives the last three.

<?php
$email = "mia@example.com";
if (str_contains($email, "@")) {
    echo "Looks like an email\n";
}
$at = strpos($email, "@");
echo "The @ is at position $at\n";
echo "Name: " . substr($email, 0, $at) . "\n";
echo "Ends: " . substr($email, -3) . "\n";
if (str_ends_with($email, ".com")) {
    echo "A .com address\n";
}

Leave out the length, as in substr($email, 4), and you get everything to the end.