I have a function and within the function I have:
$clean_dir = str_replace("temp_images/".$foldername."/output/", "", $dir);
$foldername value is outside the function.
When I run the above temp_images/xyz/output remains temp_images/xyz/output, but then I replace temp_images/xyz/output with xyz directly instead of $foldername. then it changes.
How can I insert mixed text with a variable in str_replace?
Thanks
I don't recommend using global variables. Pass $foldername
as a function parameter instead:
function some_func($foldername) {
...
$clean_dir = str_replace("temp_images/".$foldername."/output/", "", $dir);
...
}
Pass a variable with the function :
function qwerty($foldername){
....
$clean_dir = str_replace("temp_images/".$foldername."/output/", "", $dir);
....
}
qwerty("xyz");
use global $foldername;
inside function to get $foldername variable value if it is outside the function. Example :
$foldername="xyz";
function qwerty(){
global $foldername;
$clean_dir = str_replace("temp_images/".$foldername."/output/", "", $dir);
....
}
It's not a good practice to use global variable. Instead use the 1st example.