K
K
Karl_Fridrih2020-01-24 14:17:54
Python
Karl_Fridrih, 2020-01-24 14:17:54

How to make a reference to a function variable in a string argument?

Good afternoon! A rather primitive question arose, the answer to which, unfortunately, could not be found either in the manual or in Google. The essence of the problem is as follows. There is a certain method which accepts a certain line as argument.

function test($str){
            $a=5;
            $b=$str;
            return $b;
}

When calling this method, I need to pass a string in which there will be a place for the $a variable present in the method. That is, conditionally, it would look like this: Naturally, in this form, PHP tries to immediately substitute the value of the $a variable, insert it into the argument, and send everything as a regular string. If you use single quotes then $a will of course escape as plain text in the string. Of course, there are "printf" and "vsprintf" functions, but in this case they do not suit me - because it is important for me to leave the maximum possible number of variations of the $str argument passed to the function. Actually, the question is whether it is possible in the function argument to correctly refer to a variable that will be calculated already during the execution of the function, and if so, what is the easiest way to implement this?
$result=$obj->test("Моя строка $a");

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir, 2019-04-23
@vintello

multithreading and only it alone will save
we break the incoming file into chunks of 2000 records, for example
, in each block we nail unnecessary lines
, we write each chunk to a new file , there
will be problems with writing to the file. so you need to use this recipe
, the road will be mastered by the walking one :)

S
Stimulate, 2020-01-24
@Karl_Fridrih

function test($str) {
            $a=5;
            $b=str_replace('$a', $a, $str);
            return $b;
}

echo test('Моя строка $a');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question