Lesson 6 of 100
Strings10 min5 checksPHP 8.5

Words into working code.

Meet PHP strings. Make a greeting. See it come to life.

01 · THE IDEAREAD → TRY → LEARN

Make yourself understood

A string is a piece of text. In PHP, wrap text in single or double quotes. You can store it in a variable, return it from a function, or send it to the page with echo.

Put two strings together

Use the dot (.) operator to join strings. Spaces and punctuation are part of the text, so include them inside the quotes.

Example
$name = "Ada";
echo "Hello, " . $name . "!";
// Hello, Ada!
Output
Hello, Ada!

Give input a little polish

Names sometimes arrive with extra whitespace. trim() removes spaces, tabs and line breaks from the beginning and end of a string. It leaves spaces inside the name alone.

Example
$name = trim("  Ada Lovelace  ");
// "Ada Lovelace"
Output
(No output)

Your turn

Complete greet(string $name): string so it returns Hello, NAME!, with whitespace trimmed from the name. Keep the comma, space and exclamation mark exactly as shown. Return the greeting from the function; the supplied echo lets you try it.

Check Answer calls your function with five different inputs, including whitespace and an empty name. Passing means your function behaves correctly for every case, not just the example on screen.

Before you start

Variables · Function arguments

You’ll practice
  • Join strings with the dot operator
  • Trim surrounding whitespace
  • Return a greeting for different inputs
Your task

Make greet() pass all 5 checks. Run your code as often as you like, then check your answer.

exercise.phpSaved in this browserPHP 8.5
CONSOLE & CHECKS● Ready

Ready when you are

Your output will appear here.
Make it work. Then make it pass.

PHP loads on your first run. Each check starts fresh. Your code stays in this browser; clearing site data removes your progress. A check or the task looks wrong? Report a problem with this exercise.