1

Here is my situation, let's say i have two variables:

$a = "Peter";
$b = "tall";

Then, i can combine this by

$c = " $a is very $b ";

which give me the string : "Peter is very tall"

However, if i have another variable $e passed to my function, and the string is " $a is very $b " then, i print out $e, it just show me " $a is very $b ";

What i expected is "Peter is very tall", because $e is equal to $c.

This is the whole logic flow:

$e = " $a is very $b ";
getMsg($e);
function getMsg($e){
  $a = "Peter";
  $b = "tall";
  $c = " $a is very $b ";
  echo $c //Peter is very tall
  echo $e //$a is very $b
}

How can i achieve this function?

3
  • Where's the function that you tried? Commented Sep 27, 2019 at 11:14
  • 1
    Turn on error reporting and you will see something like Notice: Undefined variable: a in ... and Notice: Undefined variable: b in ... . $a and $b are undefined in your $e line. And echo $e will probably output " is very ". (Also, your echos are missing a ;) php.net/manual/en/language.variables.scope.php Commented Sep 27, 2019 at 11:21
  • You should never use double quotes for string, always single ones. If you try to rewrite your code without double quotes and concatenate strings by yourself, I think you will easily find what's wrong Commented Sep 27, 2019 at 12:06

3 Answers 3

2

You could use sprintf sprintf documentation. The %s symbols are string tokens that get substituted in the format string (the 1st param to sprintf).

$e = "%s is very %s";
getMsg($e);
function getMsg($e){
  $a = "Peter";
  $b = "tall";
  echo sprintf($e, $a, $b);
}

Or a less coupled version of the getMsg() function:

function getMsg($msg,$name,$is){
  echo sprintf($msg, $name, $is);
}  
getMsg("%s is very %s", "Peter", "tall");
Sign up to request clarification or add additional context in comments.

3 Comments

You should never use double quotes for string, always single ones. That's what caused OPs confusion in the first place
Not always. e.g. $a='hello'; echo '$a'
Yes, that the whole point : that makes you think about if you need quotes or not. You can't just use them everywhere. In that exemple you absolutely don't need quotes, simple or double ones !
0

Please try this code. I hope this is what you want.

<?php
// Your code here!
 $a = "Peter";
  $b = "tall";
$e = $a."is very".$b;
getMsg($e,$a,$b);
function getMsg($e,$a,$b)
{
 $c = $a."is very".$b;
  echo $c;
 echo $e;
}
?>

1 Comment

You need to check the scope of variables and also you need to check you function definition and how you're calling your function.
0

Try this. It should work fine. You need to Define $a and $b outside the function.

function getString($a,$b,$e){

  $c = " $a is very $b ";
  echo $c;
  echo '<br/>'.$e;
}
$a = "Peter";
$b = "tall";
$e = " $a is very $b ";
$response = getString($a,$b,$e);

OUTPUT :

Peter is very tall 
Peter is very tall

Comments

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.