ksun Posted February 17, 2013 Share Posted February 17, 2013 so here's one for ya, i've got a block of php code within a section of a html table with 3 columns, once the php code is phrased, it will echo stuff in the middle column in the html table. however this block of code will also change some variables, that are already echoed in the first column of the html table. seeing as how php and html is up to down, everytime the middle column php code is pharsed and echoed the variables in the first column will stay as if the variables are not changed. I really hope someone can help me and that there's a really newby/easy way to solve this problem. Quote Link to comment https://forums.phpfreaks.com/topic/274585-php-html-paradox/ Share on other sites More sharing options...
requinix Posted February 17, 2013 Share Posted February 17, 2013 I think you're having problems understanding causality... What's the code? Quote Link to comment https://forums.phpfreaks.com/topic/274585-php-html-paradox/#findComment-1412889 Share on other sites More sharing options...
ksun Posted February 17, 2013 Author Share Posted February 17, 2013 i can't 2 much code. the idea is this <?php $data = 1; $number = 2; function change() { global $data, $number; $data += 1; echo $number; } ?> <body> <table width="358" border="3"> <tr> <th scope="col"><?php echo $data; ?></th> <th scope="col"><?php change(); ?></th> <th scope="col"> </th> </tr> </table> <table width="200" border="3"> <tr> <th scope="col"> </th> </tr> </table> </body> </html> see where the $data is echoed??? it's mean't to echo 2, instead it echos 1; cause it echos B4 it adds 1; what i want to know is, is there any way for it to echo 2 while still retaining the structure of the function change(); Quote Link to comment https://forums.phpfreaks.com/topic/274585-php-html-paradox/#findComment-1412892 Share on other sites More sharing options...
gizmola Posted February 17, 2013 Share Posted February 17, 2013 No. At the point you are echoing out the value, the value is 1. Quote Link to comment https://forums.phpfreaks.com/topic/274585-php-html-paradox/#findComment-1412899 Share on other sites More sharing options...
ksun Posted February 18, 2013 Author Share Posted February 18, 2013 is it possible to use javascript? Quote Link to comment https://forums.phpfreaks.com/topic/274585-php-html-paradox/#findComment-1413055 Share on other sites More sharing options...
Jessica Posted February 18, 2013 Share Posted February 18, 2013 If you make your function return a value instead of echoing it, you can call it before you need to echo the value. Your logic should be separate from your presentation. Quote Link to comment https://forums.phpfreaks.com/topic/274585-php-html-paradox/#findComment-1413056 Share on other sites More sharing options...
l0gic Posted February 18, 2013 Share Posted February 18, 2013 I have a feeling that's not the actual code you're using and trying to figure out. Which doesn't help us to help you. For what you've posted, your example. You'd be better off not even using PHP as I don't see any looping, any real data or value modification. Why don't you post the code that you're actually having an issue with? That way we can help you a lot more, believe it or not most of us understand poorly constructed code better than poorly written English round-a-bout or near-enough questions. As to 'can you do it with Javascript' well, I'm not entirely sure what you're trying to do. In closing: Post the code you're having an issue with. Post any errors, or describe what isn't working the way you want it to. Then we have a better chance of helping you. Quote Link to comment https://forums.phpfreaks.com/topic/274585-php-html-paradox/#findComment-1413087 Share on other sites More sharing options...
Christian F. Posted February 18, 2013 Share Posted February 18, 2013 When dealing with PHP and HTML there are (at least) three fundamental aspects you need to know: Each block of PHP is executed fully on the server, before any content is sent to the client (browser). Every page load runs all of the code from scratch, meaning that no previous values will be saved (except sessions). The parsing of the PHP is a completely different and separate process from the processing of the HTML, CSS and JS. PHP can be used to output the latter, but has no intrinsic understanding of what it does. Only the browser does, and it does not know about the PHP code. All of this means that you cannot do what you're attempting to do, at least not without using sessions to save the number across pageloads. Also, you should avoid the use of the global keyword, and use return instead of echo inside functions. The proper way of inserting values into a function is by passing them as parameters. This clearly tells everyone reading the code (including you in a few months) what it expects, and ensures that it has no hidden side-effects. In short: function change ($number) { return $number += 1; } Also, once you've echo'd something (or otherwise sent it to the browser) you cannot change it any longer. The same way you cannot have something change, before actually changing it. Quote Link to comment https://forums.phpfreaks.com/topic/274585-php-html-paradox/#findComment-1413100 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.