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.
If statement different on Windows servers and Unix?
Moderator: General Moderators
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
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
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
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
Maybe if you post a bit more of a script that is causing you problems we can advise better.
Mac
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.In Php the reason those curly braces exist (as in other programming languages) is to specify the scope of the if .
And bayridge, this
Code: Select all
if ($hr == 12) {
$amorpm = "PM";
} // this doesn'tCheers,
BDKR
-
mahara
- Forum Commoner
- Posts: 37
- Joined: Wed Nov 13, 2002 1:08 am
- Location: Bandung, Jawa Barat, Indonesia
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
instead of 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...
I always use this
Code: Select all
<?php
if ( empty($var) )
{
echo 'Error.';
}
?>Code: Select all
<?php
if ( empty($var) ) {
echo 'Error.';
}
?>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.
Anyway, that only my suggestion...
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.
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.
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.
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.
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.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.
Cheers,
BDKR