mac007 Posted March 18, 2011 Share Posted March 18, 2011 Hello, all: I have been having these odd display stuff when I'm either trying to display or call functions or definitions. Seems like they work best when ALWAYS using echo to call them, though they also work without it?? So, what gives? when or how should I use them??? Should Definitions or Functions always be used along with ECHO?? as you can see slightly confused... EXAMPLE: // Why does this line work??? note, see here I'm using echo several times in order to display right) <?php if(RIGHT_COLUMN_WIDTH != '0') { echo "<td valign='top' width='" . RIGHT_COLUMN_WIDTH . "px'>"; echo categories(); echo "</td>";} ?> // But this one doesnt display just right?? Note: see echo not used when categories() function is called, but isnt it still being called by first echo?? <?php if(RIGHT_COLUMN_WIDTH != '0') { echo "<td valign='top' width='" . RIGHT_COLUMN_WIDTH . "px'>" . categories() . "</td>";} ?> Quote Link to comment https://forums.phpfreaks.com/topic/231013-when-to-use-echo-to-display-functions-or-definitions/ Share on other sites More sharing options...
kenrbnsn Posted March 18, 2011 Share Posted March 18, 2011 What does the function categories look like. Please put your code between tags. Ken Quote Link to comment https://forums.phpfreaks.com/topic/231013-when-to-use-echo-to-display-functions-or-definitions/#findComment-1189194 Share on other sites More sharing options...
mac007 Posted March 18, 2011 Author Share Posted March 18, 2011 Looks like this... function categories() { echo '<table style="margin:20px;" width="100%">'; echo '<tr>'; echo '<td>'; $categories = mysql_query('select * from categories'); while ($rowCategory = mysql_fetch_array($categories)) { echo "<a style='line-height:20px;' href='?category=" . $rowCategory['category_id'] . "'>" . $rowCategory['category_name'] . '</a><br>'; } echo '</td>'; echo '</tr>'; echo '</table>'; mysql_free_result($categories); } Quote Link to comment https://forums.phpfreaks.com/topic/231013-when-to-use-echo-to-display-functions-or-definitions/#findComment-1189200 Share on other sites More sharing options...
kenrbnsn Posted March 18, 2011 Share Posted March 18, 2011 Functions should return a value to be used, they really shouldn't echo anything, try this instead: <?php function categories() { $tmp = array(); $tmp[] = '<table style="margin:20px;" width="100%">'; $tmp[] = '<tr>'; $tmp[] = '<td>'; $categories = mysql_query('select * from categories'); while ($rowCategory = mysql_fetch_array($categories)) { $tmp[] = "<a style='line-height:20px;' href='?category={$rowCategory['category_id']}'>{$rowCategory['category_name']}</a><br>"; } $tmp[] = '</td>'; $tmp[] = '</tr>'; $tmp[] = '</table>'; mysql_free_result($categories); return (implode("\n",$tmp) . "\n"); } ?> Then both echo examples should work. Ken Quote Link to comment https://forums.phpfreaks.com/topic/231013-when-to-use-echo-to-display-functions-or-definitions/#findComment-1189214 Share on other sites More sharing options...
exhaler Posted March 18, 2011 Share Posted March 18, 2011 basically you’ll want to use echo whenever you want to output something to the browser, and use return when you want to end a function and pass on data to another part of your script. But in your code the second if is not working to i changed it to a return and worked fine: function r_categories() { $categories = '<table style="margin:20px;" width="100%">'; $categories .= '<tr>'; $categories .= '<td>'; $categories = mysql_query('select * from categories'); while ($rowCategory = mysql_fetch_array($categories)) { $categories .= "<a style='line-height:20px;' href='?category=" . $rowCategory['category_id'] . "'>" . $rowCategory['category_name'] . '</a><br>'; } $categories .= '</td>'; $categories .= '</tr>'; $categories .= '</table>'; //mysql_free_result($categories); return $categories; } <?php if(RIGHT_COLUMN_WIDTH != '0') { echo "<td valign='top' width='" . RIGHT_COLUMN_WIDTH . "px'>" . r_categories() . "</td>";} ?> Quote Link to comment https://forums.phpfreaks.com/topic/231013-when-to-use-echo-to-display-functions-or-definitions/#findComment-1189216 Share on other sites More sharing options...
mac007 Posted March 18, 2011 Author Share Posted March 18, 2011 wow, you mean to tell me I've been using functions all wrong? I've been using echo's in them for everything! Is that a definite "no, no"?? All, or most of the darn examples I saw on how to use them show echo being used inside them. That's a bummer! Thanks guys for your help and clarifying this deeper. Gonna have to re-study proper usage of functions all over again! Quote Link to comment https://forums.phpfreaks.com/topic/231013-when-to-use-echo-to-display-functions-or-definitions/#findComment-1189222 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.