How to use a function? - SOLVED

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
matth2004
Forum Commoner
Posts: 40
Joined: Wed Sep 06, 2006 3:26 am

How to use a function? - SOLVED

Post by matth2004 »

Hi,

I have created a function like this:

Code: Select all

function SetHTML() {

$pagetop = "hello this is a title $temptitle goodbye";
$pagebottom = "this is the bottom $temptitle goodbye";
};

And I want to know how to call on it properly. I currently get the $temptitle variable from a MySQL database which has the title of the page. I then call on it so that it can set the variables correctly and the function puts the title that I have already got into the variable for me. When I go to echo $pagetop and $pagebottom outside of the function they are blank. Why is this? Is there something I'm doing wrong?

Thanks in advance,
Matt
Last edited by matth2004 on Sat Nov 11, 2006 4:43 am, edited 1 time in total.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

You need a return value. Perhaps something like this:

Code: Select all

function SetHTML() { 

$pagetop = "hello this is a title $temptitle goodbye"; 
$pagebottom = "this is the bottom $temptitle goodbye"; 

return array('pagetop' => $pagetop, 'pagebottom' => $pagebottom);

}
Then you can assign the function result to a variable.

Code: Select all

$pageinfo = SetHTML();
Then you can use info that that function returned..

Code: Select all

echo $pageinfo['pagetop'];

//

echo $pageinfo['pagebottom'];

Also, you included a ; at the end of your function. Not needed (might be a parse error, not sure).
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Simply read http://be2.php.net/manual/en/language.v ... .scope.php and you'll feel enlightened :)
matth2004
Forum Commoner
Posts: 40
Joined: Wed Sep 06, 2006 3:26 am

Post by matth2004 »

Thanks heaps for your replies and the globals variable worked perfectly! Never would of though of that.

Thanks again,
Matt
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Use the return keyword instead of globals, globals will only result in horrible code smell and unpredictable behavior.
Post Reply