Page 1 of 1

calling a function from another function

Posted: Tue Jan 26, 2010 11:49 am
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.

Re: calling a function from another function

Posted: Tue Jan 26, 2010 11:55 am
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

Re: calling a function from another function

Posted: Wed Jan 27, 2010 5:48 am
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

Re: calling a function from another function

Posted: Wed Jan 27, 2010 6:33 am
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;