Skip to content
Advertisement

String and number in for each

I have this code:

$Awaarde = 8800;
$Bwaarde = 5500;

$arrayFondsen = array("A", "B");
foreach ($arrayFondsen as $letter)
{
    $data .= ${$letter."vroeger"} . " = " . number_format(((round(${$letter."waarde"}, -2))/1000), 1) . ";n";
}

Expected result (to put as text in another PHP file):
$Avroeger = 8.8;
$Bvroeger = 5.5;

Actual result: (where the 8.3 and 5.7 are the old values of $Avroeger and $Bvroeger)
8.3 = 8.8;
5.7 = 5.5;

Anyone who can help? Thank you in advance!

Advertisement

Answer

You are doing the appending wrong. Do it as shown below.

$Awaarde = 8800;
$Bwaarde = 5500;
$data = '';
$arrayFondsen = array("A", "B");
foreach ($arrayFondsen as $letter)
{
    $data .= "$"."{$letter}vroeger = " . number_format(((round(${$letter."waarde"}, -2))/1000), 1) . ";n";
}

Demo

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement