user2854563
user2854563

Reputation: 268

Getting Vars Value From Function

I'm using this function;

function forums_fid()
{
    if (!defined('FORUMS_FID'))
    {
        $maktaba_fid = '14';
        $ask_question_fid = '9';
    }
    define('FORUMS_FID', 1);
}

I want to use variable's values in an another function, so for this I'm trying to use this code;

forums_fid();
$maktaba = '<a href="www.domain.com/forumdisplay.php?fid='.$maktaba_fid.'"><img src="./images/maktaba.png" alt="" title=""></a>';
$ask_question = '<a href="www.domain.com/newthread.php?fid='.$ask_question_fid.'"><img src="./images/ask-question.png" alt="" title=""></a>';

Bur unfortunately the variables are empty in this second code.

Please help!

Upvotes: 3

Views: 47

Answers (3)

Charlotte Dunois
Charlotte Dunois

Reputation: 4680

Forget what I said before. What you can do is define two empty variables (NULL) and declare them as global in the function, overwrite them with whatever you want and then use them. By the way, you have to put define() inside the if-clausel or PHP would throw an error.

function forums_fid()
{
    global $maktaba_fid, $ask_question_fid;

    if (!defined('FORUMS_FID'))
    {
        define('FORUMS_FID', 1);
        $maktaba_fid = '14';
        $ask_question_fid = '9';
    }
}

$maktaba_fid = NULL;
$ask_question_fid = NULL;
forums_fid();
$maktaba = '<a href="www.domain.com/forumdisplay.php?fid='.$maktaba_fid.'"><img src="./images/maktaba.png" alt="" title=""></a>';
$ask_question = '<a href="www.domain.com/newthread.php?fid='.$ask_question_fid.'"><img src="./images/ask-question.png" alt="" title=""></a>';

That's one way. The better way would be to actually return the values (as an array for example).

function forums_fid()
{
    if (!defined('FORUMS_FID'))
    {
        define('FORUMS_FID', 1);
        return array('maktaba_fid' => 14, 'ask_question_fid' => 9);
    }
}

$return = forums_fid();
$maktaba = '<a href="www.domain.com/forumdisplay.php?fid='.$return['maktaba_fid'].'"><img src="./images/maktaba.png" alt="" title=""></a>';
$ask_question = '<a href="www.domain.com/newthread.php?fid='.$return['ask_question_fid'].'"><img src="./images/ask-question.png" alt="" title=""></a>';

Upvotes: 3

Parag Tyagi
Parag Tyagi

Reputation: 8960

I would recommend the $GLOBALS super variable.

function forums_fid()
{
    if (!defined('FORUMS_FID'))
    {
        $GLOBALS['maktaba_fid'] = '14';
        $GLOBALS['ask_question_fid'] = '9';
    }
    define('FORUMS_FID', 1);
}

forums_fid();
echo $GLOBALS['maktaba_fid'].PHP_EOL;
echo $GLOBALS['ask_question_fid'];


DEMO:

http://3v4l.org/k7TVA


Documentation:

http://php.net/manual/en/language.variables.scope.php

Upvotes: 1

Satish Sharma
Satish Sharma

Reputation: 9635

try this

function forums_fid()
{
    global $maktaba_fid;
    global $ask_question_fid;
    if (!defined('FORUMS_FID'))
    {
        $maktaba_fid = '14';
        $ask_question_fid = '9';
        define('FORUMS_FID', 1);
    }

}

Upvotes: 1

Related Questions