Simple question, please help

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
frostbitten
Forum Newbie
Posts: 3
Joined: Tue Dec 29, 2009 8:35 am

Simple question, please help

Post 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!
frostbitten
Forum Newbie
Posts: 3
Joined: Tue Dec 29, 2009 8:35 am

FIXED!

Post by frostbitten »

FIXED:

Code: Select all

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

Code: Select all

        global $$namevar;
        $$namevar = $_GET[$getvar];
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Simple question, please help

Post 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;
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
frostbitten
Forum Newbie
Posts: 3
Joined: Tue Dec 29, 2009 8:35 am

Re: Simple question, please help

Post 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.
Post Reply