PDA

View Full Version : If...else problem


pardicity3
01-02-2003, 09:02 PM
Hey,

I am very new to php and actually this is my first ever "homemade" script (figures it wouldn't work!). Here is my situation. I have a program that checks whether or not AIM is open on my computer. The program then writes to a file On if AIM is on and Off if it isn't. It then procceeds to upload that file to a specified directory. What I am trying to do, is write a php script that will find out what the file contains and then do something depending on the contents.

So far, I can open the file and make the contents a string without a problem. My probelm comes when I use an if...elseif...else statement to output different pieces of text depending on the contents of my file.

Here is my code:


<?php
$prog_file_i = fopen("prog0.txt", "r");
$prog_file_s = fgets($prog_file_i, 10);
$prog_file = strtolower($prog_file_s);
if ($prog_file == 'on')
print('AIM status: ON | off');
elseif ($prog_file == 'off')
print('AIM status: on | OFF');
else
print('AIM status: unknown');
?>

The problem I am having is that the script is always printing the else statement (AIM status: unknown). I have put in a print($prog_file) statement just to make sure the file reads "on" and it does. I really don't know what is wrong. I assume it has something to do with my if($prog_file == 'on') statement, but I don't know why.

If you need to see the output of the script I have it uploaded to my site:
http://www.mikesadventures.net/trial/dynamic/trial.php

If you go to that link though, you will see this: "on AIM status: unknown". That little "on" in there is from my previously mentioned print($prog_file) statement that I used just to make sure that $prog_file contained "on".

Also, one last note, I have the program make $prog_file lowercase because I just want to be safe and not have the file reading "On" or something like that.

Thanks ahead of time!

mordred
01-02-2003, 09:43 PM
Maybe there's unnecessary whitespace characters (\r, \n, etc.) after "On" or "Off"? Depends on how you write to the file, and fgets does not necessarily stop excluding characters when encountering a newline character that's different from \n.

pardicity3
01-02-2003, 09:53 PM
Hey mordred,

You were right, there is a return (\r I think...) after On. So in other words the file reads:

On
(new line from return)

So, now that I know that, my only problem is that I don't know how to get rid of the return!! It's probably so simple, I even think I remember reading about how to do it, but I can't recall the process. Any help?

pardicity3
01-02-2003, 10:00 PM
Wait a tick, I think I would do this by exploding the file contents ($prog_file in this case) at each \r right? But now that I think about it, I don't know if this is a newline (\n) or a carriage return (\r); is there anyway to figure this out?

mordred
01-02-2003, 10:00 PM
Either you replace the unwanted whitespace characters with str_replace() or preg_replace(), but the simplest solution IMO would be to only compare the first two/three characters of the string, like:


if (substr($prog_file, 0, 2) == 'on') {
print('AIM status: ON | off');
}

pardicity3
01-02-2003, 10:05 PM
You can ignore the rest of this message if you want. I tested my new code changes and it worked, so you don't need to bother replying!

Brilliant! Much easier than my explode() idea (though I may have to use that soon enough if I try and track multiple programs). One last question though:

When I check for Off I will have to use this code because Off is longer than On right?


if (substr($prog_file, 0, 3) == 'off') {
print('AIM status: ON | off');
}

All I did was change the length to 3.

m__
01-03-2003, 02:04 AM
I know you've already solved your problem, but for my own edification -- couldn't you just write a 1 when AIM is on and 0 when AIM is off and only read the first character of the file to avoid using any extraneous code?

m__

pardicity3
01-03-2003, 05:30 AM
Hey m__,

You are very right in that it would make it a lot easier to have an output of 0 or 1. The only problem is--as much as I hate to admit it--I do not have the skills to write a program as cool as the one that I use to make and upload these files. Someone else created the program, and thus I have no access to the source code or anything, so it's just not a possibility in this case. Great suggestion though!

Oh, and I am sure no one really cares, but the program is called Pakkasherra and is made by a guy by the name of Jouni Vuorio. It is very interesting and can do many a cool things (though admittedly, it is more pizzaz than functionality the way I am using it). Here is a link: http://www.vtoy.fi/jv16/shtml/pakkasherra.shtml .