0

I use echo inside a function to display a single value on webpage but I also can do it in another way use return function.

Which way is better? I want my code to be reuse-able and tight.

Option 1 echo:

function display_wuuk_time($link, $taskid){//

    $sql = "SELECT start from wuuk where id =$taskid;";
    $result = mysql_query($sql, $link);

    $datetime = mysql_result($result,0);
    echo "<b>$datatme</b>";

};

Option 2 return:

function return_wuuk_time($link, $taskid){//

    $sql = "SELECT start from wuuk where id =$taskid;";
    $result = mysql_query($sql, $link);

    $isempty = mysql_num_rows($result);
    If($isempty ==0){return 0;}//if no rating 

    $datetime = mysql_result($result,0);
    return $datetime;

};

display.html:

<?php
// Option 1
display_wuuk_time($link, $taskid);

// Option 2 
$datetime = return_wuuk_time($link, $taskid);
echo <b>$datetime</b>
?>
DirtyBit
  • 16,613
  • 4
  • 34
  • 55
n00dle
  • 63
  • 5
  • I want my code to be reuse-able and _tight_. Better avoid the _deprecated_ mysql functions and move to either `mysqli` or `PDO` then. – DirtyBit Sep 27 '15 at 08:18
  • 4
    possible duplicate of [What is the difference between PHP echo and PHP return in plain English?](http://stackoverflow.com/questions/9387765/what-is-the-difference-between-php-echo-and-php-return-in-plain-english) – aldrin27 Sep 27 '15 at 08:19
  • [Separation of concerns](http://effectivesoftwaredesign.com/2012/02/05/separation-of-concerns/) Suggests 2. as a starting point. You'll then use a real template system to separate the actual printing and inside your templates you'll get rid of that awful `` and use ``, which you'll define in a CSS file, thus achieving separation of database logic, output logic and presentation/styling. – Tobia Tesan Sep 27 '15 at 08:26
  • thx tobia :) it's a lot of informations. I don't get it. Can you show me an example about and use? I really want to separation of database logic, output logic and presentation/styling – n00dle Sep 28 '15 at 17:53

1 Answers1

1

Option 2 gives you the most flexibility when reusing the code. Next time you use it, you may not want to echo it out directly, but to perform other actions on it, store it for later etc

Oli
  • 1,003
  • 7
  • 17
  • thx ;) You are right. I start use this function again and again. my first planned code reuse exp! – n00dle Oct 18 '15 at 03:41