phpechoreturn-value

Echo a string inside of an echo statement?


This is probably a very simple one to be answered...

I have a piece of code which I need to pull a certain piece of information.

<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('XXXX')->toHTML();?>

For this to work I need the XXXX part to pull the result of the following query:

<?php echo $_product->getAttributeText('warranty') ?>

So the output from the above query will then be the information needed to go in to XXXX.

This markup is completely wrong below but should demonstrate the idea I am trying to achieve:

<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('<?php echo $_product->getAttributeText('warranty') ?>')->toHTML();?>

Solution

  • You just have a redundant PHP opening <?php inside the code. You are already in PHP context so you can do that call directly.

    <?php  echo
    $this->getLayout()->createBlock('cms/block')->setBlockId($_product->getAttributeText('warranty'))->toHTML();?>
    

    However, this is quite complicated and difficult to debug. I would split it in several lines and use variables... remember that you can do it in that context, you are not bound to do everything in one line only :)