Search code examples
phphtmlquotes

Multiple quotes in php echo command


I've tried multiple ways with escapes. I need to utilize php code because in the end there endpoints will be variables. I've read multiple solutions and things like starting and ending php does work, but because of the need to utilize variables I need a different solution.

This worked in html:

<input type="hidden" name="cancel" value="<?php echo $payment->route("https://website.com/cancel.php", "") ?>">

PHP Attempt 1:
echo '<input type="hidden" name="cancel" value="<?php echo $payment->route("https://website.com/cancel.php", "") ?>">';

PHP Attempt 2:
echo '<input type="hidden" name="cancel" value=\"<?php echo $payment->route("https://website.com/cancel.php", "") ?>\">';

PHP Attempt 3:
echo '<input type=\"hidden\" name=\"cancel\" value=\"<?php echo $payment->route(\"https://website.com/cancel.php\", \"\") ?>\">';

PHP Attempt 4:
echo '<input type="hidden" name="cancel" value="<?php echo $payment->route(\"https://website.com/cancel.php\", \"\") ?>">';

PHP Attempt 5:
$val_1='<?php echo $payment->route("https://website.com/cancel.php", "") ?>';

echo '<input type="hidden" name="cancel" value="'.$val_1.'">';

PHP Attempt 6:
$val_1='<?php echo $payment->route("https://website.com/cancel.php", "") ?>';

echo '<input type="hidden" name="cancel" value=\"'.$val_1.'\">';


Solution

  • correct way to write all your attempts . You have to learn how to concatenate string and variables in php using . dot concatenate symbol in php. i have corrected all your attempts below. any one will work inside php code. your attempts 1 to 4 have same way to write so the answer is same from 1 to 4 and same for 5- 6 . please compare the wrong use of / also you cannot use <?php inside echo statement unless you have to print it as a string.

    <?php
    
    // echo input string with value directly inside value
    echo '<input type="hidden" name="cancel" value="'.$payment->route("https://website.com/cancel.php", "").'">';
    
    
    // using variable to print value
    $val_1=$payment->route("https://website.com/cancel.php", "");
    echo '<input type="hidden" name="cancel" value="'.$val_1.'">';
    
    ?>
    
    to print inside html you can do as below php file
     <?php
        $val_1=$payment->route("https://website.com/cancel.php", "");
     ?>
    
     <html>
     <body>
     <input type="hidden" name="cancel" value="<?php echo $val_1 ?>">
    
     or
    
     <input type="hidden" name="cancel" value="<?=$val_1 ?>">
     </body>
     </html>