Skip to content

How Compiled PHP Console Applications (CLI) Work

ExeOutput for PHP can create console applications without a GUI. These applications operate as if PHP were executed in CLI mode.

Console APP

You can decide whether to create a GUI or a console application when starting a new project. This choice is final! The status bar of ExeOutput for PHP indicates whether you are working on a GUI or a console application:

Console APP in status bar

ExeOutput for PHP creates a single, stand-alone executable file: end users simply launch the resulting EXE file. No PHP installation is necessary. All compiled files are automatically loaded into the PHP runtime upon request.

By default, the index page script runs at startup. You can specify a different command line in the PHP Settings => Main Settings page.

Warning

Console applications cannot prompt for elevated rights: use an administrator command prompt to perform administrative tasks.

The function exo_get_resstring can be used to protect resource strings.

Warning

Many ExeOutput for PHP features are NOT supported by console applications: global variables, global protection (security), HEScript scripting...

Access the Folder Containing the .EXE File

Do not use $_SERVER['DOCUMENT_ROOT'] in a console application, as the $_SERVER variable is empty in this context (a console app does not run on a server or handle HTTP requests). Instead, you can retrieve the full path to the directory containing the EXE file by using the environment variable EXOPHPEXEPATH as shown below:

$exoexepath = getenv('EXOPHPEXEPATH');
print($exoexepath);

This prints the full path of the EXE's directory. For example:

print(getenv('EXOPHPEXEPATH'));

Additionally, you can access the virtual "Data" subfolder using the environment variable EXOPHPDATAPATH. For instance:

print(getenv('EXOPHPDATAPATH'));

Script Example

The following code was used to generate the console app screenshot shown above.

<?php

print("Hello World from a console app made with ExeOutput for PHP!\n");

print(exo_get_protstring('str1'));

fputs(STDOUT, "\nThe Amazing Favourite Colour Script\n");
fputs(STDOUT, "What is your favourite colour? ");

$sometext = strtolower(trim(fgets(STDIN, 256)));
fputs(STDOUT, "Your favourite colour is $sometext!\n\n");
?>