Saturday, December 26, 2015

Print variable on top of the page where variable is defined bottom - php

Some time we need to print/echo a variable on the top of the page but that variable need to define after a long code bottom of the page. as we know in PHP all instructions are executed sequentially so we cannot read a variable that hasn't yet been defined. 

We can do it easily using jquery. Example

We need to print a total discount value top of the page 

<p> Total Discount : <span id="demo"></span> USD </p>

But the sum of discount value need to find after a long code 
...............................php  code bla bla bla to find a variable $sum_discount  value..................
...............................php  code bla bla bla to find a variable $sum_discount  value..................

Then asign  that value within a jquery 

 <?php    
     echo "<script type='text/javascript'> 
                        document.getElementById('demo').innerHTML = ".$sum_discount.";     
                </script>";
?>

Total Pageviews