Jump to content


PHP-html paradox

  • Please log in to reply
7 replies to this topic

#1 ksun

  • Members
  • PipPip
  • Member
  • 15 posts

Posted 17 February 2013 - 08:06 AM

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.

#2 requinix

  • Administrators
  • Lazy Administrator
  • 9,378 posts
  • LocationWA

Posted 17 February 2013 - 08:34 AM

I think you're having problems understanding causality...

What's the code?
The Reimann Zeta Function Trolley Problem | "Summer is when I, the great ice fairy, can show my true power!"

#3 ksun

  • Members
  • PipPip
  • Member
  • 15 posts

Posted 17 February 2013 - 08:47 AM

i can't 2 much code. the idea is this

$data = 1;
$number = 2;
function change()
global $data, $number;
$data += 1;
echo $number;
<table width="358" border="3">
    <th scope="col"><?php echo $data; ?></th>
    <th scope="col"><?php change(); ?></th>
    <th scope="col">&nbsp;</th>
<table width="200" border="3">
    <th scope="col">&nbsp;</th>
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();

#4 gizmola

  • Administrators
  • Advanced Member
  • 4,610 posts
  • LocationLos Angeles, CA USA

Posted 17 February 2013 - 09:00 AM

No.  At the point you are echoing out the value, the value is 1.  

#5 ksun

  • Members
  • PipPip
  • Member
  • 15 posts

Posted 18 February 2013 - 03:14 AM

is it possible to use javascript?

#6 Jessica

  • Staff Alumni
  • This is not my name.
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 18 February 2013 - 03:15 AM

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. 
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#7 l0gic

  • Members
  • PipPipPip
  • Advanced Member
  • 110 posts
  • LocationChristchurch City, New Zealand

Posted 18 February 2013 - 10:48 AM

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.

I start more projects than I finish.

#8 Christian F.

Christian F.
  • Staff Alumni
  • Advanced Member
  • 3,106 posts
  • LocationNorway

Posted 18 February 2013 - 01:52 PM

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.
Keeping it simple.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users