How do you check if a variable has numeric content?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

How do you check if a variable has numeric content?

Post by simonmlewis »

Code: Select all

is_numeric (var_name)
I know this code checks if it is numeric, but what if I want to know if $ping has numeric content?

Code: Select all

$ping = "32ms";
is_numeric won't take that as being numeric. I need to know if there is numeric content *within* the variable.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How do you check if a variable has numeric content?

Post by requinix »

If you consider "ksjvdyfby3sdkhfb" to have numeric content then all you're doing is looking for a digit in there.

Code: Select all

preg_match('/\d/', $variable)
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you check if a variable has numeric content?

Post by simonmlewis »

It doesn't matter.
I'm building a little tool where customers can ping their site, through ours. So they can see the server response time, without having to work out the CMD ... PING tool in Windows.
The result that comes back is like 32ms... but if the result is an error, I want to spot if it has numbers in it.

Code: Select all

<div class='width-narrow'>
<?php
$domain = isset($_GET['domain']) ? $_GET['domain'] : null;

function ping($host, $port, $timeout) { 
  $tB = microtime(true); 
  $fP = fSockOpen($host, $port, $errno, $errstr, $timeout); 
  if (!$fP) { return "down"; } 
  $tA = microtime(true); 
  return round((($tA - $tB) * 1000), 0)." ms"; 
}

if (isset($domain))
{
$ping =  ping("$domain", 80, 10); 
}
echo "<div class='site-speed'>
<div class='site-speed-clock'>";
if (!isset($domain)) { echo "<i class='fa fa-clock-o' aria-hidden='true'></i>";}
if (isset($domain) && (1 === preg_match('~[0-9]~', $ping)))
{
if ($ping > 100) { echo "<i class='fa fa-frown-o' aria-hidden='true' style='color: #ff0000'></i>";}
if ($ping < 100 && $ping > 60) { echo "<i class='fa fa-meh-o' aria-hidden='true' style='color: #ff5500'></i>";}
if ($ping < 60) { echo "<i class='fa fa-smile-o' aria-hidden='true' style='color: #009900'></i>";}
}

echo "</div>";
if (!isset($domain))
{
echo "Enter your full URL of your website here and click Go.";
}

if (isset($domain) && (1 === preg_match('~[0-9]~', $ping)))
{
//Echoing it will display the ping if the host is up, if not it'll say "down".

echo "The speed Google pings $domain at is: <b>$ping</b>";
if ($ping > 100) { echo "<br/><font color='#ff0000'>This is rather slow, and slow sites are difficut for consumers, and Google will penalize you for it.</font>";}
if ($ping < 100 && $ping > 60) { echo "<br/><font color='#ff5500'>This is quite a good speed.<br/>No real issues, though could be quicker.<br/>Try a F5 to refresh to see if the speed reduces.</font>";}

if ($ping < 60) { echo "<br/><font color='#009900'>This is very good.  No issues - very fast!</font>";}
}
if (isset($domain) && (1 !== preg_match('~[0-9]~', $ping)))
{
echo "Sorry the domain you entered is either invalid, or the site you entered is down.";
}

echo "<form method='GET' action='index.php'>
<input type='hidden' name='page' value='website-speed'>
<input type='text' name='domain' ";
if (isset($domain)) { echo "value='$domain'";}
echo "><input type='submit' value='Go'></form></div>";
?><br/>
</div>
The other issue is that when you do an actual CMD Ping, it does it four times.
Trying to work out how I can make this function do it four times with four separate results in four variables.

Apart from literally duplicating the code and renaming it all... can you see a way to do that?
Then I could take the four values, get the average and give the result.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do you check if a variable has numeric content?

Post by Celauran »

simonmlewis wrote:Trying to work out how I can make this function do it four times with four separate results in four variables.
You mean like a for loop?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you check if a variable has numeric content?

Post by simonmlewis »

Sure, but I would need the function to go into a loop, and then to store them into four variables... wouldnt' it?
Not sure how to do that.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do you check if a variable has numeric content?

Post by Celauran »

Why four variables? Why not just use an array?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do you check if a variable has numeric content?

Post by Celauran »

Alternately, you could pass the count as a parameter to the ping function and have the for loop inside the function.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you check if a variable has numeric content?

Post by simonmlewis »

Alternately, you could pass the count as a parameter to the ping function and have the for loop inside the function.
How...??
Also, I'm still not that familiar with Arrays.
This would also be a set of 4, like the cmd/ping is.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do you check if a variable has numeric content?

Post by Celauran »

What if you did something like this instead?

Code: Select all

function ping($host, $count = 4, $port = 80, $timeout = 10) {
    $ping_times = [];

    for ($i = 0; $i < $count; $i++) {
        $start_time = microtime(true);
        $fp = fsockopen($host, $port, $errno, $errstr, $timeout);

        if (!$fp) {
            throw new Exception('Not a valid hostname or domain is down');
        }

        $end_time = microtime(true);
        fclose($fp);
        $response_time = round((($end_time - $start_time) * 1000), 0);
        $ping_times[] = $response_time;
    }

    return $ping_times;
}
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you check if a variable has numeric content?

Post by simonmlewis »

Ok so $count is set to 4. $i counts up to 4, and while it does that it loops through it.
The $response_time is put into the $ping_times array.
So how do I then extra it from that array, and get an average of the four?
Or.... extra them to show the four results, and calculated based on an average?

I assume something like $pingavg = avg($pine-array);
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do you check if a variable has numeric content?

Post by Celauran »

You could iterate over them using foreach. Average could be as simple as

Code: Select all

$pings = ping('domain.com');
$average = array_sum($pings) / count($pings);
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you check if a variable has numeric content?

Post by simonmlewis »

Code: Select all

function ping($host, $count = 4, $port = 80, $timeout = 10) {
    $ping_times = [];

    for ($i = 0; $i < $count; $i++) {
        $start_time = microtime(true);
        $fp = fsockopen($host, $port, $errno, $errstr, $timeout);

        if (!$fp) {
            echo "sorry this is not right";
        }

        $end_time = microtime(true);
        fclose($fp);
        $response_time = round((($end_time - $start_time) * 1000), 0);
        $ping_times[] = $response_time;
    }

    return $ping_times;
}

if (isset($domain))
{
$ping =  ping("www.bbc.co.uk", 80, 10); 
$average = array_sum($ping_times) / count($pings);
}


echo "$average";
So this should show a result ??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do you check if a variable has numeric content?

Post by Celauran »

That's going to try 80 times on port 10...

Also, $pings and $ping_times do not appear to be defined.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do you check if a variable has numeric content?

Post by Celauran »

Code: Select all

<?php

function ping($host, $count = 4, $port = 80, $timeout = 10) {
    $ping_times = [];

    for ($i = 0; $i < $count; $i++) {
        $start_time = microtime(true);
        $fp = fsockopen($host, $port, $errno, $errstr, $timeout);

        if (!$fp) {
            throw new Exception('Not a valid hostname or domain is down');
        }

        $end_time = microtime(true);
        fclose($fp);
        $response_time = round(($end_time - $start_time) * 1000);
        $ping_times[] = $response_time;
    }

    return $ping_times;
}

$times = ping('www.bbc.co.uk');

echo "<p>Average response time: " . round(array_sum($times) / count($times)) . "ms</p>";

foreach($times as $time) {
    echo $time . "ms<br>";
}
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you check if a variable has numeric content?

Post by simonmlewis »

Gotcha. Clever. I wondered why it really churned before throwing a bunch of errors.
Only problem now is that if I enter "asdf" into a field and post it (_GET) to $domain, it throws a ton of errors, rather than throwing it out.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply