0

is it possible to call a function inside function?. I have looking for a solution here. But honestly i don't know how to implement it to my code.

This is my first learn to write an advance PHP code from scratch, please help me to fix my code.

here is my code

<?php
function postdata($datatype){
    if ($datatype == "processbalance"):
        $site           = "ADJUST BALANCE";
        $signature      = sha1($transactId.$cardNo.$amount.$transactionDate.$merchantID.$site.$apikey);
        $appSignature   = sha1($transactId.$cardNo.$amount.$transactionDate.$merchantID.$site.$signature.$appkey);
        $params         = array('transactId'=>$transactId,'cardNo'=>$cardNo,'amount'=>$amount,'transactionDate'=>$transactionDate,'partnerID'=>$merchantID,'site'=>$site,'signature'=>$signature,'appSignature'=>$appSignature,'submit'=>$submit);
    elseif ($datatype == "processreward"):
        $site           = "ADJUST POINT";
        $signature      = sha1($transactId.$cardNo.$reward.$transactionDate.$merchantID.$site.$apikey);
        $appSignature   = sha1($transactId.$cardNo.$reward.$transactionDate.$merchantID.$site.$signature.$appkey);
        $params         = array('transactId'=>$transactId,'cardNo'=>$cardNo,'reward'=>$reward,'items'=>$items,'transactionDate'=>$transactionDate,'merchantId'=>$merchantID,'site'=>$site,'signature'=>$signature,'appSignature'=>$appSignature,'submit'=>$submit);
    elseif ($datatype == 'processdeactivate'):
        $site           = "DEACTIVATE CARD";
        $status         = "False";
        $signature      = sha1($transactId.$oldnbc.$status.$status.$transactionDate.$merchantID.$site.$apikey);
        $appSignature   = sha1($transactId.$oldnbc.$status.$status.$transactionDate.$merchantID.$site.$signature.$appkey);
        $params         = array('transactId'=>$transactId,'cardNo'=>$oldnbc,'status'=>$status,'isGold'=>$status,'merchantDate'=>$transactionDate,'merchantId'=>$merchantID,'site'=>$site,'signature'=>$signature,'appSignature'=>$appSignature,'submit'=>$submit);
    endif;

    $encoded_params = array();

    foreach ($params as $k => $v){
        $encoded_params[] = urlencode($k).'='.urlencode($v);
    }

    $url = $url.implode('&', $encoded_params);
    $arrContextOptions = array("ssl"=>array("verify_peer"=>false,"verify_peer_name"=>false,),); 
    $url = file_get_contents($url, false, stream_context_create($arrContextOptions));
    $getstatus = json_decode($url, true);

    if ($getstatus['Status'] == 1):
        echo $site." Succedd!";
    else:
        echo $site." Failed!";
    endif;
}

function nbchd($type,$cardNo,$amount,$reward,$oldnbc){
    if ($type == "migration" || $type == "adjustment"):
        if ($amount != ""):
            postdata("processbalance");
            echo $result;
        endif;
        if ($reward != ""):
            postdata("processreward");
            echo $result;
        endif;
    elseif ($type == "replacement"):
        if ($oldnbc != ''):
            //post balance&reward to new nbchd
            if ($amount != ""):
                postdata("processbalance");
                echo $result;
            endif;
            if ($reward != ""):
                postdata("processreward");
                echo $result;
            endif;

            //deactivating oldnbc
            $cardNo = $oldnbc;
            if ($amount != ""):
                postdata("processbalance");
                echo $result;
            endif;
            if ($reward != ""):
                postdata("processreward");
                echo $result;
            endif;
            postdata('processdeactivate');
            echo $result;
        else:
            echo "Deactivating Error, Please fill OldNBC Number to continue";
        endif;
    endif;
}

date_default_timezone_set("Asia/Jakarta");
$apikey         = "8ECDD8C5DF4647F0E6EB8CDEEC836E7DA8BACE5A3677424B";
$appkey         = "27D5C651BBC94AE19BDF4F5E2B542667AE7C6EAE62D3BA34";
$transactId     = "";
$url            = "https://192.168.0.39:444/merchantwebapi/api/merchant?";
$submit         = "Invoke";
$merchantDate   = date("Y-m-d H:i:s", time());
$transactionDate= $merchantDate;
$merchantID     = "BLITZ";
$merchantId     = $merchantID;

