Jump to content

Function Echo Vs. Return


JakkkeM

Recommended Posts


Hi Guys,

 

I'm an amateur coder working on a huge project probably way above my capabilities and thus continue to run into problems. So, I seek your help!

 

In my developing system, I'm making a dynamic website so you can control the front-end from the back-end.

 

To edit the front-end you have a page Manage Layout which allows you to edit the HTML of the home page. Then you just type in my custom elements like, [menu],


etc.

 

I'm using preg_replace to change these into the scripts I require.

 

The HTML of the homepage is contained in a database, then when it's executed on the homepage I do the preg replace etc.

 

What I'm doing for my [menu] is:

 

KKLv5.png

 

Now, the problem occurs. It executes this 100%, the preg_replace carries it out perfectly, BUT instead of it executing where I've placed it in the overall position on the page, it puts everything on the first line after the <body> tag...

 

The preg_replace simply replaces [menu] with createMenu(); so I would've thought that my function would be executed wherever I had [menu]???

 

I'm not entirely sure what I've said is clear but lets hope so.

 

Cheers.

 

kKfWB.png

 

ABOVE:The menu is being placed on the first line, rather than in the div where I have [menu] (which is consequently to the right of the CrossFit St Marys Logo)

Link to comment
Share on other sites

Another advantage to using return over echo is that you can arrange your functions however you like, without affecting the structure of the site. That means you can also change the structure of your site, without having to re-arrange your functions.

 

For instance: If you found out you wanted a bread crumb on your page, and that it should go before the menu. However, the breadcrumb is relying upon some of the processing you've done in relation with the menu. If you've used echo inside the functions, you either have to duplicate that bit of code (which greatly increases the probability of bugs), move the breadcrumb to be after the menu, or do position magic with CSS to make it appear as if it was before the menu.

With return, you just create the breadcrumb after the menu has been created, and then echo them out in their respective places in the code.

 

Also, I see that you've used a query inside a loop in your code. This is strongly recommended against as it hurts performance like nothing else, and on top of that quite unnecessary. What you should do instead is to use one query with a JOIN statement, to fetch all of the data you need the first time around. Then loop through the result, and do whatever you want to do.

 

PS: Please post your code as text, and use the [code][/code] tags around your code. Doing both helps make both your post and your code a lot easier to read.

Edited by Christian F.
Link to comment
Share on other sites

You could use classes for this as that allows you to keep the identifier '[menu]' together with the HTML.

 

class MenuGeneratorView {
  public function generate() {
  }
  public function getIdentifier() {
    return 'menu';
  }
}

 

echo str_replace('['.$view->getIdentifier().']', $view->generate(), $str);

 

Something else you can do is parse each [<identifier>] so that you allow parameters for example.

 

[menu do="generate" direction="vertical" sort="asc"]

 

public function generate($direction = 'horizontal', $sort = 'desc') {
  ..
}

 

[menu do="breadcrumb" direction="horizontal" sort="asc"]

 

public function breadcrumb($direction = 'horizontal', $sort = 'asc') {
  ..
}

Edited by ignace
Link to comment
Share on other sites

Another advantage to using return over echo is that you can arrange your functions however you like, without affecting the structure of the site. That means you can also change the structure of your site, without having to re-arrange your functions.

 

For instance: If you found out you wanted a bread crumb on your page, and that it should go before the menu. However, the breadcrumb is relying upon some of the processing you've done in relation with the menu. If you've used echo inside the functions, you either have to duplicate that bit of code (which greatly increases the probability of bugs), move the breadcrumb to be after the menu, or do position magic with CSS to make it appear as if it was before the menu.

With return, you just create the breadcrumb after the menu has been created, and then echo them out in their respective places in the code.

 

Also, I see that you've used a query inside a loop in your code. This is strongly recommended against as it hurts performance like nothing else, and on top of that quite unnecessary. What you should do instead is to use one query with a JOIN statement, to fetch all of the data you need the first time around. Then loop through the result, and do whatever you want to do.

 

PS: Please post your code as text, and use the


tags around your code. Doing both helps make both your post and your code a lot easier to read.
[/quote]

The problem I face if I use a JOIN statement, is that for every link, it does the button again?

echo "<h1>Test Page</h1>";


$query = mysql_query("SELECT * FROM `menus` LEFT JOIN `links` ON `menus`.`menu_id` = `links`.`assoc_menu`");


while($MENU = mysql_fetch_array($query, MYSQL_ASSOC)){
  $return_menu = $MENU['title']." ".$MENU['link_title']."<br />";  
  echo $return_menu;  
}

 

But now, for every link, it returns the title that link belongs too...

 

So I'm not sure how to handle this because if I have 10 links for my first menu item, I'll have 10 buttons for that item?

Link to comment
Share on other sites

You could use classes for this as that allows you to keep the identifier '[menu]' together with the HTML.

 

class MenuGeneratorView {
 public function generate() {
 }
 public function getIdentifier() {
   return 'menu';
 }
}

 

echo str_replace('['.$view->getIdentifier().']', $view->generate(), $str);

 

Something else you can do is parse each [<identifier>] so that you allow parameters for example.

 

[menu do="generate" direction="vertical" sort="asc"]

 

public function generate($direction = 'horizontal', $sort = 'desc') {
 ..
}

 

[menu do="breadcrumb" direction="horizontal" sort="asc"]

 

public function breadcrumb($direction = 'horizontal', $sort = 'asc') {
 ..
}

 

Unfortunately, and this is where the amateur part comes in, I don't know how to do anything you suggested. Haha. :sweat:

Link to comment
Share on other sites

Another advantage to using return over echo is that you can arrange your functions however you like, without affecting the structure of the site. That means you can also change the structure of your site, without having to re-arrange your functions.

 

For instance: If you found out you wanted a bread crumb on your page, and that it should go before the menu. However, the breadcrumb is relying upon some of the processing you've done in relation with the menu. If you've used echo inside the functions, you either have to duplicate that bit of code (which greatly increases the probability of bugs), move the breadcrumb to be after the menu, or do position magic with CSS to make it appear as if it was before the menu.

With return, you just create the breadcrumb after the menu has been created, and then echo them out in their respective places in the code.

 

Also, I see that you've used a query inside a loop in your code. This is strongly recommended against as it hurts performance like nothing else, and on top of that quite unnecessary. What you should do instead is to use one query with a JOIN statement, to fetch all of the data you need the first time around. Then loop through the result, and do whatever you want to do.

 

PS: Please post your code as text, and use the


tags around your code. Doing both helps make both your post and your code a lot easier to read.

 

The problem I had with return, was that instead of giving me all of my menus, it only returned the first one... :/ I'll test it with the new SQL format and see how I go.

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.