I have this simple php script which outputs a string every second.
<?php
$i = 1;
while(1)
{
exec("cls"); //<- Does not work
echo "test_".$i."\n";
sleep(1);
$i++;
}
I execute the script in the command shell on windows (php myscript.php) and try to clear the command shell before every cycle. But I don't get it to work. Any ideas?
How about this?
<?php
$i = 1;
echo str_repeat("\n", 300); // Clears buffer history, only executes once
while(1)
{
echo "test_".$i."\r"; // Now uses carriage return instead of new line
sleep(1);
$i++;
}
the str_repeat() function executes outside of the while loop, and instead of ending each echo with a new line, it moves the pointer back to the existing line, and writes over the top of it.
can you check this solution
$i = 1;
echo PHP_OS;
while(1)
{
if(PHP_OS=="Linux")
{
system('clear');
}
else
system('cls');
echo "test_".$i."\n";
sleep(1);
$i++;
}
Apparently, you have to store the output of the variable and then print it to have it clear the screen successfully:
$clear = exec("cls");
print($clear);
All together:
<?php
$i = 1;
while(1)
{
$clear = exec("cls");
print($clear);
echo "test_".$i."\n";
sleep(1);
$i++;
}
I tested it on Linux with clear instead of cls (the equivalent command) and it worked fine.
Duplicate of ➝ this question
Under Windows, no such thing as
#exec('cls');
Sorry! All you could possibly do is hunt for an executable (not the cmd built-in command) like here...
You have to print the output to the terminal:
<?php
$i = 1;
while(1)
{
exec("cls", $clearOutput);
foreach($clearOutput as $cleanLine)
{
echo $cleanLine;
}
echo "test_".$i."\n";
sleep(1);
$i++;
}
if it is linux server use following command (clear)
if it is window server use cls
i hope it will work
$i = 1;
while(1)
{
exec("clear"); //<- This will work
echo "test_".$i."\n";
sleep(1);
$i++;
}
second solution
<?php
$i = 1;
echo PHP_OS;
while(1)
{
if(PHP_OS=="Linux")
{
$clear = exec("clear");
print($clear);
}
else
exec("cls");
echo "test_".$i."\n";
sleep(1);
$i++;
}
This one worked for me, tested also.
Related
I'm making PHP script to move shell script. when hoge < 50.
Code is this. Does it OK?
When I use that script , I click this link.
http://localhost/index.php?hoge=30
<?php
if(isset($_GET['hoge'])) {
$hoge = $_GET['hoge'];
print("$hoge<br>\n");
}
if ($hoge > 50 ){
echo 'F1';
}
elseif ($hoge == 50) {
echo 'F2';
} else {
echo 'F3';
exec(' /Users/hoge/Desktop/test.sh');
}
?>
You can add a test like
if (file_exists('/Users/hoge/Desktop/test.sh')) {
exec('/Users/hoge/Desktop/test.sh');
} else {
echo 'NO FILE'
}
Check also that the file /Users/hoge/Desktop/test.sh is executable
Is it possible to have one code block execute when the loop runs throught once, then every time after it run a different code block?
for example, I have:
while ($blank > 0) {
echo "<td></td>";
$blank--;
$day_count++;
}
I would like to change it to this for the first time it runs:
echo "<td class = 'left'></td>";
Then after the first loop through, go to this:
echo "<td></td>";
Is that possible at all?
$firstrow = true;
while ($blank > 0) {
if ($firstrow) {
echo "<td class = 'left'></td>";
$firstrow = false;
} else {
echo "<td></td>";
}
$blank--;
$day_count++;
}
It's not exactly clear from your question what "first time" means for you. But in principle, you should be able to create a variable that indicates whether this is the first time. For example, you can use isset:
while($blank>0) {
if !isset($notTheFirstTime){
...
}
else {
}
$notTheFirstTime = 1
}
The first time you encounter this loop, the variable $notTheFirstTime does not yet exist, and the first block of code is run; once you've been through the loop once, the variable exists. That means this will only run once. On the other hand, if you want to do something "on the first pass through the loop, every time I encounter the loop" then I would recommend just putting the relevant code outside of the loop, rather than inside.
This should work for you:
<?php
//$blank = 3;
//$day_count = 0;
for($i = $blank; $blank > 0; $blank--) {
if($i == $blank)
echo "<td class = 'left'></td>";
else
echo "<td></td>";
$day_count++;
}
?>
Output:
<td class = 'left'></td><td></td><td></td>
I want to echo some output before and after some pause.
Here is my Code:
<?php
class AsyncOperation extends Thread {
public function __construct(){
}
public function run(){
sleep(11);
echo "Running ";
}
}
echo "Its Here";
flush();
$thread = new AsyncOperation();
$thread->start();
?>
This should show output "Its Here" ....and after 11 seconds ... should show "Running"
but the browser shows it like "Running Its Here", and shows the whole string after 11 seconds.
I don't know why this is happening . Please help me solve this problem I have been struck here for last 2 nights.
Any Solution ??
You need Output Buffering...!
I am not sure this works on all browsers, However this works perfectly on
Google's Chrome.
Mozilla FireFox
Internet Explorer
Opera
<?php
if (ob_get_level() == 0) ob_start();
class AsyncOperation
{
public function run()
{
for ($i = 0; $i < 10; $i++) {
echo " ";
echo str_pad('', 4096) . "\n";
# Flushing out..........!
ob_flush();
flush();
sleep(1);
}
echo "Running ";
}
}
echo "Its Here<br>";
$thread = new AsyncOperation();
$thread->run();
?>
I modified your run() with sleep(1) under a for loop which is same as sleep(11).
I have 2 seperate files.
The iframe executes the changePercent command in the parent window, but it doesn't change the number instantly, it is only changed after the loop finished. Is there any way to fix this? Need this for a progress bar thingy.
Thanks in advance!
This is the file I open in my browser
<span id="test">1</span><br />
<iframe src="script.php?league=Nemesis"></iframe>
<script type="text/javascript">
function changePercent(val) {
document.getElementById("test").innerHTML=val;
}
</script>
This is the embedded iframe
<?php set_time_limit(0);
include('../assets/includes/functions.inc.php'); ?>
<?php
$i = 0;
$step = 200;
$end = 15000 - $step;
$league = $_GET['league'];
$found = false;
$status = false;
while ($found == false && $i < $end) {
if($i < $end) { $i = $i + $step; }
$ladder = file_get_contents("http://api.pathofexile.com/ladders/".$league."?limit=".$step."&offset=".$i);
$ladder = str_replace('"online":false', '"online":"no"', $ladder);
$ladder = str_replace('"online":true', '"online":"yes"', $ladder);
$json = json_decode($ladder, true);
foreach ($json['entries'] as $address) {
if($address['online'] = "yes") {
$status = true;
// do something
}
}
?>
<script type="text/javascript">
parent.changePercent('<?php echo $i ?>');
</script>
<?php
}
?>
php is a server side language. when the php runs the while loop, that means the browser is still waiting for page data, and it will be sent to the browser only when the php is finished.
in your changePercent you have a php code that is located AFTER the while loop, so it will run the while loop before it can evaluate the php inside the changePercent
so there is no "fix", you should just learn the basics of web programming
You're exiting out of the PHP loop when you use ?> try the following code segment.
echo '<script type="text/javascript"> parent.changePercent(' . $i .'); </script>';
This will tell PHP to print out that line for each time your loop executes through.
i have a for loop that will loop through a set of page in php.
for ($count=0;$count<=$curr;$count=$count + 10)
{
if ($find==1) {
$result = "file.php?count=$count";
}else {
$result = "file2.php?count=$count";
}
$match = file_get_contents($result);
$nc=$count+10;
if (preg_match("/\b$file\b/i", $match)) {
print "found in $count";
} else {
print "not found in $count";
}
}
the problem is that the result is displayed after the last page is executed, since it will loop through 500 pages which takes more time. so how can i make this code display the print result as it is executed each cycle,
while(something)
{
// do something
echo "Hi";
flush();
}
Using the flush() function will output everything to the browser that has been sent so far (sent as in echo, print or other similar functions.).