Is this possible in PHP?

Hello

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?

<?php
function printHERE($data){
    // Some fancy code to output $data where this function is defined
}


// <p>Hello</p> represents about 1,000 lines of PHP/HTML
?>
<p>Hello</p>

<?php
printHERE('<p>Howdy</p>');
?>

That code will output:

Hello
Howdy

A few other things of note:

  • The function my be called multiple times
    Example output if called twice:
Howdy
Howdy
Hello
  • 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?

Thanks in advance for any assistance!

I’m going to say: no.

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.

The code for any page should be laid out in this general order -

  1. initialization
  2. post method form processing
  3. get method business logic - get/produce the data needed to display the page
  4. html document

The html document should use simple php code - require statements, conditional statements, loops, echo statements, … or use an actual template engine.

Function definitions should be in the initialization section, either in-line or via require statements.

Well written functions should return the result they produce to the calling code, so that they can be used in any context, and should not output it directly.

1 Like

Basically, every PHP file has 3 parts:

  1. The PHP file INCLUDES the header file, where all the initialization and HTML <head>, <body> and the first <div> tag is.

  2. The PHP/HTML for the specific page

  3. The PHP file INCLUDES the footer file

Over the past 3-4 years, I’ve never really ran into an issue using this structure until now.

My issue is that I want to add some HTML after the opening body, but before the first div. But the HTML that will be inserted there depends on what happens in the main part of the PHP file.

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.

1 Like

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.

Do you mean you want to echo the data at the top of the PHP file, or at the top of the page as it is rendered in your browser? If the latter, surely you can just stick it all in another <div> and use CSS to position it? Or even something like a short piece of Javascript that runs after the whole page is loaded and inserts the data into the start of the page?

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.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.