Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP echo function return value vs echo inside function

Tags:

php

Typically, I'll write a function like so:

function alertClass($field,$full=false){
 global $formErrors;
 $html = $full ? ' class="alert"' : ' alert';
 if (!empty($formErrors[$field])) return $html;
}

and then where I want the html to show I'll echo the return value of the function like so:

echo alertClass('somefield')

but today I was thinking why not just put the echo in the function instead of using it's return value? So instead of "return $html" it would be "echo $html"... Is there an advantage to one way or the other?

like image 830
Karl Avatar asked Aug 30 '10 16:08

Karl


Video Answer


1 Answers

for example when you echo the text out of your function just like this...

function yourStatus(){
   echo ' Done';
}

echo 'Status ='. yourStatus();

your output will look like this

"DoneStatus ="

instead of

"Status = Done"

cheers

like image 50
pabenta.com.ph Avatar answered Sep 28 '22 07:09

pabenta.com.ph