strategos Posted April 26, 2012 Share Posted April 26, 2012 Hello, I am trying to define some variables in a function, but the variable just returns null no matter what I do. Could someone please tell me what I'm doing wrong? <?php function checkname() { global $t; if($_SESSION['name'] == '') { return false; $t = false; } elseif(isset($_POST['cont']) && $_SESSION['name'] != '') { return true; $t = true; } } var_dump($t); // returns NULL upon correct or incorrect submit. ?> Quote Link to comment https://forums.phpfreaks.com/topic/261654-set-variables-in-a-function/ Share on other sites More sharing options...
rythemton Posted April 26, 2012 Share Posted April 26, 2012 Your problem is the 'return's. Functions stop once they return something. The second problem is that you really shouldn't be using globals. Instead: $t = checkname(); That will capture the return value. Quote Link to comment https://forums.phpfreaks.com/topic/261654-set-variables-in-a-function/#findComment-1340780 Share on other sites More sharing options...
PFMaBiSmAd Posted April 26, 2012 Share Posted April 26, 2012 That's not how functions should produce values and make them available to the code that calls the function. Please read my replies in the following thread - http://www.phpfreaks.com/forums/index.php?topic=358171.0 Quote Link to comment https://forums.phpfreaks.com/topic/261654-set-variables-in-a-function/#findComment-1340781 Share on other sites More sharing options...
strategos Posted April 26, 2012 Author Share Posted April 26, 2012 Thank's for your quick replies. Ok I get it now. I can declare the function as a variable. That works too <?php function checkname() { if($_SESSION['name'] == '') { return false; } elseif(isset($_POST['cont']) && $_SESSION['name'] != '') { return true; } } $t = checkname(); var_dump($t); ?> Quote Link to comment https://forums.phpfreaks.com/topic/261654-set-variables-in-a-function/#findComment-1340784 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.