-1

Possible Duplicate:
Difference between single quote and double quote string in php

I have the following:

<?php
$message = '<div id="msg-status" class="msg-status">Item #{$item} - {$desc} has been added successfully.</div>';
        $data    = array(
            'success' => $products_table,
            'message' => $message
        );
        header('Content-type: application/json');
        echo json_encode($data);
?>

Can someone help me make {$item} and {$desc} actual variables. I read you should put brackets around them to make them variables but they are still coming up as part of the string. Any suggestions on where I am going wrong?

Community
  • 1
  • 1
Dev Newb
  • 565
  • 1
  • 6
  • 24
  • 3
    I'd suggest reading [the manual](http://php.net/manual/en/language.types.string.php) – Jonnix Jan 05 '13 at 20:53
  • @jon - no need to be rude about it. We all started somewhere... – Lix Jan 05 '13 at 21:02
  • @Lix Who's being rude? It is the best place to get a proper understanding of all the different string constructs and their respective behaviours. *shrug* – Jonnix Jan 05 '13 at 21:05
  • @jon - this is true, but there is nothing wrong with simple questions like these on [so]. A link to the relevant manual page is a great resource, don't get me wrong, it's all about how you say it. – Lix Jan 05 '13 at 21:08

3 Answers3

3
<?php
$message = '<div id="msg-status" class="msg-status">Item #'.$item.' - '.$desc.' has been added.';
?>

Use Above. You do not have to add your variable to string within quotes instead you have to concatenate it with the string using "."

Muhammad Talha Akbar
  • 9,952
  • 6
  • 38
  • 62
  • Exactly what I was looking for and I HAVE seen this before. Just learning and I got confused with the other method. Thx! – Dev Newb Jan 05 '13 at 21:15
3

Variables in strings will only be expanded if the string is in double quotes, not single.

<?php
$message = "<div id=\"msg-status\" class=\"msg-status\">Item #{$item} - {$desc} has been added successfully.</div>";
    $data    = array(
        'success' => $products_table,
        'message' => $message
    );
    header('Content-type: application/json');
    echo json_encode($data);
?>
Reactgular
  • 52,335
  • 19
  • 158
  • 208
1

If you are using single quote to wrap your string, you 'll simple have to terminate the string and concatenate the variables and the rest of the string. Like this -

$epoch = time();
$a = 'Epoch time is' . $epoch . ', woohoo!';

If you are using double quotes to wrap your strings, you can just place the variable directly into the string. Like this -

$epoch = time();
$a = "Epoch time is $epoch, woohoo!";

Usually, for better readability, I'll use double quotes together with curly brackets to signify a variable within a string -

$epoch = time();
$a = "Epoch time is {$epoch}, woohoo!";

To put it all together, your string would have to look something like this -

$message = "<div id='msg-status' ... >Item #{$item} - {$desc} ...</div>";

Notice that the quotes within the string had to be changed also to single if we changed the outer ones to double. You could leave the double quotes in there, but then you'd need to worry about escaping them \" and your code be comes really annoying to read :)

Lix
  • 47,311
  • 12
  • 103
  • 131