Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/148475-solved-echoinggggg/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/148475-solved-echoinggggg/#findComment-779676
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/148475-solved-echoinggggg/#findComment-779682
Share on other sites

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";

Link to comment
https://forums.phpfreaks.com/topic/148475-solved-echoinggggg/#findComment-779771
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/148475-solved-echoinggggg/#findComment-780101
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/148475-solved-echoinggggg/#findComment-780197
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/148475-solved-echoinggggg/#findComment-780207
Share on other sites

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 ?

Link to comment
https://forums.phpfreaks.com/topic/148475-solved-echoinggggg/#findComment-780221
Share on other sites

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.

 

 

Link to comment
https://forums.phpfreaks.com/topic/148475-solved-echoinggggg/#findComment-780230
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/148475-solved-echoinggggg/#findComment-780257
Share on other sites

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....

Link to comment
https://forums.phpfreaks.com/topic/148475-solved-echoinggggg/#findComment-780268
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/148475-solved-echoinggggg/#findComment-780342
Share on other sites

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. =)

Link to comment
https://forums.phpfreaks.com/topic/148475-solved-echoinggggg/#findComment-780350
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.