0

I have a problem in my PHP code. I got an array like this as example ( this array is created dynamic with data from a database).

$db_res =array('debiteur_id'=>'1020','user_id'=>'495','b2b_or_b2c'=>'B2C');

When I show the values with

foreach ($db_res as $key => $value )
{
   echo $key.' - '.$value.' , ';
}

It is displaying these values what is OK

debiteur_id - 1020 , user_id - 495 , b2b_or_b2c - B2C

Now I have a function to test if some data in the array is set or not and to return some values from this $db_res array of $_POST array

    function isnull_post($naam)
    {
        if (isset($db_res[$naam]))
            return($db_res[$naam]);
        else
        {
            if (isset($_POST[$naam]))
                return($_POST[$naam]);
            else
                return('');
        }
    }

When I use the following code to show my array

    foreach ($db_res as $key => $value )
    {
        echo $key.' - '.$value.' , ';
        $val = isnull_post($key);
        echo ('isnull : '.$val.' , ');
    }

This is my output

debiteur_id - 1020 , isnull : , user_id - 495 , isnull : , b2b_or_b2c - B2C , isnull :

What am I doing wrong?

6
  • 3
    $db_res is not accessible in your function isnull_post you need to pass it along with the key. Commented May 1, 2014 at 5:49
  • 1
    or you can use global $db_res; as first line in your function isnull_post Commented May 1, 2014 at 5:51
  • What about the $db_res value!? which you have written in the function!? Is it global variable!? Just try to print that value in that function so that you came to know if it will return the value or not!? What array it will print let me know. Commented May 1, 2014 at 5:53
  • 1
    Please do not do what Amit has suggested Commented May 1, 2014 at 5:53
  • Hello Phil, Why not doing what Amit suggested, that is the answer that does what I expected Commented May 1, 2014 at 6:19

4 Answers 4

1

You need to do some changes in isnull_post() function as your $db_res array scope is not global. So make it global in the function where you want to use it as a global variable.

function isnull_post($naam)
    {   
            global $db_res;

            if (isset($db_res[$naam]))
                return($db_res[$naam]);
            else
            {
                if (isset($_POST[$naam]))
                    return($_POST[$naam]);
                else
                    return('');
            }
    }

You can also do it by passing the array to the function as :

 $db_res =array('debiteur_id'=>'1020','user_id'=>'495','b2b_or_b2c'=>'B2C');

    function isnull_post($naam,$db_res)
        {   
            if (isset($db_res[$naam]))
            return($db_res[$naam]);
            else
            {
                if (isset($_POST[$naam]))
                    return($_POST[$naam]);
                else
                    return('');
            }
        }

        foreach ($db_res as $key => $value )
        {   
            $val = isnull_post($key,$db_res);
            echo $key.' - '.$val.' , ';
        }

if your requirement for using this array is only for this function,then you can pass it to the function but if other functions requires this array then you have to define the array global to that function's body scope.

Sign up to request clarification or add additional context in comments.

Comments

0

Use Below code:

$db_res =array('debiteur_id'=>'1020','user_id'=>'495','b2b_or_b2c'=>'B2C');
function isnull_post($naam,$db_res)
{   
    if (isset($db_res[$naam]))
    return($db_res[$naam]);
    else
    {
        if (isset($_POST[$naam]))
            return($_POST[$naam]);
        else
            return('');
    }
}

foreach ($db_res as $key => $value )
{   
    $val = isnull_post($key,$db_res);
    echo $key.' - '.$val.' , ';
}

Comments

0

As already is commented, the variable $db_res isn't accessible in your function, you either make it a global (which I don't prefer), or you pass it on, like this:

<?php
    function isnull_post($naam, $db_res)
    {
        if (isset($db_res[$naam]))
            return($db_res[$naam]);
        else
        {
            if (isset($_POST[$naam]))
                return($_POST[$naam]);
            else
                return('');
        }
    }


    foreach ($db_res as $key => $value )
    {
        echo $key.' - '.$value.' , ';
        $val = isnull_post($key, $db_res);
        echo ('isnull : '.$val.' , ');
    }
?>

11 Comments

so is $_POST here also an array ??
@CodeLover "* $_POST: An associative array of variables passed to the current script via the HTTP POST method. This is a 'superglobal', or automatic global, variable. This simply means that it is available in all scopes throughout a script. There is no need to do global $variable; to access it within functions or methods. " - php.net
yes i got that ..but i see like $_post['name'] .but whats the use of $_post[$var] ??...
It's bases on the code of the TS, if the function is called like this isnull_post('user_id') the function will first check if $db_res['user_id'] is set, if not it will check if $_POST['user_id'] is set. What the use of this function is, I don't know... that you ask the TS. Is that what you want to know? Or are you pointing on something else?
am asking something else ..i just need to know why ($_POST[$naam])) is added here ..
|
0

You can write any of the way,

        $db_res =array('debiteur_id'=>'1020','user_id'=>'495','b2b_or_b2c'=>'B2C');
        function isnull_post($naam)
        {   
            global $db_res;
            if (isset($db_res[$naam]))
            return($db_res[$naam]);
            else
            {
                if (isset($_POST[$naam]))
                    return($_POST[$naam]);
                else
                    return('');
            }
        }

        foreach ($db_res as $key => $value )
        {   
            $val = isnull_post($key);
            echo $key.' - '.$val.' , ';
        }

Or as the following Steps..

            $db_res =array('debiteur_id'=>'1020','user_id'=>'495','b2b_or_b2c'=>'B2C');
        function isnull_post($naam,$db_res)
        {   
            if (isset($db_res[$naam]))
            return($db_res[$naam]);
            else
            {
                if (isset($_POST[$naam]))
                    return($_POST[$naam]);
                else
                    return('');
            }
        }

        foreach ($db_res as $key => $value )
        {   
            $val = isnull_post($key,$db_res);
            echo $key.' - '.$val.' , ';
        }

But I prefer to use first one.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.