Jump to content

function inside function


arunpatal

Recommended Posts

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

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.

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 :)

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);

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 :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.