PHP Take a phrase value from within the array

Asked

Viewed 74 times

-2

People below I have a code that checks the ping on MS of a url or IP but what I need is to take the value of the average of the result that is within the array [10]

Example :

M dia = 74ms

I just wanted the value 74 in a echo.

Below is a test code

<?php
function pingAddress($ip) {
    $pingresult = exec("ping  -n 3 $ip", $outcome, $status);
    if (0 == $status) {
        $status = "alive => ( ".print_r($outcome[10])." )";
    } else {
        $status = "fora";
    }
}
pingAddress("www.google.com");

?>
  • $outcome[10] is an array or is an array value?

  • @sam he is taking the value of the 11th position of the executed variable in exec()

  • Ask the question the result of print_r(pingAddress("www.google.com"));

1 answer

-1

You can make a runtime calculation using the microtime()

<?php
function ping($host, $port = 80, $timeout = 10) { 
  $tB = microtime(true); // inicia um valor de timestamp 
  $fP = fSockOpen($host, $port, $errno, $errstr, $timeout); // checagem da url pretendida na porta desejada por um tempo determinado
  if (!$fP) { return "down"; } 
  $tA = microtime(true); // inicia um segundo valor de timestamp 
  return round((($tA - $tB) * 1000), 0)." ms"; // calcula o tempo de execução 
}

//Mostrando o ping caso esteja executando se não exibe 'down'
echo ping("www.google.com", 80, 10); 
  • Turned out pretty cool as I do to check by IP without port just drip the ip am putting ip 192.168.50.17 where I want to drip this giving a wrong Warning: Missing argument 3 for ping

  • that ip by windows cmd is giving 7ms

  • Hello @Erlon Charles

  • When you ping on CMD, even without putting the port you are accessing port 80, then you can set it as default by setting function ping($host, $port = 80, $timeout = 10) { . that way just put ping('www.google.com')

Browser other questions tagged

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