I am making some changes to a PHP site, and came across a bit of an issue. I need PHP to echo data at the top of a PHP file, but only if some conditions are met. And I won’t know if those conditions are met until later in the file.
Because of the structure of the file, it will be a huge pain to put the validation at the beginning, so I am hoping something like this is possible. Or if it’s not possible, is there a better way to accomplish this?
// Some fancy code to output $data where this function is defined
// <p>Hello</p> represents about 1,000 lines of PHP/HTML
That code will output:
A few other things of note:
The function my be called multiple times
Example output if called twice:
There are other functions in the same file that use the “echo” statement, and they need behave normally.
TL;DR: Is it possible to have a function output HTML from where the function is defined, instead of where the function is called?
Reading between the lines, it seems to me that the want for this is a fix for a bad code structure in the first place.
Your code examples are a bit too vague to really know what you are trying to do, but I’m guessing you have “mish-mash” of HTML and PHP logic going on throughout the page, and that is giving you a headache.
The most painless way to do PHP is to separate your programming logic from the HTML you serve to the page.
Do have all your processing happen at the beginning before you even think about outputting any HTML. So first run all your programming logic to determine what the output will eventually be, before you output anything. That way it won’t matter when you call the function that depends on what conditions were met previously, because when you have already worked averything out, you begin your output to a clean sheet. So the output you give can be anything at all.
Again, it’s all a bit too vague to be specific about this, and I may have your intention all wrong, it’s just that you asking the question sets off alarm bells that you are structuring your code badly and suffering the consequence.
OK, so your first part should have no HTML in it.
You want to start with pure PHP where all the logic happens that determines what will appear in the HTML, before you get into your HTML.
So you will plan out the whole page structure before you start to create it, if that makes sense.
It’s that kind of separation that will free you of this kind of conunderum.
Again, it’s hard to give specifics.
Validation should always by at the top of the page before output to the browser. Nothing should be echoed from this section of the page. If a message needs to be displayed to the user further down in the page like “Please enter the correct value”, set the message to a variable or array to be echoed within the <body> of the page. The advantage to doing this is you can have any number of options available before output to the browser such as;
Not processing values to the DB because something was not right, so you can keep the values in the form and display an error message to the user.
All values entered passed validation, insert to DB, create a SUCCESS message to be shown below and use a header refreash delay for say 3 seconds so they see the message and then and page reloads without form values.
Upon Success use a header location to directly send the user to a secure section of the site and this CAN’T be done if there has been output to the browser.
You can completely change what is shown on the page such as a completely different set of questions to answer.
A list of records will show updated values as the page reloads.
So by keeping php logic separate and above html gives you options as to what can happen.
Neither of those are in any way “better” than structuring things better as everyone else has already discussed, of course.
I’m going to vote no as well. PHP simply does not track where a function is defined in any useful fashion.
Maybe (heavy emphasis on the maybe) you could use output buffering to capture the html as it is being generated. You could then add an html comment to act as a placeholder for where you want the function output to end up. Your function could then replace the comment with actual output. Very much a long shot.
I get that you probably cannot redesign the entire app so the various “close the barn door” suggestions are probably of limited value. It is still not clear (to me at least) why you need this functionality. Consider writing a single simple PHP file containing an example of what you are trying to do and posting.