amit
amit

Reputation: 10261

Suppress echo from PHP include file

I have a file PHP01.php which: - performs a function - creates an array - echo's some message

I need to include this file in another php script, say PHP02.php as I need access to the array it created. But when I jquery POST request to PHP02.php, the data it returns also has the echo from PHP01.php

How can I suppress the echo from the first file?

Upvotes: 5

Views: 3632

Answers (4)

Ry-
Ry-

Reputation: 224942

You can output buffer it if editing or otherwise restructuring is not possible:

ob_start();

include "PHP02.php";

ob_end_clean();

Upvotes: 15

OpensaurusRex
OpensaurusRex

Reputation: 842

Try adding a conditional to PHP01.php that checks to see where it is being called from, this will ensure that you only echo it out if the file making the call is PHP01.php

Additionally it is better if you place functions in their own file to be included if needed so as to keep certain features that are present from being included for example in PHP01.php you can add include 'function01.php'; and it will have that function shared across the two files.

Upvotes: 0

Tango Bravo
Tango Bravo

Reputation: 3309

If you can, you should look at refactoring the code in the original PHP file. If it's performing a function, it should do that. Then, the code that called the function should decide if they want to echo a message.

As you've just learned, this is an important part of orthogonal design. I'd recommend re-writing it so that it performs what you want it to, and let the code that calls the function, decide what they want to output. That way you won't have to worry about these things again.

You can also look into using output buffers. See ob_flush in PHP: http://php.net/manual/en/function.ob-flush.php

Upvotes: 0

Paul Dessert
Paul Dessert

Reputation: 6389

create a new function without the echo

Upvotes: -1

Related Questions