xiao Posted April 18, 2008 Share Posted April 18, 2008 Do you have to send POST and SESSION variables to functions? Like: function something($postvar,$sessionvar){ echo $postvar . " " . $sessionvar; } Or can a function read those anyway, like this? function something(){ echo $_POST['var1'] . " " . $_SESSION['var2']; } Link to comment https://forums.phpfreaks.com/topic/101762-solved-small-question-about-functions/ Share on other sites More sharing options...
p2grace Posted April 18, 2008 Share Posted April 18, 2008 $_SESSION variables you can read inside the function, I believe you have to send $_POST vars through as parameters. Link to comment https://forums.phpfreaks.com/topic/101762-solved-small-question-about-functions/#findComment-520657 Share on other sites More sharing options...
rhodesa Posted April 18, 2008 Share Posted April 18, 2008 All 'magic variables' like $_POST, $_SESSION, $_GET, and $_SERVER are automatically global and can be access directly from inside functions. Link to comment https://forums.phpfreaks.com/topic/101762-solved-small-question-about-functions/#findComment-520659 Share on other sites More sharing options...
xiao Posted April 18, 2008 Author Share Posted April 18, 2008 ok, thanks Link to comment https://forums.phpfreaks.com/topic/101762-solved-small-question-about-functions/#findComment-520660 Share on other sites More sharing options...
Barand Posted April 18, 2008 Share Posted April 18, 2008 $_POST, $_GET, etc are superglobals and are available anywhere, even inside functions. However, it is better to write functions with arguments - it makes them more re-usable. With your first function you could something ('foo', 'bar'); // --> foo bar but your second is limited to only those post and session values. Link to comment https://forums.phpfreaks.com/topic/101762-solved-small-question-about-functions/#findComment-520661 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.