0

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

3
  • 4
    use global $foldername; inside function to get $foldername variable value if it is outside the function. Can you show the whole code, please. Commented Oct 20, 2013 at 15:10
  • How can I insert mixed text with a variable in str_replace? -- what do you mean by that? What does var_dump($foldername); output? And what do you want the result to be? Commented Oct 20, 2013 at 15:13
  • @PannyMonium: Using global variables is widely regarded as a bad practice. Pass the variable as a function parameter instead. See my answer below. Commented Oct 20, 2013 at 15:19

2 Answers 2

4

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);
    ...
}
Sign up to request clarification or add additional context in comments.

5 Comments

I tried it as in function FolderToDropbox($dir, $dropbox_link, $foldername) but it didn't work
@PannyMonium: didn't work is not very descriptive. Add ini_set('display_errors',1); error_reporting(E_ALL); to the very top of your script and refresh the page. Do you get any errors?
Missing argument 3 for FolderToDropbox()
@PannyMonium: The error is pretty self-explanatory, no? Fix your function call ;)
Are you brave enough to answer this question? stackoverflow.com/questions/19483881/… :-)
-1

Pass a variable with the function :

function qwerty($foldername){
 ....
 $clean_dir = str_replace("temp_images/".$foldername."/output/", "", $dir);
 ....
}
qwerty("xyz");

Other Way (not good)

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.

3 Comments

If it's not a good practice, then why include it in your answer? :)
The user must know the both ways. Sometimes global is needed in desperate times. So other users who sees this answer might find it helpful.
@Subin I agree, but don't put the bad solution at the top of an answer. Most users will only take that and if it works, they won't give the actual solution a try.

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.