Search code examples
phpfopenfedex

Php file definition with variables


I am modifying FedEx's API files to fit my site, and I am struggling to make the definitions dynamic based on the total number of packages.

$totalPackages = 4;
for($i=1; $i<=($totalPackages-1); $i++) {
    define('SHIP_CHILDLABEL_'.$i, 'shipchildlabel_'.$i.'pdf');
}

Later on in the file...

for($i=1; $i<=($totalPackages-1); $i++) {
    $fp = fopen(SHIP_CHILDLABEL_.$i, 'wb');
    fwrite($fp, ($childResponse->CompletedShipmentDetail->CompletedPackageDetails->Label->Parts->Image));
    fclose($fp);
    echo '<a href="./'.SHIP_CHILDLABEL_.$i.'.pdf">'.SHIP_CHILDLABEL_.$i.'</a> was generated';
}

Of course, this causes fopen to reference SHIP_CHILDLABEL_1.pdf, SHIP_CHILDLABEL_2.pdf, and SHIP_CHILDLABEL_3.pdf instead of shipchildlabel_1.pdf, shipchildlabel_2.pdf, and shipchildlabel_3.pdf. How can I properly make these definitions dynamic?


Solution

  • You can use constant:

    $fp = fopen(constant('SHIP_CHILDLABEL_'.$i), 'wb');
    

    Docs here