Re: How do you check if a variable has numeric content?
Posted: Fri Feb 17, 2017 6:39 am
It should throw one exception, which you'll need to catch. If it's doing something beyond that, post the code you're using and what's happening.
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
<div class='width-narrow'>
<?php
$domain = isset($_GET['domain']) ? $_GET['domain'] : null;
if (isset($domain))
{
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;
}
$times = ping('$domain');
echo "<p>Average response time: " . round(array_sum($times) / count($times)) . "ms</p>";
foreach($times as $time) {
echo $time . "ms<br>";
}
}
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>Code: Select all
<div class='head'><h1>How fast is your website?</h1></div>
<div class='width-narrow'>
<p>A website's speed is down to server response time, then processing time, and of course images and data time.<br/>
If your website is packed full of images, and your server is slow, this will affect your ranking.<br/>
This simple tool will tell you if your site's server (not the processing of data or images) is good or bad.</p>
<?php
$domain = isset($_GET['domain']) ? $_GET['domain'] : null;
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;
}
$times = ping('$domain');
echo "<p>Average response time: " . round(array_sum($times) / count($times)) . "ms</p>";
foreach($times as $ping) {
echo $ping . "ms<br>";
}
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/>
<h2>How does it find out the speed?</h2>
<p>The tool will 'ping' your website. It just sends it a little message 'are you there', and it will report back if it is there, and if the time in milliseconds that it took to respond and send back a message.</p>
<p>
If you wish to do this yourself NOT via this tool, just open 'CMD' prompt, and enter 'ping {site domain name}'.
</p>
<p style='text-align: center'><img src='/images/ping.jpg' alt='ping' /></p>
<p>This tool does all that for you via a short system. It is best to run it 3 or 4 times. After you have run it. Just hit F5 and it will run again, and just take an average. The tool will tell you if it is slow, ok, or really good.</p>
</div>Code: Select all
<?php
// This section is where we're going to have the domain logic
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("Unable to open connection to '${host}'");
}
$end_time = microtime(true);
fclose($fp);
$response_time = round((($end_time - $start_time) * 1000), 0);
$ping_times[] = $response_time;
}
return $ping_times;
}
$pings = [];
$error_message = '';
// Grab the domain if one has been set
$domain = isset($_GET['domain']) ? $_GET['domain'] : null;
if (isset($domain)) {
// Try to get ping times. If something goes wrong, we catch the exception
// and record an error message for display
try {
$pings = ping($domain);
} catch (Exception $e) {
$error_message = "Sorry the domain you entered is either invalid, or the site you entered is down.";
}
}
?>
<?php /* We're going to keep the display logic below */ ?>
<div class="head"><h1>How fast is your website?</h1></div>
<div class="width-narrow">
<p>
A website's speed is down to server response time, then processing time, and of course images and data time.<br/>
If your website is packed full of images, and your server is slow, this will affect your ranking.<br/>
This simple tool will tell you if your site's server (not the processing of data or images) is good or bad.
</p>
<div class="site-speed">
<div class="site-speed-clock">
<?php if (!isset($domain)): ?>
<i class="fa fa-clock-o" aria-hidden="true"></i>
<?php endif; ?>
</div>
<?php if (!isset($domain)): ?>
Enter your full URL of your website here and click Go.
<?php endif; ?>
<?php if (count($pings)): ?>
The speed Google pings <?php echo $domain; ?> at is:<br>
<?php foreach ($pings as $ping): ?>
<b><?php echo $ping; ?>ms</b>
<?php if ($ping > 100): ?>
<i class='fa fa-frown-o' aria-hidden='true' style='color: #ff0000'></i>
<font color='#ff0000'>This is rather slow, and slow sites are difficut for consumers, and Google will penalize you for it.</font>
<?php elseif ($ping <= 100 && $ping >= 60): ?>
<i class='fa fa-meh-o' aria-hidden='true' style='color: #ff5500'></i>
<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>
<?php elseif ($ping < 60): ?>
<i class='fa fa-smile-o' aria-hidden='true' style='color: #009900'></i>
<font color='#009900'>This is very good. No issues - very fast!</font>
<?php endif; ?>
<br>
<?php endforeach; ?>
<?php elseif (!empty($error_message)): ?>
<span class="error"><?php echo $error_message; ?></span>
<?php endif; ?>
<form method="GET" action="index.php">
<input type="hidden" name="page" value="website-speed">
<input type="text" name="domain" value="<?php echo isset($domain) ? $domain : ''; ?>">
<input type="submit" value="Go">
</form>
</div>
<br/>
<h2>How does it find out the speed?</h2>
<p>The tool will 'ping' your website. It just sends it a little message 'are you there', and it will report back if it is there, and if the time in milliseconds that it took to respond and send back a message.</p>
<p>If you wish to do this yourself NOT via this tool, just open 'CMD' prompt, and enter 'ping {site domain name}'. </p>
<p style='text-align: center'><img src='/images/ping.jpg' alt='ping' /></p>
<p>This tool does all that for you via a short system. It is best to run it 3 or 4 times. After you have run it. Just hit F5 and it will run again, and just take an average. The tool will tell you if it is slow, ok, or really good.</p>
</div>
Code: Select all
<?php elseif ($ping < 60): ?>
<i class='fa fa-smile-o' aria-hidden='true' style='color: #009900'></i>
<font color='#009900'>This is very good. No issues - very fast!</font>
<?php endif; ?>Code: Select all
else if ($ping < 60)
{
echo "text here";
}
Because I generally keep presentation and business logic separate. I would have ping() in a separate file and have the results passed in. Essentially, I try to keep my presentation files (generally called views) to be mostly HTML with only PHP for simple if/else/loops. Further, I use the alternate syntax so that I don't have to mess with echo and single/double quote juggling. Open an if on one line, go back to normal HTML, then close the if. Keeps it much cleaner and easier to read IMO. That's less important than keeping the logic separated, though, as that promotes code reuse.simonmlewis wrote:Why do you open and close PHP so often in your code?
Code: Select all
foreach($times as $ping) {
echo $ping . "ms<br>";
}
$averageping = round(array_sum($times) / count($times));Code: Select all
<?php
// This section is where we're going to have the domain logic
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("Unable to open connection to '${host}'");
}
$end_time = microtime(true);
fclose($fp);
$response_time = round((($end_time - $start_time) * 1000), 0);
$ping_times[] = $response_time;
}
return $ping_times;
}
$pings = [];
$error_message = '';
// Grab the domain if one has been set
$domain = isset($_GET['domain']) ? $_GET['domain'] : null;
if (isset($domain)) {
// Try to get ping times. If something goes wrong, we catch the exception
// and record an error message for display
try {
$pings = ping($domain);
} catch (Exception $e) {
$error_message = "Sorry the domain you entered is either invalid, or the site you entered is down.";
}
}
?>
<?php /* We're going to keep the display logic below */ ?>
<div class="head"><h1>How fast is your website?</h1></div>
<div class="width-narrow">
<p>
A website's speed is down to server response time, then processing time, and of course images and data time.<br/>
If your website is packed full of images, and your server is slow, this will affect your ranking.<br/>
This simple tool will tell you if your site's server (not the processing of data or images) is good or bad.
</p>
<div class="site-speed">
<div class="site-speed-clock">
<?php if (!isset($domain)): ?>
<i class="fa fa-clock-o" aria-hidden="true"></i>
<?php endif; ?>
</div>
<?php if (!isset($domain)): ?>
Enter your full URL of your website here and click Go.
<?php endif; ?>
<?php if (count($pings)): ?>
The speed Google pings <?php echo $domain; ?> at is:<br>
<?php foreach ($pings as $ping): ?>
<b><?php echo $ping; ?>ms</b>
<br>
<?php endforeach; ?>
<?php $average_ping = round(array_sum($pings) / count($pings)); ?>
<?php if ($average_ping > 100): ?>
<i class='fa fa-frown-o' aria-hidden='true' style='color: #ff0000'></i>
<font color='#ff0000'>This is rather slow, and slow sites are difficut for consumers, and Google will penalize you for it.</font>
<?php elseif ($average_ping <= 100 && $average_ping >= 60): ?>
<i class='fa fa-meh-o' aria-hidden='true' style='color: #ff5500'></i>
<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>
<?php elseif ($average_ping < 60): ?>
<i class='fa fa-smile-o' aria-hidden='true' style='color: #009900'></i>
<font color='#009900'>This is very good. No issues - very fast!</font>
<?php endif; ?>
<?php elseif (!empty($error_message)): ?>
<span class="error"><?php echo $error_message; ?></span>
<?php endif; ?>
<form method="GET" action="index.php">
<input type="hidden" name="page" value="website-speed">
<input type="text" name="domain" value="<?php echo isset($domain) ? $domain : ''; ?>">
<input type="submit" value="Go">
</form>
</div>
<br/>
<h2>How does it find out the speed?</h2>
<p>The tool will 'ping' your website. It just sends it a little message 'are you there', and it will report back if it is there, and if the time in milliseconds that it took to respond and send back a message.</p>
<p>If you wish to do this yourself NOT via this tool, just open 'CMD' prompt, and enter 'ping {site domain name}'. </p>
<p style='text-align: center'><img src='/images/ping.jpg' alt='ping' /></p>
<p>This tool does all that for you via a short system. It is best to run it 3 or 4 times. After you have run it. Just hit F5 and it will run again, and just take an average. The tool will tell you if it is slow, ok, or really good.</p>
</div>Very nice. Keep in mind that mysql_ functions are gone now, so be mindful of which codebases you migrate.simonmlewis wrote:ps. we are going to be testing PHP 7!!