Jump to content

Recommended Posts

Wheres the function.

 

It is all dependent on what you require.

 

Generally if it is a calculation being done a return is good to do. But if it is a string that all you would do after it is returned is print it out than just print it out in the function.

 

You can also print something and return something say this:

 

<?php
function returnAndPrint() {
     print "This function ran!";
     return true;
}

$pass = returnAndPrint();

if ($pass) {
    print "The function printed and returned a value! SWEET!";
}
?>

 

All in all it is up to you to decide what you need.

it depends on what the function is suppose to do and how complex a system the function is a part of.

 

  If the function is simple and its purpose is to generate atomic html then use echo.

ie, if you want to print "hello world" then use echo.

 

If the function is part of more complex system like database systems, Object oriented, and/or implementing the MVC (Model View Controller) architectural pattern then use returns.

ie, complexSQLQuery( param1, param2,param3,param 4)

    {

        //complext code here

        return answer

      }

 

echo is more flexible eg

 

you can do this

<?php
$ar = array (1,2,3);
echo '<pre>', print_r($ar, true), '</pre>';
?>

 

but you can't do

<?php
$ar = array (1,2,3);
print '<pre>', print_r($ar, true), '</pre>';
?>

 

Also, echo is shorter to type.

 

 

I often use echo for output that should be in the script and print for outputting debug values. That way it's easy, when deploying, to search for "print" to remove code that shouldn't be there.

echo is more flexible eg

 

you can do this

<?php
$ar = array (1,2,3);
echo '<pre>', print_r($ar, true), '</pre>';
?>

 

but you can't do

<?php
$ar = array (1,2,3);
print '<pre>', print_r($ar, true), '</pre>';
?>

 

Also, echo is shorter to type.

 

 

I often use echo for output that should be in the script and print for outputting debug values. That way it's easy, when deploying, to search for "print" to remove code that shouldn't be there.

 

I like the debugging idea. I always just used print for no reason really. It just works easier for me, but as far as that second code is concerned, no you cannot pass the data like parameters to print but this would display the same thing =)

 

<?php
$ar = array (1,2,3);
print '<pre>' . print_r($ar, true) . '</pre>';
?>

 

I do understand the differences in concatanation(sp?) and passing with arguments. Just stating that.

 

But I may take the advice and use one for strictly debugging and the other for general use. It beats the coments //REMOVE BEFORE PRODUCTION  I generally use =)

Little off topic, but I like to use something like //START DEBUG and then //END DEBUG if I have a long block of debug stuff, or if I'm OCD about a script cause then I can go back in a text editor or a different php script and just do a regexp replace to remove it all ;p.

 

Corbin->GetOnTopic();

 

I like to use return because if you're using echo/print and you decide you need to use the value later instead of printing it there, it can be a pain....

 

Although sometimes it is a pain to echo out every return from a function, I like it, and really it's only just like 10 more characters ;p.

Not at all, just being informative. This topic was already discussed thoroughly with a few members on the forum found here:

 

http://www.phpfreaks.com/forums/index.php/topic,132729.msg558095.html#msg558095

 

That might help explain some of the differences between echo and print.

In many languages you have procedures (which do something) and functions (which do something then return a value). In PHP you don't have that distinction, just functions. I cannot subscribe the purist view, therefore, that a function must always return something. There's nothing wrong with the "C" void type function, returning no value.

 

In many instances you want to repeat a set of statements at different points in the program, so make it a "procedure" (ie void function).

 

Even if it is called only once, such a procedure can help break up the logic of a program into smaller, manageable chunks.

 

eg

switch ($var) {
    case 'x' :
               do_something();
               break;
    case 'y':
               do_something_else();
               break
}

 

you took what i said to way to literally. I just meant all functions output should be a return value as opposed to echo. I never ever meant functions need to have output, but if they do it should a be a return value unless it is something like var_dump or print_r.

 

The reasons I say what I say is not because of personal preference, something like this is not personal preference if you continue to echo stuff you will find you have way less control over your programs output and depending on what you are doing it will come back to bite you.

 

What I have said is not personal preference, people who use echo all the time will have major design issues and the only way they will be able to work around them will be to use output buffering literally everywhere. This is why I think it is bad to use echo's everywhere.

 

I understand what nameless is saying and looking back at my code I never echo anything via a function anymore. It is always returned as an array or object or string to pass to my "templating" function which than uses that.

 

I would have to agree with that statement Nameless said. It gives you full control of your output =)

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.