Sounds like you're looking for output buffering:

ob_start();
system($command, $returnCode);
$output = ob_get_clean();

This should preserve all white-space characters at the end of each output line (exec as you wrote destroys these, so implode would not be an option).

Alternatively, you can open a process and aquire the pipes (standard output, STDOUT) and read the output out of these. But it's more complicated (but gives you more options). See proc_open.

Answer from hakre on Stack Overflow
๐ŸŒ
PHP
php.net โ€บ manual โ€บ en โ€บ functions.returning-values.php
PHP: Returning values - Manual
Values are returned by using the optional return statement. Any type may be returned, including arrays and objects. This causes the function to end its execution immediately and pass control back to the line from which it was called.
๐ŸŒ
BCCNsoft
doc.bccnsoft.com โ€บ docs โ€บ php-docs-7-en โ€บ function.system.html
Execute an external program and display the output
<?php echo '<pre>'; // Outputs all the result of shellcommand "ls", and returns // the last output line into $last_line. Stores the return value // of the shell command in $retval. $last_line = system('ls', $retval); // Printing additional info echo ' </pre> <hr />Last line of the output: ' .
๐ŸŒ
Dinocajic
dinocajic.com โ€บ home โ€บ programming โ€บ php โ€” p37: functions returning values
PHP Functions Returning Values - User Defined Functions
August 11, 2023 - To return a value, you will use the return keyword inside of your function. Make sure that the return statement is the last statement inside of your function since PHP will exit the function after the return statement is executed.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ php โ€บ php_returning_values.htm
PHP - Returning Values
When called, it performs a certain task and sends the program control back to position from where it was called even if return statement is not used. The return statement allows it to take a value, along with the control, back to the calling environment. function foo($arg_1, $arg_2) { statements; ...
๐ŸŒ
Hacking with PHP
hackingwithphp.com โ€บ 4 โ€บ 15 โ€บ 1 โ€บ return-values
Return values โ€“ Hacking with PHP - Practical PHP
The "return" keyword sets up the function return value to be whatever variable you use with it, then exits the function immediately. You can also just use "return;", which means "exit without sending a value back." ... <?php function foo() { print "In function"; return 1; print "Leaving ...
๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ php_functions.asp
PHP Functions
function myFamily(...$firstname, $lastname) { $txt = ""; $len = count($firstname); for($i = 0; $i < $len; $i++) { $txt = $txt."Hi, $firstname[$i] $lastname.<br>"; } return $txt; } $a = myFamily("Doe", "Jane", "John", "Joey"); echo $a; Try it Yourself ยป ยท In the examples above, notice that we did not have to tell PHP which data type the variable is. PHP automatically associates a data type to the variable, depending on its value.
Find elsewhere
๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ keyword_return.asp
PHP return Keyword
zip_close() zip_entry_close() ... zip_read() PHP Timezones ... The return keyword ends a function and, optionally, uses the result of an expression as the return value of the function....
๐ŸŒ
DEV Community
dev.to โ€บ karleb โ€บ return-types-in-php-3fip
Return Types In PHP - DEV Community
March 27, 2023 - Note that since the return type of getAddress() is specified as :object, the returned value can be an instance of any class, not just Address. What if we can not tell the actual type a class should return or do we want to intentionally make the return type for any class dynamic? PHP allows us to include additional return types using the union type, this allows us to include alternate types by separating them with a pipe |.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ php โ€บ php_functions.htm
PHP - Functions
Here are the key concepts used in php functions โˆ’ ยท Function Parameters and Arguments: Pass data to functions and specify default values. Return Values: Use return to send the results back to the caller.
๐ŸŒ
Exakat
php-dictionary.readthedocs.io โ€บ en โ€บ latest โ€บ dictionary โ€บ return-value.ini.html
Return Value โ€” PHP Dictionary 1.0.0 documentation
January 10, 2025 - A value is returned with the keyword return. When no such command is used, for example when a method doesnโ€™t have a return command and reach the end of the methodโ€™s body, then null is returned. <?php function foo() : int { return 1; } function goo() : never { throw new Exception("Error"); ...