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 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. 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 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 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); Link to comment https://forums.phpfreaks.com/topic/283482-function-inside-function/#findComment-1456428 Share on other sites More sharing options...
arunpatal Posted October 31, 2013 Author 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 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. Link to comment https://forums.phpfreaks.com/topic/283482-function-inside-function/#findComment-1456437 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.