Current Location: Home> Latest Articles> How to Customize and Implement Command-Line Script Operations in PHP's Main Function

How to Customize and Implement Command-Line Script Operations in PHP's Main Function

M66 2025-06-15

In many programming languages, the main function serves as the entry point for a program. Although PHP does not require a main function by default, we can simulate this structure, especially when writing command-line scripts, as it helps in organizing the code and making it more maintainable.

This article will demonstrate how to customize a main function in PHP and implement basic command-line script operations. In the example, we will handle command-line arguments, perform simple logic, and show how to use PHP scripts to implement functions similar to command-line tools.


1. Defining the Main Function in a PHP Script

PHP, being a scripting language, executes the file directly, without the need for an entry function like in C or Java. However, we can define our own main function and write the program logic inside it. Then, we simply call main() to achieve a unified entry point.

<?php
<p>function main(array $argv) {<br>
// Program entry point, $argv is the array of command-line arguments<br>
}</p>
<p>main($argv);<br>

  • $argv is a global variable automatically defined by PHP, which contains all the arguments passed from the command line.

  • By passing $argv, we can directly use the command-line arguments within the main function, making it easier to test and expand.


2. Parsing Command-Line Arguments

PHP command-line scripts often need to handle arguments. We can implement argument parsing using a simple loop and switch, or alternatively, by using the getopt() function.

The following example demonstrates a simple script that supports -h for displaying help, and -n for passing a name and printing a greeting message.

<?php
<p>function printHelp() {<br>
echo "Usage: php script.php [-h] [-n name]\n";<br>
echo "Options:\n";<br>
echo "  -h        Show help message\n";<br>
echo "  -n name   Specify the name to greet\n";<br>
}</p>
<p>function main(array $argv) {<br>
$name = null;<br>
$argc = count($argv);</p>
    switch ($argv[$i]) {
        case '-h':
            printHelp();
            return 0;
        case '-n':
            if (isset($argv[$i + 1])) {
                $name = $argv[++$i];
            } else {
                echo "Error: -n option requires a name.\n";
                return 1;
            }
            break;
        default:
            echo "Unknown option: {$argv[$i]}\n";
            return 1;
    }
}

if ($name) {
    echo "Hello, {$name}!\n";
} else {
    echo "Hello, World!\n";
}

return 0;

}

exit(main($argv));

Execution example:

php script.php -h
php script.php -n Alice

3. Example with Network Requests

Suppose the script needs to fetch data from the web. The following example demonstrates how to use PHP's file_get_contents() function to request a URL and process the returned content.

Note that you should replace the domain with m66.net as per your needs.

<?php
<p>function fetchData(string $url) {<br>
$content = @file_get_contents($url);<br>
if ($content === false) {<br>
echo "Failed to fetch data from {$url}\n";<br>
return null;<br>
}<br>
return $content;<br>
}</p>
<p>function main(array $argv) {<br>
$url = "<a rel="noopener" target="_new" class="" href="http://m66.net/api/data">http://m66.net/api/data</a>";</p>
if ($data === null) {
    return 1;
}

echo "Received data:\n";
echo $data . "\n";

return 0;

}

exit(main($argv));


4. Conclusion

  • PHP does not have a built-in main function, but it can be customized to provide a unified program entry point.

  • By using the $argv variable, you can handle command-line arguments and create flexible scripts.

  • By combining PHP's standard functions, you can implement various functionalities like network requests and file operations, building powerful command-line tools.

By following the methods outlined above, you can easily create customized PHP command-line scripts while maintaining clear and organized code that is easy to maintain and expand.