-3

I need to concat the value of $sourceName with a string.

This code didn't work:

$sourceName = '<p class="description">123'.$query.'</p class="description">';
echo '<div id="listings" class="listings">
    <strong></strong> '.$$sourceName.'
</div>';

It can't get $query value and the variable only stores 123.

Roma Rush
  • 4,167
  • 1
  • 18
  • 16
  • 2
    `$sourceName` outside the php, `$$sourceName` undefined – devpro Mar 09 '16 at 12:57
  • You'll need to post up your whole code ... – Panda Mar 09 '16 at 12:58
  • yes, first part is in php too. As I said it can ech 123 but can't echo the value of $query – Jessica Baker Mar 09 '16 at 12:59
  • 2
    Ok, it's **not** called echoing inside variable, it's called **concatenation**. [How to combine two strings together?](http://stackoverflow.com/q/8336858/4577762) – FirstOne Mar 09 '16 at 13:00
  • You code has some things that are so weird. 1- opening `` with no text inside; 3- `$$sourceName` where it should be `$sourceName` (you **definitely don't** have a variable called `

    123'.$query.'

    ` xD - for reference: [Variable variables](http://php.net/manual/language.variables.variable.php)
    – FirstOne Mar 09 '16 at 13:08

1 Answers1

1

Use the following code, it works. In your case you need to make sure that $query is not empty.

<?php
    $query = "query var value goes here";
    $sourceName = '<p class="description">123 '.$query.'</p class="description">';
    echo '<div id="listings" class="listings"><strong>'.$sourceName.'</strong></div>';
?>
yergo
  • 4,761
  • 2
  • 19
  • 41
N.J
  • 219
  • 2
  • 5
  • ok, let me be clear, when i add $query = "query var value goes here"; it works but im going to get $query value from input form that users type in – Jessica Baker Mar 09 '16 at 13:32
  • 2
    in that case use $query = $_POST['text_field_name'] (if form method is post) or $query = $_GET['text_field_name'] (if form method is get) – N.J Mar 09 '16 at 13:43