Suppress echo from PHP include file

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

I need to include this file in another php script, say PHP02.php , since I need access to the array being created. But when the jquery POST request is in PHP02.php , the returned data is also echo from PHP01.php

How can I suppress the echo from the first file?

+4
source share
4 answers

You can output a buffer if editing or other restructuring is not possible:

 ob_start(); include "PHP02.php"; ob_end_clean(); 
+12
source

create a new function without echo

0
source

If you can, you should look at refactoring the code in the PHP source file. If he performs a function, he must do it. Then the code calling the function must decide if they want echo messages.

As you just learned, this is an important part of orthogonal design. I would recommend rewriting it so that it does what you want, and let the code that calls the function decide what they want to output. That way, you no longer have to worry about it.

You can also look at the use of output buffers. See ob_flush in PHP: http://php.net/manual/en/function.ob-flush.php

0
source

Try adding a condition to PHP01.php that checks where it is being called from, this ensures that you only echo the code if the file PHP01.php call is PHP01.php

In addition, it is better if you add functions to your own file, if necessary, to save certain functions that are present, for example, in PHP01.php , you can add include 'function01.php'; , and it will have this function divided between two files.

0
source

Source: https://habr.com/ru/post/1394463/


All Articles