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
Gemster
Forum Newbie
Posts: 13 Joined: Thu Jul 29, 2010 10:38 am
Post
by Gemster » Thu Jul 29, 2010 10:43 am
Hi, im new to php if statments. i have a little ping code that i need help with.
Code: Select all
<?php
if(system('ping -c 1 64.85.163.6')) {
echo "online";
} else {
echo "offline";
}
?>
Baiscally i want it to ping that ip and if it pings then echo Online and if not then echo Offline, that all i want it to do
Thanks for any help
Gemster
jraede
Forum Contributor
Posts: 254 Joined: Tue Feb 16, 2010 5:39 pm
Post
by jraede » Thu Jul 29, 2010 12:22 pm
What's wrong with it? Are you getting any error messages?
Gemster
Forum Newbie
Posts: 13 Joined: Thu Jul 29, 2010 10:38 am
Post
by Gemster » Thu Jul 29, 2010 1:17 pm
jraede wrote: What's wrong with it? Are you getting any error messages?
It outputs this
PING 64.85.163.6 (64.85.163.6) 56(84) bytes of data. 64 bytes from 64.85.163.6: icmp_seq=1 ttl=54 time=98.4 ms --- 64.85.163.6 ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 98.439/98.439/98.439/0.000 ms online
I want it to output: 64.85.163.6 Online and if it dont ping, 64.85.163.6 Offline
Thanks
Gemster
Jade
Forum Regular
Posts: 908 Joined: Sun Dec 29, 2002 5:40 pm
Location: VA
Post
by Jade » Thu Jul 29, 2010 1:21 pm
Try this:
Code: Select all
<?php
$ip = '64.85.163.6';
if(system('ping -c 1 $ip', $result)) {
echo "$ip online";
} else {
echo "$ip offline";
}
?>
jraede
Forum Contributor
Posts: 254 Joined: Tue Feb 16, 2010 5:39 pm
Post
by jraede » Thu Jul 29, 2010 1:24 pm
The system function displays the output of whatever command you pass to it. Use exec() instead.
Gemster
Forum Newbie
Posts: 13 Joined: Thu Jul 29, 2010 10:38 am
Post
by Gemster » Thu Jul 29, 2010 2:27 pm
ok thanks guys, just 1 question,
this now works:
Code: Select all
<?php
if(exec('ping -c 1 64.85.163.6')) {
echo "IP 64.85.163.6 Is <b><u>Online</b></u>";
} else {
echo "IP 64.85.163.6 Is <b><u>Offline</b></u>";
}
?>
But this does not work:
Code: Select all
<?php
$ip = '64.85.163.6';
if(exec('ping -c 1 $ip')) {
echo "IP $ip Is <b><u>Online</b></u>";
} else {
echo "IP $ip Is <b><u>Offline</b></u>";
}
?>
The top code works fine an says IP 64.85.163.6 Is Online but the second code says IP 64.85.163.6 Is Offline
Any help with that would be great as i like the idea of useing $ip.
Thanks
Gemster
jraede
Forum Contributor
Posts: 254 Joined: Tue Feb 16, 2010 5:39 pm
Post
by jraede » Thu Jul 29, 2010 3:00 pm
PHP only fills in variables in strings if you use double quotes ("). If you use single quotes, it will take everything literally, so it will try to ping the IP $i.
Gemster
Forum Newbie
Posts: 13 Joined: Thu Jul 29, 2010 10:38 am
Post
by Gemster » Thu Jul 29, 2010 3:56 pm
Sorry, I dont understand :/
Maybe if u fix the code above and post that then I will understand what you meen.
Thanks
Gemster
Jade
Forum Regular
Posts: 908 Joined: Sun Dec 29, 2002 5:40 pm
Location: VA
Post
by Jade » Mon Aug 02, 2010 10:30 am
Code: Select all
<?php
$ip = '64.85.163.6';
if(exec('ping -c 1' . $ip)) {
echo "IP $ip Is <b><u>Online</b></u>";
} else {
echo "IP $ip Is <b><u>Offline</b></u>";
}
?>
jraede
Forum Contributor
Posts: 254 Joined: Tue Feb 16, 2010 5:39 pm
Post
by jraede » Mon Aug 02, 2010 4:03 pm
Sorry for the delay. To clarify:
Code: Select all
$variable = 'test';
echo "The variable is $variable"; // This will display: The variable is test
echo 'The variable is $variable'; // This will display: The variable is $variable
PHP doesn't parse strings in single quotes for variables, so it will display purely the content of the string. Double quotes are parsed for variables and their corresponding values are inserted. Naturally, this takes a bit longer, so if you have a large application, ideally you should use single-quotes unless there's a variable involved to shave some microseconds off processing time. It's barely noticeable though.