Jump to content

when to use echo to display Functions or Definitions??


mac007

Recommended Posts

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>";} ?>

Link to comment
Share on other sites

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);
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>";} ?>

Link to comment
Share on other sites

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!

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.