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.
$name = "Ada";
echo "Hello, " . $name . "!";
// Hello, Ada!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.
$name = trim(" Ada Lovelace ");
// "Ada Lovelace"(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.
Variables · Function arguments
- Join strings with the dot operator
- Trim surrounding whitespace
- Return a greeting for different inputs