-5
<?php
function remove_char(string $s):string
    {
        settype($s, 'string');
        $pieces = str_split($s);
        unset($pieces[0]);
        $numb = count($pieces);
        unset($pieces[$numb - 1]);
        echo implode("", $pieces);
    }

    $lotsp = "lotsp";`enter code here`
    remove_char($lotsp);
?>

The function 'remove_char()' supposed to bring 'String' type of output. However it is outputting anything other than string.

Error: FATAL ERROR Uncaught TypeError: Return value of remove_char() must be of the type string, none returned in /home4/phptest/public_html/code.php70(5) : enter code here

I don't know where is the problem. Any help is greatly appreciated. Thanks

bob_cat
  • 11
  • 7

1 Answers1

1

You are declaring that remove_char() should return a string but you are not returning anything.

Remove :string from your function declaration:

function remove_char(string $s)
{
    settype($s, 'string');
    $pieces = str_split($s);
    unset($pieces[0]);
    $numb = count($pieces);
    unset($pieces[$numb - 1]);
    echo implode("", $pieces);
}

Now you should be able to echo from within the function.

See this answer about the difference between echo and return.

Camilo
  • 6,504
  • 4
  • 39
  • 60