I'm debugging a PHP script that runs a couple SQL queries and emails a set of users. I'm sure this is a very basic thing, but every time I try to echo, print, or print_r it doesn't appear while running the script.
So say I have this in the script:
print("This should print");
echo "on the command line";
When I run the script via command line php script.php it doesn't actually print anything to the command line while running the script.
Is there a way of having PHP print to console? I feel like I'm missing something extremely basic here.
Thats the method of doing it.
Things to check for are output buffering
http://php.net/manual/en/function.ob-flush.php
Is that code actually being run ? Make sure it doesnt branch before it gets there
A good approach could be:
function log($message)
{
$message = date("H:i:s") . " - $message - ".PHP_EOL;
print($message);
flush();
ob_flush();
}
It will output to your terminal each line you call. Just use :
log("Something");
log("Another line");
The following code working fine for me.
<?php
print("This should print");
echo "on the command line";
?>
with tags.
I know it is old, but I am going post my own solution to this just in case if someone would run into similar situation. I had a problem that a legacy command line PHP script wouldn't print or echo anything to the terminal when DSN is incorrectly configured and the script got stuck for very long time (not sure how long, never waited for it to terminate by itself). After putting ob_end_flush() in the entry line of the script, the output came back to the terminal. So it turned out that all output was buffered and since the script stuck at some point, the output stayed buffered and so never went to the terminal.
Related
I have a PHP file that currently runs via my console on my Mac. It's a private piece of software that interacts with an API via a loop that runs every x seconds.
At the minute Im am echoing out all information during each loop but the problem is within console it just repeats the content and appends it on to the previous. Is there a way of clearing all previous echoed results after each loop?
I thought I could use \r to replace the line but that doesn't seem to be working. I have also tried a few other examples after searching on here without any luck.
while(1){
echo "Some info goes here";
sleep(5);
}
For example if I try the following in a while loop
while(1){
echo "==============\r\n";
echo "==============\r\n";
sleep(5);
}
I end up with after a hand full of loops
Thanks
Two ways that I can think of off the top of my head:
Use the ANSI escape sequence to clear the screen:
print chr(27) . "[2J" . chr(27) . "[;H";
Run the command that clears the screen for your platform:
system("cls"); for windows
system("clear"); for unix-like systems
echo "\033[2J\033[;H";
Should clear your terminal.
If you just want to overwrite the same line again, this is working for me:
<?php
while(1) {
echo "test\r";
sleep(1);
}
?>
ok, I know I'm doing something weird here but I gotta do it.
So my elixir program needs to run one PHP script and take the information from that script, but it never comes in. I can manually run the php script and it works fine. At the bottom of the script it says:
echo $avar->getPUId();
So it's printing it out to echo I guess. I get the data I need when I run it from a command line:
C:\PHP>php.exe "-f" "C:\pap.php" "613b8859"
37a69912
But when I run it from iex I get nothing:
iex(1)> System.cmd("C:\\PHP\\php.exe", ["-f", "C:\\pap.php", "613b8859"])
Terminate batch job (Y/N)? y
Now, if I mess up on the filename or something php will give me an error in response saying like:
iex(1)> System.cmd("C:\\PHP\\php.exe", ["-f", "C:\\pap321.php", "613b8859"])
{"Could not open input file: c:/pap123.php\n", 1}
...
so I know I can talk to PHP, and it can talk back to me with errors, but echo isn't caught by my elixir application that calls it apparently. Is there any other way I can grab the data as it comes out of the PHP script?
I used
https://github.com/alco/porcelain
result = Porcelain.shell("C:\\PHP\\php.exe -f C:\\pap.php 613b8859")
IO.inspect result.out
it worked great.
I am trying learn to execute shell scripts from within PHP code. So, I made a test program to execute a bash script from within PHP. However, it has no effect. The relevant code is shown below.
<?php
.......
shell_exec('/bin/bash /var/www/html/just_touch.sh');
?>
The just_touch.sh script just creates a new file, like as shown below.
touch /home/user/some.txt
I was expecting to have file /home/user/some.txt after execution, but no, it isn't made. What mistake, am I doing?
P.S: The following code works though.
$output = shell_exec('ls /home/user');
echo $output;
Does this have anything to do with permissions?
Moreover, I notice that while this prints "Can you see me?".
$output = shell_exec('echo Can you see me?');
echo $output;
This doesn't!
shell_exec('echo Can you see me?')
What is going on here?
Stderr is lost when using shell_exec. You might wan't to use:
shell_exec('/bin/bash /var/www/html/just_touch.sh 2>&1');
how do you issue commands to the shell and then forget it's output and such? for example:
<?php
echo `sleep 2;echo hi`;
echo "foo";
?>
the result for this is hifoo. i would want a result that gives me foohi. why? i want the command issued to the shell simply issued and forgotten, i am confused about why PHP will wait for the result. is such a result possible?
(the idea behind this is setting up the correct number of selenium grid RC instances programatically. currently, it will stop after the first process is opened)
From php.net exec()
If a program is started with this
function, in order for it to continue
running in the background, the output
of the program must be redirected to a
file or another output stream. Failing
to do so will cause PHP to hang until
the execution of the program ends.
The same applies for all shell commands.
It's like anything you do at the command prompt, unless you take measures to put the new command in the background, the shell (and PHP ) will block until the command exits. Try this:
<?php
echo `(sleep 2; echo hi) &`;
echo 'foo';
?>
Note the brackets around your command, without that, the & woulud apply only to the echo , and you'd still have the 2 second pause.
After so much trouble I find out that when I use the flush function in my PHP mail script then I get garbage or dump characters on browser like below.
The code is below
if ($mail->Send()) {
echo "<br><font color=darkgreen>[$num successful send to $to]</font> ";
// flush();
return true;
}
If I comment that flush line then out is simple English but I uncomment that the whole page the text looks like garbage.
Now is that a PHP problem, browser problem or server problem?
If I use the same script from the shell, I mean execute inside the shell terminal then I can see the HTML output. But it does not work in browsers.
I found the answer to my own question. I had to turn
zlib_compression off
in my php.ini settings file.
(What does that mean and why did it work?. I had been trying this for 1 year but could not resolve the problem but now it worked.)