PHP Outside the Browser: How to Write Cross-Platform CLI Applications

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

PHP is Not Just About the Web

\n\n

When people say "PHP," most immediately imagine the VKontakte news feed, a shopping cart in an online store, or the WordPress backend. This is logical: the language was born as a tool for the web. But what if I told you that PHP handles tasks usually assigned to Python, Bash, or Go excellently? We're talking about cross-platform development in PHP — creating utilities that work on Windows, Linux, and macOS without rewriting code.

\n\n

In this article, we won't teach you how to design forms. We'll show you how to write a console file manager, a log parser, and a folder monitoring system — all in pure PHP, without a single web server. You'll learn how to navigate the pitfalls of different operating systems and write code that runs identically everywhere.

\n\n

Ready? Then open your terminal and get ready to be amazed by the capabilities of good old PHP.

\n\n

Why PHP for Cross-Platform Tasks? Three Solid Arguments

\n\n

Before diving into the code, let's figure out why you would even use PHP for desktop and server utilities. Python has Django and Flask for this, Node.js has Express. Why PHP?

\n\n
    \n
  • Ubiquitous Availability. PHP is installed on almost every shared hosting and on 80% of servers worldwide. If you're writing an administration tool, there's a high chance PHP is already on the target machine. No need to install an additional runtime.
  • \n
  • Low Entry Barrier for Web Developers. You already know the syntax, string and array functions. You don't need to learn a new language to automate routine tasks on the server.
  • \n
  • Rich Standard Library. PHP has everything for working with files, processes, network sockets, and zip archives. And PECL extensions like parallel or pthreads add multithreading capabilities.
  • \n
\n\n

Of course, PHP isn't ideal for GUI applications (although PHP-GTK exists). But for console utilities and daemons, it's a mature and stable tool. Let's see how to write such code correctly.

\n\n

Basics of Cross-Platform PHP Code

\n\n

The first thing to remember: path separators. On Linux it's /, on Windows it's \\. PHP provides the DIRECTORY_SEPARATOR constant, but it's even more convenient to use the universal forward slash / — Windows handles it perfectly from PHP 7+.

\n\n

Working with Paths Correctly

\n\n
<?php\n// Bad — hardcoded slash\n$path = '/var/log/errors.log'; // Will fail on Windows\n\n// Good — using DIRECTORY_SEPARATOR\n$path = 'var' . DIRECTORY_SEPARATOR . 'log' . DIRECTORY_SEPARATOR . 'errors.log';\n\n// Excellent — using / and realpath()\n$path = __DIR__ . '/logs/errors.log';\necho $path; // Works on both OSes\n?>\n
\n\n

The second important point is encoding. On Windows, the console often works in CP866 by default, while PHP scripts use UTF-8. To output Cyrillic on Windows, use the mb_convert_encoding() function or set the encoding via chcp 65001 at the beginning of the script.

\n\n

Example: Cross-Platform File Reading

\n\n
<?php\nfunction readLogFile(string $filename): string {\n    // Check if the file exists\n    if (!file_exists($filename)) {\n        throw new RuntimeException("File $filename not found");\n    }\n    \n    // Read the contents\n    $content = file_get_contents($filename);\n    \n    // Determine the OS and convert encoding for output\n    if (PHP_OS_FAMILY === 'Windows') {\n        $content = mb_convert_encoding($content, 'CP866', 'UTF-8');\n    }\n    \n    return $content;\n}\n\necho readLogFile('logs/app.log');\n?>\n
\n\n

Creating a Real Utility: Folder Change Monitoring

\n\n

Let's write something useful — a script that monitors a folder and out

Blogs

Book Recommendations