...

View Full Version : variable in a function



LoRd_aLmIghTy
12-21-2006, 06:07 PM
i have a function and i have it running on a page, but i set a value for a variable in the function and i want the variable to be able to be used for the page not just in the function. how do i do this? thanks

mlseim
12-21-2006, 06:19 PM
Difficult to explain because there are many aspects to this, but this page tells it all:

http://www.php.net/manual/en/language.functions.php

See the part about returning values.
http://www.php.net/manual/en/functions.returning-values.php


... and you'll learn a lot more too.

Brandoe85
12-21-2006, 06:48 PM
I also think taking a look at variable scope will help you as well:
http://us3.php.net/global

good luck.

Tyree
12-21-2006, 07:07 PM
The short answer to that particular scenario (if I understand it correctly) is to "return" the variable.

Is the value of that one variable the result of the function or just one small piece of what it does?

LoRd_aLmIghTy
12-21-2006, 08:29 PM
my function is

<?php
function user_level()
{
if ($_SESSION['lvl']
{
$lvl = $_SESSION['lvl'];
}
else
{
$lvl = "user"
}
}
?>

and now i want to use the $lvl variable to show the lvl of each user for my forum. i dont see how the return will hlp

GJay
12-21-2006, 09:49 PM
function user_level() {
if (isset($_SESSION['lvl'])) {
$lvl = $_SESSION['lvl'];
}
else {
$lvl = "user"
}
return $lvl;
}
$level = user_level();
echo $level;

Something like that?

Tyree
12-21-2006, 09:50 PM
That's what I'm screamin'! ;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum