/    Sign up×
Community /Pin to ProfileBookmark

Function called when defined

I have the following in [b]tester.php[/b]:

[code]<?php
function myfunction(){ print ‘HELLO<br>’; }
print ‘LINE 1<br>LINE 2<br>’.myfunction().’LINE 3<br>’;
?>[/code]

This is what I expect to see:

[quote]

LINE 1
LINE 2
HELLO
LINE 3

[/quote]

But instead I get this:

[quote]

HELLO
LINE 1
LINE 2
LINE 3

[/quote]

It seems that my function is being called when defined instead of when I call it between my lines 2 and 3.

Does anyone have an idea of what could be causing this?

Thanks,
Marcus

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@bionoidFeb 08.2012 — It's because the function is printing the text instead of returning it to append to the string:

[CODE]<?php
function myfunction(){ [COLOR="Red"]return[/COLOR] 'HELLO<br>'; }
print 'LINE 1<br>LINE 2<br>'.myfunction().'LINE 3<br>';
?>[/CODE]
Copy linkTweet thisAlerts:
@marcus2769authorFeb 08.2012 — It seems that when called, the function executes where it is [i]defined[/i] as opposed to where it is [i]called[/i] (see example below). Is this a new behavior in PHP 5?
&lt;?php
function myfunction(){ print 'HELLO&lt;br&gt;'; }
print 'LINE 1&lt;br&gt;LINE 2&lt;br&gt;'.myfunction().'LINE 3&lt;br&gt;'.myfunction().'LINE 4&lt;br&gt;'.myfunction().'LINE 5&lt;br&gt;';
?&gt;

Produces:
HELLO

HELLO

HELLO

LINE 1

LINE 2

LINE 3

LINE 4

LINE 5[/quote]

Sure I could make my functions return what I want printed, but that limits their usefulness quite dramatically. For example, for years I have been using a function defined in its own PHP file to handle user login. The function prints a login form, I require() the file on multiple pages throughout my site, and then call the function where I want the login form to appear on any particular page. This seems like a more logical behavior than having the function execute where it is defined, regardless of where it is called.

Thanks,

Marcus
Copy linkTweet thisAlerts:
@bionoidFeb 08.2012 — The results you are getting are correct because you are concatenating the strings together and executing the functions during that process. So the output of the function calls will appear first.

Alternatively you can use the echo command and specify your strings as parameters instead (,):

[CODE]<?php
function myfunction(){ print 'HELLO<br>'; }
[COLOR="Red"]echo [/COLOR]'LINE 1<br>LINE 2<br>'[COLOR="red"],[/COLOR]myfunction()[COLOR="Red"],[/COLOR]'LINE 3<br>'[COLOR="red"],[/COLOR]myfunction()[COLOR="red"],[/COLOR]'LINE 4<br>'[COLOR="red"],[/COLOR]myfunction()[COLOR="red"],[/COLOR]'LINE 5<br>';
?>[/CODE]


output

[CODE]LINE 1
LINE 2
HELLO
LINE 3
HELLO
LINE 4
HELLO
LINE 5[/CODE]
Copy linkTweet thisAlerts:
@NogDogFeb 08.2012 — 
...

Sure I could make my functions return what I want printed, but that limits their usefulness quite dramatically....[/QUOTE]


Actually, in general, the opposite is true. By having a function directly output to the client, you [i]limit[/i] its re-usability, since it becomes a pain to use in any situation where you [i]don't[/i] want it to output directly to the client, as in the situation you have run into here where things are much cleaner / simpler if you have it return its value.

As a general rule of thumb, I recommend that all functions should return by default rather than outputting, unless you have a good reason for it to directly output something.
Copy linkTweet thisAlerts:
@marcus2769authorFeb 08.2012 — Correct you are. Concatenation was my problem, although I solved it this way:
&lt;?php
function myfunction(){ print 'HELLO&lt;br&gt;'; }
print 'LINE 1&lt;br&gt;LINE 2&lt;br&gt;'; myfunction(); print 'LINE 3&lt;br&gt;';
?&gt;

This is how I originally called all my functions because they all printed directly and did not return. After a while I started utilizing [b]return[/b] for inline functions for things like quick and easy formatting of times and dates. When I returned to my login function I called it the way I have gotten used to with inline functions, and completely overlooked this rather obvious difference in behavior.

Thanks for your help bionoid ?.

Marcus

EDIT:
As a general rule of thumb, I recommend that all functions should return by default rather than outputting, unless you have a good reason for it to directly output something.[/QUOTE]
I have been migrating more and more to functions that return rather than print directly (like my above example with times and dates). I had a bit of tunnel vision since I was focused on the login function I described earlier though. That function serves a specific purpose so it's handy to print directly. The main reason for defining it as a function is to allow for easy access across different pages.
×

Success!

Help @marcus2769 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...