/    Sign up×
Community /Pin to ProfileBookmark

variable = function?

Hi,

i have a very simple question. Im trying to have a variable get the same value a defined function produces. For example, if i put somewhere in the code this

[CODE]title();[/CODE]

it will automatically print the title for that page like this

[CODE]Home[/CODE]

Now, i want a variable to have the value that function generates, so something like this:

[code=php]$title = title();[/code]

This doesnt seem to work because instead of defining the variable the function is executed. Could anybody please tell me how to do it?

Thank you.

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@MindzaiJun 04.2010 — Have your function return the value instead of echoing it. This is good practice as it is more flexible. You can either use:

[code=php]echo title();[/code]

or

[code=php]$variable = title();[/code]
Copy linkTweet thisAlerts:
@supercainauthorJun 04.2010 — Sorry, i dont get it. I had already tried with this

$title = title();

as you suggest but it doesnt work because it echoes the value intead of defining the variable. I cant modify the function itself because i didnt make it so i dont know where is it and how to modify it.

So i cant just have a variable that equals the value echoed by the function?
Copy linkTweet thisAlerts:
@criterion9Jun 04.2010 — You can collect the output from a buffer....though this is really a highly inefficient way to handle it...

http://php.net/manual/en/function.ob-start.php
Copy linkTweet thisAlerts:
@NedalsJun 04.2010 — Not a PHP expert, but I'll guess...

The function title probably looks like this..

function title() {

echo 'Home';

}

so when you run it, it prints 'Home'

What you need is...

function title() {

return 'Home';

}

Now you can write, $variable = title(); and print the $variable.
Copy linkTweet thisAlerts:
@MindzaiJun 04.2010 — Sorry, i dont get it. I had already tried with this

$title = title();

as you suggest but it doesnt work because it echoes the value intead of defining the variable. I cant modify the function itself because i didnt make it so i dont know where is it and how to modify it.

So i cant just have a variable that equals the value echoed by the function?[/QUOTE]


That was just an example of how you will be able to do what you want after you fix your function. ? You need to have your function return a value before you can use it in this way. Information on how to do this here. If you don't know where it is defined, track it down by following the includes, ore use your IDE to search all the files for the string "function title". Otherwise you will have to use output buffering as mentioned, but that is a horrible, horrible hack just to get around having to track down the function definition.
Copy linkTweet thisAlerts:
@tirnaJun 04.2010 — Sorry, i dont get it. I had already tried with this

$title = title();

as you suggest but it doesnt work because it echoes the value intead of defining the variable. I cant modify the function itself because i didnt make it so i dont know where is it and how to modify it.

So i cant just have a variable that equals the value echoed by the function?[/quote]


You need to some how edit the code in title() to something like this:

[code=php]
function title() {
$str = 'Home';
return $str;
}

$variable = title();

//the title() function will now 'return' the value of $str to the function calling statement
//and be assigned to the variable $variable
[/code]
×

Success!

Help @supercain 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.18,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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