Page 1 of 1

Simple question, please help

Posted: Tue Dec 29, 2009 8:46 am
by frostbitten
I have a script to check if a variable exists via GET, if it does it should set a corresponding name to that variable.


varsetfunc.php?t=test

Code: Select all

<?php
function varset($getvar, $namevar){
    if ( isset($_GET[$getvar]) ) {
        $$namevar = $_GET[$getvar];
        }
    }
varset('t','test');
echo $test;
?>
Simple enough? Well nothing appears and my error.log file records:
Undefined variable: test in C:\\www\\htdocs\\internaltest2\\varsetfunc.php
I am unsure why it does this. Any help, direction would be appreciated. Thanks!

FIXED!

Posted: Tue Dec 29, 2009 9:15 am
by frostbitten
FIXED:

Code: Select all

        $$namevar = $_GET[$getvar];
-became->

Code: Select all

        global $$namevar;
        $$namevar = $_GET[$getvar];

Re: Simple question, please help

Posted: Tue Dec 29, 2009 9:28 am
by AbraCadaver
I don't see the point, but another way rather than using globals:

Code: Select all

function varset($getvar){
    if ( isset($_GET[$getvar]) ) {
       return $_GET[$getvar];
    }
}
$test = varset('t');
echo $test;

Re: Simple question, please help

Posted: Tue Dec 29, 2009 11:44 am
by frostbitten
yeah i thought of that but my solution is more concise. the point is to check if the variable is there or not, keeping warnings away.