I need a PHP loop for the following code below: What is the easiest way to produce the below example.
$a = new ShipPackage('UPS');
$a->setParameter('length','5');
$a->setParameter('width','5');
$a->setParameter('height','5');
$a->setParameter('weight','5');
$combined->addPackageToShipment($a);
If loop 3X, it should look like this below:
$a = new ShipPackage('UPS');
$a->setParameter('length','5');
$a->setParameter('width','5');
$a->setParameter('height','5');
$a->setParameter('weight','5');
$combined->addPackageToShipment($a);
$a2 = new ShipPackage('UPS');
$a2->setParameter('length','5');
$a2->setParameter('width','5');
$a2->setParameter('height','5');
$a2->setParameter('weight','5');
$combined->addPackageToShipment($a2);
$a3 = new ShipPackage('UPS');
$a3->setParameter('length','5');
$a3->setParameter('width','5');
$a3->setParameter('height','5');
$a3->setParameter('weight','5');
$combined->addPackageToShipment($a3);
for($x = 0; $x < 3;$x++)
{
$a = new ShipPackage('UPS');
$a->setParameter('length','5');
$a->setParameter('width','5');
$a->setParameter('height','5');
$a->setParameter('weight','5');
$combined->addPackageToShipment($a);
}
Im not sure about your question but as how i understand it, this will work.
You can use a for
loop to loop through the code as many times as necessary. You did not give any info about why you need the loop, so I would assume a for
loop will be best.
for ($b = 0; $b < 3; $b++) {
$a = new ShipPackage('UPS');
$a->setParameter('length','5');
$a->setParameter('width','5');
$a->setParameter('height','5');
$a->setParameter('weight','5');
$combined->addPackageToShipment($a);
}
Change 3
(in $b < 3
) to the number of times you want to loop. You can replace 3
with a variable, if applicable.
You can also use a while
loop to execute code while a certain condition is true.
while ($canShip) {
$a = new ShipPackage('UPS');
$a->setParameter('length','5');
$a->setParameter('width','5');
$a->setParameter('height','5');
$a->setParameter('weight','5');
$combined->addPackageToShipment($a);
}