calling a function from another function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
krisbadsg
Forum Newbie
Posts: 4
Joined: Thu Jun 25, 2009 4:53 am

calling a function from another function

Post by krisbadsg »

i got a problem while calling a function from another function. the code runs like this.

function get_all($agcode){
global $connection;
$query="SELECT * FROM agdb WHERE agentcode={$agcode}";
$result=mysql_query($query, $connection);
confirm_query($result);
while($row=mysql_fetch_array($result)){
$agent_name=$row['agentname'];
$agent_eligible_code=$row['agencyeligcode'];
$dobyear=$row['dobyear'];
$dobmonth=$row['dobmonth'];
$dobdate=$row['dobdate'];
$pannumber=$row['pannumber'];
$dev_off_code=$row['devoffcode'];
}
}

function get_age($agcode){
get_all($agcode);
$dob=$dobmonth ."/". $dobdate."/".$dobyear;
return $dob;
}

and the error message is there is an undifined variables called $dobmonth, $dobdate, $dobyear for linenumbers corresponding to second function. Hence Please help me where i have gone wrong.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: calling a function from another function

Post by requinix »

To pass information into a function you should use arguments.
To pass information out of a function you should use return values.

That's it. Beyond that, variables in one function do not magically appear in another function.
PHP manual on functions
krisbadsg
Forum Newbie
Posts: 4
Joined: Thu Jun 25, 2009 4:53 am

Re: calling a function from another function

Post by krisbadsg »

tasairis wrote:To pass information into a function you should use arguments.
To pass information out of a function you should use return values.

That's it. Beyond that, variables in one function do not magically appear in another function.
PHP manual on functions

thank s for quick reply
i passed the arguments to both the functions, and my question is we are calling the first function from within the second function and the the variables extracted from the database whether or not become known to the second function.

in one way i can achieve the task. just copying the same bit of code in the first function and paste it in the second. but doing so i am defeating the concept of function and code re usability. hence please advise me my friend how to achieve the task of using the variables extracted in the first function in the second
nothing1306
Forum Newbie
Posts: 4
Joined: Tue Jan 26, 2010 6:18 am

Re: calling a function from another function

Post by nothing1306 »

OOP :crazy:

Code: Select all

class class_name {
    public $dobmonth;
    public $dobdate;
    public $dobyear;
 
    function get_all($agcode){
        global $connection;
        $query="SELECT * FROM agdb WHERE agentcode={$agcode}";
        $result=mysql_query($query, $connection);
        //confirm_query($result);
        while($row=mysql_fetch_array($result)){
            $agent_name=$row['agentname'];
            $agent_eligible_code=$row['agencyeligcode'];
            $this->dobyear=$row['dobyear'];
            $this->dobmonth=$row['dobmonth'];
            $this->dobdate=$row['dobdate'];
            $pannumber=$row['pannumber'];
            $dev_off_code=$row['devoffcode'];
        }
    }
 
    function get_age($agcode){
        $this->get_all($agcode);
        $dob=$this->dobmonth ."/". $this->dobdate."/".$this->dobyear;
        return $dob;
    }
}
 
$obj = new class_name();
$f_get_age = $obj->get_age(1);
echo "<pre>";
print_r($f_get_age);
echo "</pre>";
echo $obj->dobmonth;
echo "<br/>";
echo $obj->dobdate;
echo "<br/>";
echo $obj->dobyear;
Post Reply