Jump to content

PHP-html paradox


ksun

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

When dealing with PHP and HTML there are (at least) three fundamental aspects you need to know:

  1. Each block of PHP is executed fully on the server, before any content is sent to the client (browser).
  2. Every page load runs all of the code from scratch, meaning that no previous values will be saved (except sessions).
  3. 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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.