Search code examples
phpvirtuemart

What is the point of this line of code?


I found this line of code in the Virtuemart plugin for Joomla on line 2136 in administrator/components/com_virtuemart/classes/ps_product.php

eval ("\$text_including_tax = \"$text_including_tax\";");

Solution

  • Scrap my previous answer.

    The reason this eval() is here is shown in the php eval docs

    This is what's happening:

    $text_including_tax = '$tax <a href="...">...</a>';
    
    ...
    
    $tax = 10;
    
    ...
    
    eval ("\$text_including_tax = \"$text_including_tax\";");
    

    At the end of this $text_including_tax is equal to:

    "10 <a href="...">...</a>"
    

    The single quotes prevents $tax being included in the original definition of the string. By using eval() it forces it to re-evaluate the string and include the value for $tax in the string.

    I'm not a fan of this particular method, but it is correct. An alternative could be to use sprintf()