Jump to content

function inside function


arunpatal
Go to solution Solved by 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
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.

Link to comment
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.

 

Thanks for quick reply....  am leaning PHP and were doing some experiments :)

Link to comment
Share on other sites

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 by Barand
Link to comment
Share on other sites

  • Solution

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.