If statement different on Windows servers and Unix?

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

Post Reply
bayridge
Forum Newbie
Posts: 5
Joined: Wed Nov 13, 2002 7:38 pm

If statement different on Windows servers and Unix?

Post by bayridge »

I am considering moving from a Unix hosting company to a Windows hosting company. I have a lot of php scripts. I moved a couple of scripts to check them out and found that they would work if I had curly brackets in my IF statements and when I removed them the script worked. Am I right in my thinking that I will have to rewrite all my php scripts and make changes? Are there any other functions that work differently on Windows servers? I just assumed I could make the switch without changing my scripts.

Example code
if ($hr == 12)
$amorpm = "PM"; //this works on windows

this works

if ($hr == 12) {
$amorpm = "PM";
} // this doesn't

Thanks for any help. I'm a novice at php.
CoderMan
Forum Newbie
Posts: 1
Joined: Thu Nov 14, 2002 1:54 am

Post by CoderMan »

I am running Apache and PHP locally on a windows Machine and the curly braces work fine with if statemnts. In Php the reason those curly braces exist (as in other programming languages) is to specify the scope of the if .
Basically anything in the curly braces is considered part of the if clause other wise it will only consider the first line after the if.

Example assume you want hello world printed only if the value of $test was 1:

$test=1;
if($test == 1)
echo "hello";
echo "world";
//This prints hello and will ALWAYS print world even if $test is not equal to 1

if($test ==1)
{
echo "hello";
echo "world";
}

//This is only print "world" when $test is 1
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

One of PHP's big selling points is it's lack of dependence on a particular platform so no, you shouldn't have to change all your code because you're moving from one platform to another, most likely if you have to change your code it's because PHP will be configured differently on the new server.

Maybe if you post a bit more of a script that is causing you problems we can advise better.

Mac
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

In Php the reason those curly braces exist (as in other programming languages) is to specify the scope of the if .
This of course isn't the case wth languages like VB, which our friend may be coming from, or Python, Ruby. Personally, it drives me nuts.

And bayridge, this

Code: Select all

if ($hr == 12) {
$amorpm = "PM";
} // this doesn't
really should work. What is the exact error message you're seeing? How about some of the code just before and just after?

Cheers,
BDKR
mahara
Forum Commoner
Posts: 37
Joined: Wed Nov 13, 2002 1:08 am
Location: Bandung, Jawa Barat, Indonesia

Post by mahara »

Well, I think it's good to have a good habit like using curl braces on every block you type, even it has only one statement in it.

I always use this

Code: Select all

<?php
if ( empty($var) )
{
    echo 'Error.';
}
?>
instead of

Code: Select all

<?php
if ( empty($var) ) {
    echo 'Error.';
}
?>
No matter which way you choose, I think both will work.

Why should? First, see how easy if you want to add more lines in the block, escpecially when the block has block/s inside it. Absolutely easier to add the curl braces than re-type the whole script. :) Second, as told before, this way works on most systems (if it's not all).

Anyway, that only my suggestion...
bayridge
Forum Newbie
Posts: 5
Joined: Wed Nov 13, 2002 7:38 pm

Post by bayridge »

I get your point about the curly brackets. I only had one condition after the IF, so I didn't realize the problem with multiple statements after the IF as in the Hello World example.

I will try the other suggestion and see if that works. Shouldn't matter if the bracket is on the same line as the IF statement or on a separate line, but will try anyway.

Another problem I have is that I have to use .php extensions instead of .php3. All my current files have .php3, but they don't work on the Windows server unless I rename them to .php. This is hosted, it is not my own server.

Thanks.
hedge
Forum Contributor
Posts: 234
Joined: Fri Aug 30, 2002 10:19 am
Location: Calgary, AB, Canada

Post by hedge »

I move back and forth and have had no issues other than $_SERVER vars being different.

I believe you may be running into issues between configurations... maybe register_globals. If register globals is off then your $hr variable will not be defined and may be actling like the if statement isn't working.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

bayridge wrote: Another problem I have is that I have to use .php extensions instead of .php3. All my current files have .php3, but they don't work on the Windows server unless I rename them to .php. This is hosted, it is not my own server.
Is there any problem with you renaming them all to php? If it doesn't work on your own server, change it's configuration to reflect what the production server has. You can tell the server to parse whatever extension you wish as a php or php3 file. You could tell the server to parse HTML files with the php engine if you wish, which may not be a bad idea.

Cheers,
BDKR
Post Reply