0

I have this code

$text = "Hello $person";
$persons = array("Mike", "Tom", "Foo");
foreach ($persons as $person) {
    echo $text."<br>";
}

The goal is to display:

Hello Mike<br>
Hello Tom<br>
Hello Foo<br>

I thought about using {} and $$, but that doesn't seem to be ok. What am I missing?

3
  • 2
    You could just try echo "Hello ".$person."</br>"; instead. No need for the $text string then Commented Mar 2, 2016 at 17:31
  • Thanks for pointing to the right direction: sprintf Commented Mar 2, 2016 at 17:40
  • $text = "Hello %s"; inside the loop: echo sprintf($text,$person); Commented Mar 2, 2016 at 17:47

6 Answers 6

3

What am I missing?

You're assigning $text to a string that includes $person, but $person isn't defined yet (you should be getting a Notice: Undefined variable message). You could define $text within the loop, like many other answers suggest. But your code sample might look more familiar with sprintf:

$text = "Hello %s";
$persons = array("Mike", "Tom", "Foo");
foreach ($persons as $person) {
    echo sprintf($text, $person) . "<br>";
}

sprintf() will allow you to format a string by passing a parameter to it.

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

Comments

2

This should achieve it.

    $persons = array("Mike", "Tom", "Foo");
      foreach ($persons as $person) {
         echo "Hello " . $person . "<br>";
      }

1 Comment

Indeed that would work, but the real variable is defined much more extensive.
0

To make this simpler you could do

 $persons = array("Mike", "Tom", "Foo"); 
foreach ($persons as $person)
 { echo "hello".$person."<br>";
 }

Or you would do

   $text = "Hello";
    $persons = array("Mike", "Tom", "Foo");
     foreach ($persons as $person) 
    { 
    echo $text." ".$person."<br>"; 
    }

Comments

0

Just append hello using foreach the way i am doing is somehow very similar!

$persons = array("Mike", "Tom", "Foo");
      foreach ($persons as $person) {
      echo "Hello " . $person. "<br>";// Remove text and append Hello + break tag ofcourse
      }

Outputs:-

Hello Mike
Hello Tom
Hello Foo

Comments

0

Let be a little creative, probably not the best, but it is one liner.

$persons = array("Mike", "Tom", "Foo");
echo vsprintf(str_repeat("Hello %s<br>", count($persons)), $persons);

Another one liner can be achieved with implode

echo "Hello" . implode("<br>Hello ", array("Mike", "Tom", "Foo")) . "<br>";

Note that my solution is not recommended, but it would be boring to have same answer as other people.

1 Comment

Even more beautiful ;-)
-1

Try this:

$persons = array("Mike", "Tom", "Foo");
foreach ($persons as $person) {
    $text = "Hello $person";
    echo $text."<br/>";
}

$text is outside foreach, for that reason $person wasn't echoed

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.