2

Stupid question, this code:

<?php
$variable = system("cat /home/maxor/test.txt");
echo $variable;
?>

with file test.txt:

blah

prints:

blah
blah

What can I do with system() function to not print nothing so I get 1 "blah"???

5 Answers 5

8

system displays whatever the program outputs and returns the last line of output.

exec displays nothing and returns the last line of output.

passthru displays whatever the program outputs and returns nothing.

Sign up to request clarification or add additional context in comments.

Comments

5

Use exec instead of system

http://us.php.net/manual/en/function.system.php#94262

Comments

2

According to the manual -- see system() :

system() is just like the C version of the function in that it executes the given command and outputs the result.

Which explains the first blah


And :

Returns the last line of the command output on success

And you are echoing the returned value -- which explains the second blah.


If you want to execute a command, and get the full output to a variable, you should take a look at **[`exec`][2]**, or **[`shell_exec`][3]**.

The first one will get you all the lines of the output to an array (see the second paramater) ; and the second one will get you the full output as a string.

Comments

2

Use exec instead. To get all the output, rather than just the last line do this:

$variable = array();
$lastline = exec("cat /home/maxor/test.txt", $variable);
echo implode("\n", $variable);

Comments

0

system is calling the actual cat program, which is outputting "blah" from test.txt. It also returns the value back to $variable which you're then printing out again.

Use exec or shell_exec instead of system.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.