Here is a part of my PHP application:
Stage 1:
I get an array of payment methods from user and assign an ID to it, because it can contain multiple choices.
I filled $paymentMethod variable with some examples.
$paymentMethod = ['PayPal','Visa','Master'];
$paymentMethodNames = array("PayPal", "Visa", "Master", "COD");
$paymentMethodNumerical = array("1", "2", "3", "4");
$paymentMethodCalculated = implode(str_replace($paymentMethodNames, $paymentMethodNumerical, $paymentMethod));
the result of echo $paymentMethodCalculated is 123 which is perfectly correct.
Stage 2:
I need to create a special variable which is needed to work for another part of my application.
for ($x = 0; $x < strlen($paymentMethodCalculated); $x++) {
$paymentFinal .= '$_'.substr($paymentMethodCalculated, $x, 1);
if(isset($paymentFinal) && $x !== strlen($paymentMethodCalculated) - 1) {
$paymentFinal .= '.",".';
}
}
The result of echo $paymentFinal is $_1.",".$_2.",".$_3 which is perfectly correct.
Stage 3:
Now I define:
$_1 = Test;
$_2 = Test2;
$_3 = Test3;
Now, when I echo $paymentFinal
It still shows:
$_1.",".$_2.",".$_3
But my desired result is:
Test.",".Test2.",".Test3
Question:
Why PHP does not replace defined variables in $paymentFinal variable?
$_1prior to referring to them in your statement for them to be automatically replaced.evalit. Although I would really recommend not doing it, as pointed in the manual: Caution The eval() language construct is very dangerous because it allows execution of arbitrary PHP code. Its use thus is discouraged. If you have carefully verified that there is no other option than to use this construct, pay special attention not to pass any user provided data into it without properly validating it beforehand.$paymentMethodNumerical, in this specific case?