Page 1 of 1

functions and such

Posted: Tue Mar 21, 2006 11:04 am
by elecktricity
Just thought id try something earlier, couldnt get it to work, what do you guys think?

Code: Select all

<?PHP
//this dosnt work
function varhtml($var1, $var2) {
	$var1 = htmlentities($var2);
}
varhtml('name', '<b>query1</b>');
echo $name;


//this does
$name = htmlentities('<u>query2</u>');
echo $name;
?>

Posted: Tue Mar 21, 2006 11:14 am
by onion2k
It's a problem of scope. And references.

Code: Select all

function varhtml($var1, $var2) {
    global $$var1;
    $$var1 = htmlentities($var2);
}
varhtml('name', '<b>query1</b>');
echo $name;
That will do what you're trying to do. Horrible way of doing it though. You should be returning the text and assigning it to a variable in the main block.

Posted: Tue Mar 21, 2006 11:19 am
by elecktricity
yea onion that works great, but can you show me an example of what your saying I dont get it

Posted: Tue Mar 21, 2006 11:23 am
by Weirdan
elecktricity wrote: but can you show me an example of what your saying I dont get it

Code: Select all

function varhtml($var2) {
    return htmlentities($var2);
}
$name = varhtml('<b>query1</b>');
echo $name;
and even better (no extra function call):

Code: Select all

$name = htmlentities('<b>query1</b>');
echo $name;