0

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?

3

1 Answer 1

1

The problem is that PHP doesn't alter your string based on your defined variables. You have variable, which contains set of characters which remain the same until you alter the string by yourself (like str_replace or something similar). The "variables" inside $paymentFinal ("$_1.",".$_2.",".$_3") are just set of characters; they don't represent the actual variables you have defined inside your code.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your definite answer. Can you explain the logic of this behavior of PHP? Wouldn't be better if it was possible?
This is a normal behavior of any programming language. The variable is just a pointer to some space inside the memory, where the values of variables are stored. And very simply, the string variables are stored there as well. They are just bytes located inside the memory. Nothing more. If you put "$_1" inside your memory, it will be set of bytes representing this value. The programming language won't try to replace this string with other variables (as you thought), unless you tell it explicitly to do it (like str_replace).
Of course if we would create a language, where this could be an automatic feature, it would be a really dangerous. Imagine creating a program, where users could put their own input. If the user put the "variable" we're talking about, it could brake or expose the program. If you want similar behavior, it would be better to create some list/map, where you could put those "variables" (for example as a key) and access them there. This way if someone would put something that is not inside the list, it wouldn't allow them to access it. Of course, there can be many other ways to do this thing!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.