$type   = "adjustment";
$cardNo = "6048180000000017";
$amount = "10";
$reward = "1";
$oldnbc = "";

nbchd($type,$cardNo,$amount,$reward,$oldnbc);
?>

the code above running to call an API function in my local network, so i'm sorry i cannot post it as a working code.

after running the code, i've got some message telling that all of my variables was Undefined, here is for example:


Notice: Undefined variable: cardNo in C:\xampp\htdocs\nbchd\function.php on line 5

Notice: Undefined variable: transactId in C:\xampp\htdocs\nbchd\function.php on line 5

Notice: Undefined variable: amount in C:\xampp\htdocs\nbchd\function.php on line 5

Notice: Undefined variable: transactionDate in C:\xampp\htdocs\nbchd\function.php on line 5

Notice: Undefined variable: merchantID in C:\xampp\htdocs\nbchd\function.php on line 5


@john-conde I think this problem is not same with PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" . The variables was missing when i call the function. This code is working when it write it directly, not from function.

here is my simple code that work

<?php
date_default_timezone_set("Asia/Jakarta");
$apikey         = "8ECDD8C5DF4647F0E6EB8CDEEC836E7DA8BACE5A3677424B";
$appkey         = "27D5C651BBC94AE19BDF4F5E2B542667AE7C6EAE62D3BA34";
$transactId     = "";
$url            = "https://192.168.0.39:444/merchantwebapi/api/merchant?";
$submit         = "Invoke";
$merchantDate   = date("Y-m-d H:i:s", time());
$transactionDate= $merchantDate;
$merchantID     = "BLITZ";
$merchantId     = $merchantID;
$type           = "adjustment";
$cardNo         = "6048180000000017";
$amount         = "10";
$reward         = "1";
$oldnbc         = "";

if ($type == "adjustment"):
    if ($amount != ""):
        $site           = "ADJUST BALANCE";
        $signature      = sha1($transactId.$cardNo.$amount.$transactionDate.$merchantID.$site.$apikey);
        $appSignature   = sha1($transactId.$cardNo.$amount.$transactionDate.$merchantID.$site.$signature.$appkey);
        $params         = array('transactId'=>$transactId,'cardNo'=>$cardNo,'amount'=>$amount,'transactionDate'=>$transactionDate,'partnerID'=>$merchantID,'site'=>$site,'signature'=>$signature,'appSignature'=>$appSignature,'submit'=>$submit);
        $encoded_params = array();

        foreach ($params as $k => $v){
            $encoded_params[] = urlencode($k).'='.urlencode($v);
        }

        $url = $url.implode('&', $encoded_params);
        $arrContextOptions = array("ssl"=>array("verify_peer"=>false,"verify_peer_name"=>false,),); 
        $url = file_get_contents($url, false, stream_context_create($arrContextOptions));
        $getstatus = json_decode($url, true);

        if ($getstatus['Status'] == 1):
            echo $site." Succedd!";
        else:
            echo $site." Failed!";
        endif;
    endif;
endif;
?>
Community
  • 1
  • 1
Haris
  • 51
  • 5
  • Of course you can call a function inside a function. You just cant create that function inside the function. – Evan Carslake May 20 '15 at 00:05
  • If you use variables that are declared outside the function, you'll need to pass them into the function. Variables such as `$transactId`, `$cardNo`, `$amount`, etc. are currently out of scope. See [What is variable scope](http://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and). – showdev May 20 '15 at 00:08
  • Showdev is correct. You need to either include ($transactId, $cardNo, $amount, $transactionDate, $merchantID, $site, $apikey) in the function call .... function postdata($datatype, $transactId, $cardNo, $amount, $transactionDate, $merchantID, $site, $apikey) ... or inside the function as globals ... global $transactId, $cardNo, $amount, $transactionDate, $merchantID, $site, $apikey; etc. – Brian May 20 '15 at 00:13

0 Answers0