-1

In PHP why does capturing a function (which executes an echo statement) in a variable automatically print it without using echo? I'm sure i'm missing a fundamental truth about functions, but for the life of me I can't work this out.

when I've defined variables before, the value only prints to browser/terminal if I use echo, e.g.

$value = 3;//doesn't print anything as just defining the variable
echo $value;//prints 3 as have used echo

but if I assign a function (which itself executes an echo statement) to a variable, the act of assigning automatically prints the echo statement, e.g.

function number() 
{echo "3";}

$number = number(); // Prints: 3

Does anybody have any idea what I'm trying to get at or am I talking gibberish?

Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • I think you're looking for [return](https://www.php.net/manual/en/functions.returning-values.php) instead of `echo`. – KIKO Software May 19 '23 at 09:55
  • The function does the echoing. The `$numbers` variable contains nothing, because the function doesn't `return` anything. – deceze May 19 '23 at 09:56

1 Answers1

1

The line $number = number(); isn't assigning the function, it's calling it. Since it is executed, echo is called, and you'll see 3 on your terminal.

Mureinik
  • 297,002
  • 52
  • 306
  • 350