Search code examples
phparraysvariablesquotes

PHP variables inside an array wrapped in single quotes


I am using Amazon API to display dvd and bluray product information on my website. I have the following PHP code that looks up product information based on its ItemID.

$movies = file_get_contents('movies.xml');
$xml = new SimpleXmlElement($movies);

$dvd = $xml->movie->amazon->dvd;
$bluray = $xml->movie->amazon->bluray;

$request = aws_signed_request('co.uk', array(
            'Operation' => 'ItemLookup',
            'ItemId' => '$dvd, $bluray',
            'ResponseGroup' => 'Medium, Offers',
            'MerchantId' => 'All'), $public_key, $private_key, $associate_tag);

I know the following line of code causes the problem:

'ItemId' => '$dvd, $bluray'

I'm not sure how I can assign two variables inside the array wrapped in single quotes.

If I assign direct values to ItemId, then everything works:

$request = aws_signed_request('co.uk', array(
            'Operation' => 'ItemLookup',
            'ItemId' => 'B004Q9SZGC, B004Q9T6CO',
            'ResponseGroup' => 'Medium, Offers',
            'MerchantId' => 'All'), $public_key, $private_key, $associate_tag);

However, I would like to use my $dvd and $bluray variables to look up ItemId stored in the local XML file (movies.xml) rather than hardcode the actual ItemId value inside the array.


Solution

  • In PHP you can concatenate expressions (e.g. variables and strings) with the . sign.

    For example:

    'ItemId' => $dvd.', '.$bluray,