Develop PHP Applications on Windows for Linux Servers – Effective Strategies

linuxPHPwindows

Is it fine to code PHP on Windows and host it later on a server running Linux? Can there be any problems in the migration of such a project?

I would think that there really can't be any problems, especially since I am a beginner in PHP and I won't use any of the advanced functions that may be OS-specific. However, I would like to make sure since I really don't like Linux at all.

Best Answer

Some pointers:

Filesystem case sensitivity

If your file is called HelloWorld.php this:

include "helloworld.php";

is legit on Windows and will work. But Linux filenames are case sensitive, you can have files called HelloWorld.php, helloworld.php, hEllOwOrlD.php in the same directory. So you should develop on Windows as if you were developing on a case sensitive filesystem: use exactly the correct filenames, directory names, extension names - .php is also different from .PHP.

Directory and path separators

In Windows we say:

include 'classes\myClass.php';

But in Linux we would say:

include 'classes/myClass.php';

PHP is smart enough to not care, both separators work in both systems. But you should be consistent and go with the slash (/) everywhere as it's also the norm on most systems. There is a nifty predefined constant DIRECTORY_SEPARATOR that translates to the correct one, if you want to go that far:

include "classes" . DIRECTORY_SEPARATOR . "myClass.php";

The same goes for the path separator, which is semicolon on Windows, colon otherwise. So to be safe you should do:

set_include_path(get_include_path() . PATH_SEPARATOR . $path);

when in need of a path separator. Although most people think that since PHP doesn't mind which separator you use it's ok, but there is one important catch: The separators will be the system specific ones when you ask the system for directories or paths. So let's say you want to explode the include path into its parts:

$includePath = get_include_path();

$pathParts = explode(";", $includePath) // Will only work on Windows
$pathParts = explode(":", $includePath) // Will work on other systems but not Windows
$pathParts = explode(PATH_SEPARATOR, $includePath) // Will work everywhere!!!

File encoding and delimiter

You should set your IDE to set file encoding for all your scripts to UTF-8 instead of Cp*, and the file line delimiter to Unix ("\n" instead of "\r\n"). In most cases it won't really matter but you should be consistent and the best way is the Unix way (which works fine on Windows but not vice versa).

Related Topic