0

I am have a code like this:

function sampleName(string $name)
{
    echo ", good morning $name" . PHP_EOL;
}

function showName(string $name, $filter): string
{
    return  "Hello" . $filter($name) . PHP_EOL;
}

showName("John", "sampleName") . PHP_EOL;

And the result is :

, good morning John

How to show the Hello when i am call showName() function?

Kholid Saifulloh
  • 191
  • 1
  • 1
  • 5
  • 1
    `sampleName()` should return the string, not echo it. – Barmar Sep 17 '21 at 02:57
  • 1
    So this is a Typo question right? I mean, you know how to write the word `return` and you know what it does, you just didn't do it. This is just a facepalm question, right? We don't have anything new to teach you here. You just reversed the `echo` and the `return`. – mickmackusa Sep 17 '21 at 05:13
  • https://stackoverflow.com/q/9387765/2943403 – mickmackusa Sep 17 '21 at 05:18

2 Answers2

1

Return values from SampleName Function and print return value of ShowName function.

function sampleName(string $name)
{
    return ", good morning $name" . PHP_EOL;
}

function showName(string $name, $filter) : string
{
    return "Hello" . $filter($name) . PHP_EOL;
}

echo showName("John", "sampleName") . PHP_EOL;

Output : Hello, good morning John

Thanjeys
  • 24
  • 4
0

The showName function return's which is why the Hello part does not show.

You could either echo instead of return in the function or echo your call to the function, ie echo showName('John', 'sampleName');

cOle2
  • 4,725
  • 1
  • 24
  • 26