Xu Wei Jie Posted March 8, 2009 Share Posted March 8, 2009 I need help with the echo command. As I am trying to implement some debugging function, my application always echo outputs to files because my application interacts with them. However for the debugging function, I wish to echo output to the console so user can see the values. How do I manipulate the standard buffer to tell it when to echo to file and when to echo to the console? TIA Quote Link to comment Share on other sites More sharing options...
DjMikeS Posted March 8, 2009 Share Posted March 8, 2009 Do you use some sort of output buffering ? because echo, by default, does not echo to a file...it just echoes.... Quote Link to comment Share on other sites More sharing options...
Xu Wei Jie Posted March 8, 2009 Author Share Posted March 8, 2009 Yup, I think I used (i.e. system("php xxx.php > outputfile.txt",$retva)l ) and all the buffer is directed to the file outputfile.txt . Quote Link to comment Share on other sites More sharing options...
Mchl Posted March 8, 2009 Share Posted March 8, 2009 You're redirecting standard output to a file. You can't expect the script to echo anything to screen. That's not a PHP issue. Quote Link to comment Share on other sites More sharing options...
Xu Wei Jie Posted March 8, 2009 Author Share Posted March 8, 2009 Can I intercept directing of the standard buffer to the console and after which continue directing the standard buffer to the file? Are there any functions to do that? I am looking at output buffering functions to see if I can come up with such a stunt. Quote Link to comment Share on other sites More sharing options...
Mchl Posted March 8, 2009 Share Posted March 8, 2009 Or maybe you should use file functions to save output to file, and use echo for displaying messages? Quote Link to comment Share on other sites More sharing options...
Xu Wei Jie Posted March 8, 2009 Author Share Posted March 8, 2009 i thought so too. But actually it is not good enough for me. I wanted everything to be output to files, so echoing to my files would be natural for me just like how scripts are output to browsers after html and php tag processing. But only in occasions where I need to debug, I wanted it to show on the console. For typical web applications, debug errors are output on the browser by default. Quote Link to comment Share on other sites More sharing options...
MadnessRed Posted March 8, 2009 Share Posted March 8, 2009 would the print function work? Sorry if that's a stupid idea. I don't know a lot about echoing to files, I never knew it was even possible Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 8, 2009 Share Posted March 8, 2009 The fact that you are directing the console output to a file is the limiting factor. You might be able to set display_errors to stderr and then use the trigger_error function to trigger user errors instead of just echoing the debugging information. Quote Link to comment Share on other sites More sharing options...
trq Posted March 8, 2009 Share Posted March 8, 2009 If you write your error messages to stderr they will be displayed on the screen, and unless you pipe stderr to your file thats where they will stay. To write to stderr using php use.... $stderr = fopen('php://stderr', 'w'); fwrite($stderr,'this is an error message'); Quote Link to comment Share on other sites More sharing options...
Xu Wei Jie Posted March 8, 2009 Author Share Posted March 8, 2009 How do I set display_error to stderr? print function and trigger_error function all output to the files. Thorpe, I tried your method. It was written to the srderr file but how do I get it on the console? Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 8, 2009 Share Posted March 8, 2009 In a script - ini_set("display_errors", "stderr") In php.ini - display_errors = "stderr" Quote Link to comment Share on other sites More sharing options...
Xu Wei Jie Posted March 8, 2009 Author Share Posted March 8, 2009 Thanks. I tried your method and like the alternative Thorpe suggested, it wasn't output to the files but neither it appeared on the console. Quote Link to comment Share on other sites More sharing options...
DjMikeS Posted March 8, 2009 Share Posted March 8, 2009 I think you should write your scripts so that you don't have to pipe the console output to a file. function writeFile($var) { $file = fopen('yourfile', 'w'); fwrite($file, $var); fclose($file); } And then when you want to output to a file: //instead of echo: writeFile("This will be written to a file"); echo "This will be outputted to the console"; Quote Link to comment Share on other sites More sharing options...
Xu Wei Jie Posted March 9, 2009 Author Share Posted March 9, 2009 Actually I also do want a write file / write to console approach. But my application design is just not suited for it. I wrote to stderr and i did all the necessary modifications to ini file ini file : display_errors="stderr" in script : ini_set("display_errors","stderr"); I tried trigger_error("error");. It does not get its output to console. I tried fwrite(STDERR,"error"); It also does not get output into console. Good thing is the output is not piped into file. lol Quote Link to comment Share on other sites More sharing options...
DjMikeS Posted March 9, 2009 Share Posted March 9, 2009 Well, I really don't see why you couldn't replace your echo's with writeFile function. The only thing you need to do is include the function in your application... I think your making it harder then it should be. Could you please explain why your application is not suited for the aforementioned approach? Quote Link to comment Share on other sites More sharing options...
Xu Wei Jie Posted March 9, 2009 Author Share Posted March 9, 2009 I am spawning new php CLI processes using system(); i.e. system("php run.php $new_id > file.php",$retval); My application is using a tree-like data structure where each node is a script by itself which recursively invokes system() to spawn more instances. I cannot use writeFile() because I need the script at each node to be processed by CLI in an 'include' like fashion which means it involves echoing. I don't write line by line. I direct all the output that is processed to the files. I tried to forgo using system() but unfortunately, I need to spawn new php instances and system() does not return me a buffer that I can using file functions to write to. Thus, the only alternative is to suspend processing into the standard buffer, output into the console and continue processing into the standard buffer. Else it would be using STDERR, directing the debug message to STDERR and this works because it does not get piped into the files. But weirdly, I can't see it on the console screen either Quote Link to comment Share on other sites More sharing options...
DjMikeS Posted March 9, 2009 Share Posted March 9, 2009 I cannot use writeFile() because I need the script at each node to be processed by CLI in an 'include' like fashion which means it involves echoing. It still don't understand why you can't replace the echo's with a function that write's to a file... Maybe it's just me... I don't write line by line. I direct all the output that is processed to the files. PHP parses line by line....so every echo could be replaced... Am I thinking in the wrong direction or are you making it harder then it needs to be ? Quote Link to comment Share on other sites More sharing options...
Xu Wei Jie Posted March 9, 2009 Author Share Posted March 9, 2009 PHP does parse line by line. i.e. xxx.php aaaaa <?php echo "output"; ?> bbbbb I wish to write to file xxx.txt aaaaa output bbbbb but if I use a write file approach, probably I need to do something like this ob_start(); include("xxx.php"); $contents = ob_get_contents(); $f=openfile("xxx.txt",'w') fwrite($f,$contents) fclose($f); ob_end_clean(); I can't simply replace 'echo' with the function because I need the script to be processed before writing to a file. And I cannot use include("xxx.php"); as well because I am calling system("php xxx.php > xxx.txt",$retval); The later system call enables me to spawn multiple php instances but include only allows me to work with 1 php instance. So it is not a simple write line by line kinda of approach. I am stuck with system() and it is a limiting factor for me to direct output to console thus I am trying out other ways to do so. Quote Link to comment Share on other sites More sharing options...
DjMikeS Posted March 9, 2009 Share Posted March 9, 2009 Why not use the output buffer in your xxx.php ? Quote Link to comment Share on other sites More sharing options...
Xu Wei Jie Posted March 9, 2009 Author Share Posted March 9, 2009 Basically it is the same. If I use output buffer in xxx.php <?php ob_start();?> aaaaa <?php echo "output"; ?> bbbbb <?php $contents = ob_get_contents(); ob_get_clean(); $file = $argv[1]; $f=fopen($file,'w'); fwrite($f,$contents); fclose($f); ?> Using such an approach, I can call system("php xxx.php xxx.txt",$retval); It will be an extra step to pass the file name. And at the same time, it still does not solve the problem cause output buffer still process the file from head to toe. And I can't insert any debug messages with echo within because it will just be processed into the buffer as well and back to the file. Worst case scenario is that i do ob_start() , ob_end_clean() and append the processed output to file before I echo the debugging message to console and after ob_start() again and...it follows. This is not an approach I would adopt because it is too cumbersome. Quote Link to comment Share on other sites More sharing options...
DjMikeS Posted March 9, 2009 Share Posted March 9, 2009 I still don't understand why you don't create a function to write to a file....then you don't have to pipe the output to a file because the script does that by itself... You could also turn the whole thing around and write a function that echo's, which is outside your output buffer. function doEcho($sayWhat) { echo $sayWhat; } I really think you need to get away from the whole 'piping to file' concept.... Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted March 9, 2009 Share Posted March 9, 2009 I still don't understand why you don't create a function to write to a file Because his use of the system() function. DjMikeS you are correct in an everyday approach to have a function that writes to a file, however not when the system output buffer is directly to a file system("php xxx.php > outputfile.txt",$retval) As Xu Wei Jie has stated the whole script needs to be processed prior to anything being written! Unless the approach to using the stderr works I see no alternative. Quote Link to comment Share on other sites More sharing options...
Xu Wei Jie Posted March 9, 2009 Author Share Posted March 9, 2009 neil.johnson, you are correct. system() is limiting me to output to console and yet I have no choice but to use it. I did tried fwrite(STDERR,"error"); and did some experimentation. Without the involvement of system() piping the output to file, it does print to console. But if with it, the output is not piped into the file but neither it appears on the console. I suspect it is piped into /dev/null. I am still figuring out a way to do this. =) Quote Link to comment Share on other sites More sharing options...
Xu Wei Jie Posted March 9, 2009 Author Share Posted March 9, 2009 I think I have figured it out system("php xxx.php 2>&1 1>xxx.txt",$retval); Output directed to stderr will be flushed to the console and stdout will be directed to the file. Cheers =) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.