How to pass the value of a PHP variable to a fixed place?

Asked

Viewed 55 times

0

Good evening gentlemen I am venturing into the world of PHP and I came across a problem that I have not yet been able to solve.

I made a connection in PHP that connects in a TELNET server and returns me the values read in a variable inside a While, with that I use an echo to print the message on the screen to follow the content of the message.

   while ($line = fgets($socket,7170)){
    $line = trim($line);
    echo $line."<br>";
}

something like this, but the result is that it generates a new line with the answer.

I would like to know a way to print this variable always in the same location.

Like a field saying " STATUS: $VALUE OF THE VARIABLE HERE" so that whenever I changed the value update there and not create a new echo.

  • How this is updated?

1 answer

0

There are two ways to do this.

First you taking the tag off <br /> which would prevent your loop from causing the value to jump from line, however, if you want the values all in the same variable you could be doing as follows:

$line = "";
while ($line = fgets($socket,7170))
{
    $line .= trim($line);
}

echo $line;
  • I don’t know why you didn’t print the values in this structure, if I put echo inside while, it prints every time it changes but, it’s in sequence it’s like $line$line$line$line$line$line$line$line$line$line$line$line$line$line$line$line line(the value of the variable) on the page. What I want to know is how the value overwrites the previously printed value.

  • If you use = you reassign the value in the variable, however, when you use .= you concatenate the value in the varialve.

  • 1

    the question of .= I understand now, thank you for sharing this information with me. The point now is that while constantly runs getting the status of my server. , but when you put it as it is in the code, it shows no information. My idea is that as if it were an html field with status and always change with the server.

  • I understand, but was the answer the solution to your question? If so, please check it as the solution to help other users as well.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.