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
$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?
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();
?>