Why "It Works on My Machine" Is a Death Sentence?
\n\nHello, colleague! If you've ever heard the phrase "It works for me, but not on the server" and nervously laughed, — this article is for you. Cross-platform development in PHP is not just a trendy term, but a survival skill in modern web development. We live in a world where you might have macOS or Windows locally, while production runs on Linux. And if your code doesn't know how to get along with different operating systems, you're doomed to eternal bugs and sleepless nights.
\n\nIn this article, we'll break down the main pitfalls that even experienced developers stumble over, and learn how to write truly universal code. Let's go!
\n\nThe Main Enemies of Cross-Platform Compatibility in PHP
\n\nBefore diving into writing code, let's figure out what exactly breaks when moving from one OS to another. There aren't many, but each point can bring down the entire application.
\n\n1. Directory Separators: / vs \\
\n\nA classic. Windows uses a backslash (\\), while Linux and macOS use a forward slash (/). If you hardcode paths, you lose.
<?php\n// BAD: will break on Linux\n$path = 'C:\\projects\\myapp\\logs\\app.log';\n\n// GOOD: use DIRECTORY_SEPARATOR\n$path = 'projects' . DIRECTORY_SEPARATOR . 'myapp' . DIRECTORY_SEPARATOR . 'logs' . DIRECTORY_SEPARATOR . 'app.log';\n\n// EVEN BETTER: use / — works everywhere\n$path = 'projects/myapp/logs/app.log';\n?>\n\nTip: even inside PHP, many functions (e.g., fopen()) correctly handle forward slashes on Windows. So try to use / — it's the simplest way to do cross-platform development in PHP without a headache.
2. End-of-Line Characters (EOL)
\n\nWindows uses \\r\\n, Linux uses \\n, and the good old macOS (before version X) uses \\r. If you write scripts for processing text files or logs, this will become a problem.
<?php\n// Universal way to split text into lines\n$text = file_get_contents('data.txt');\n$lines = preg_split('/\\r\\n|\\r|\\n/', $text);\n\n// Or use the built-in constant PHP_EOL for output\necho "First line" . PHP_EOL . "Second line";\n?>\n\nThe constant PHP_EOL automatically substitutes the correct character for the OS on which the script is running. This is the ideal option when you are writing logs or generating text files.
3. Case Sensitivity of Files and Paths
\n\nOn Windows, the file system is case-insensitive (File.txt and file.txt are the same). On Linux, it is case-sensitive. If you include files with the wrong case, you'll get a 500 error in production.
\n\n<?php\n// BAD: may not work on Linux\nrequire_once 'Includes/Config.PHP';\n\n// GOOD: always follow case\nrequire_once 'includes/config.php';\n?>\n\nThe best practice is to agree with your team on a consistent file naming style (e.g., snake_case.php or camelCase.php) and always use exact case matching. Composer autoloading with PSR-4 standards also solves this problem, but only if you follow namespaces.
Working with Paths: Use Modern Tools
\n\nInstead of reinventing the wheel, use ready-made solutions. Here are a few recipes for writing cross-platform PHP code.
\n\nThe Magic Function realpath()
\n\nrealpath() converts any relative path to an absolute path, using the correct separators for the current OS.
<?php\n$relativePath = '../logs/error.log';\n$absolutePath = realpath($relativePath);\necho $absolutePath; // For example: /var/www/project/logs/error.log\n?>\n\n