arunpatal Posted October 31, 2013 Share Posted October 31, 2013 I want to call display function..... function admin(){ global $admin; function display($display){ $sql = mysql_query("SELECT * FROM $admin") or die (mysql_error()); $result = mysql_fetch_array($sql); echo $result["$display"]; } } I tried to call like this <?php display($username) ?> But its not working Quote Link to comment https://forums.phpfreaks.com/topic/283482-function-inside-function/ Share on other sites More sharing options...
mac_gyver Posted October 31, 2013 Share Posted October 31, 2013 the function being defined inside the other function won't exist until the outer function has been executed. there's rarely a good reason to do this. what is your good reason for doing this? lastly, you have probably seen or been told this in the forum, DO NOT use the global keyword to bring values into your function. it is a sign your structure is wrong. Quote Link to comment https://forums.phpfreaks.com/topic/283482-function-inside-function/#findComment-1456426 Share on other sites More sharing options...
arunpatal Posted October 31, 2013 Author Share Posted October 31, 2013 the function being defined inside the other function won't exist until the outer function has been executed. there's rarely a good reason to do this. what is your good reason for doing this? lastly, you have probably seen or been told this in the forum, DO NOT use the global keyword to bring values into your function. it is a sign your structure is wrong. Thanks for quick reply.... am leaning PHP and were doing some experiments Quote Link to comment https://forums.phpfreaks.com/topic/283482-function-inside-function/#findComment-1456427 Share on other sites More sharing options...
Barand Posted October 31, 2013 Share Posted October 31, 2013 (edited) and if you are just echoing the parameter that was passed to the function, why the SQL query? This would work $db = new mysqli(HOST,USERNAME,PASSWORD,DATABASE); function admin($db) { function username($db) { $sql = "SELECT fullname FROM users WHERE account_type='admin'"; $res = $db->query($sql); if ($row = $res->fetch_assoc()) { $username = $row['fullname']; } else { $username = 'Not found'; } return $username; } return username($db); } echo admin($db); Edited October 31, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/283482-function-inside-function/#findComment-1456428 Share on other sites More sharing options...
Solution arunpatal Posted October 31, 2013 Author Solution Share Posted October 31, 2013 thanks and if you are just echoing the parameter that was passed to the function, why the SQL query? This would work $db = new mysqli(HOST,USERNAME,PASSWORD,DATABASE); function admin($db) { function username($db) { $sql = "SELECT fullname FROM users WHERE account_type='admin'"; $res = $db->query($sql); if ($row = $res->fetch_assoc()) { $username = $row['fullname']; } else { $username = 'Not found'; } return $username; } return username($db); } echo admin($db); I was just trying to put all PHP code in one function file.... So that when i redesign my site layout later.... i don't see many php codes Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/283482-function-inside-function/#findComment-1456436 Share on other sites More sharing options...
mac_gyver Posted October 31, 2013 Share Posted October 31, 2013 putting function definitions in a file is not the same as putting function definitions inside a function. Quote Link to comment https://forums.phpfreaks.com/topic/283482-function-inside-function/#findComment-1456437 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.