Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to evaluate/store JS/HTML code into a PHP variable instead of displaying it?

I have a PHP file paypal.php.

<script src="https://www.paypal.com/sdk/js?client-id=<?php echo get_paypal('client-id'); ?>&vault=true&intent=subscription" data-sdk-integration-source="button-factory"></script>
<script>
paypal.Buttons({
    // ...
}).render('#paypal-button-container-<?php echo get_paypal('plan-id'); ?>');

I want to evaluate paypal.php in another php file and store the evaluation result in a variable and then output it to the user at later time.

Using this approach

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

$content = eval('?>' . file_get_contents('paypal.php') . '<?php');

the script tag is being echo-ed and the javascript is executed in the browser. But I do not want it to be echo-ed. I just want to keep the result for further processing.

How can I avoid echo-ing the evaluation result and just store the result in a variable?

>Solution :

To store some content in a PHP variable before it’s displayed, you can use PHP buffering functions ob_start() and ob_get_clean() as follows:

<?php 
    ob_start(); // Start buffering 
?>
<script src="https://www.paypal.com/sdk/js?client-id=<?php echo get_paypal('client-id'); ?>&vault=true&intent=subscription" data-sdk-integration-source="button-factory"></script>
<script>
paypal.Buttons({
    // ...
}).render('#paypal-button-container-<?php echo get_paypal('plan-id'); ?>');
<?php 
// We can now store the buffered content in a variable
$buffered_content = ob_get_clean(); 
?>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading