Elementary Overview
PHP code needs a PHP runtime before it can execute. At the beginner level, the runtime is the PHP program installed on your computer or server. It reads PHP source code, prepares it for execution through the PHP engine, and runs it. You can use PHP from the command line for scripts, or connect it to a web-server environment so PHP code can generate responses for a browser.
This first PHP lesson stays deliberately focused on the runtime itself. You will verify PHP with php -v, run a file with php hello.php, execute a tiny command directly with php -r, inspect configuration with php --ini, and start PHP’s built-in development server with php -S localhost:8000. Later lessons will separately cover PHP syntax, variables, arrays, control flow, functions, forms, GET and POST, sessions, cookies, files, classes, namespaces, exceptions, Composer, PDO, databases, authentication, APIs, JSON, security, Laravel, and testing.
What You Should Learn
- What people mean by the PHP runtime.
- How to confirm PHP is installed with
php -v. - What the PHP CLI is and why it is useful.
- How to run a PHP file from a terminal.
- What a PHP SAPI is at a beginner level.
- How CLI execution differs from serving PHP through HTTP.
- How to start PHP’s built-in development server.
- Why that built-in server is for development rather than production.
- How to inspect the active PHP configuration.
- How to troubleshoot the first common PHP runtime failures.
What The PHP Runtime Does
PHP is a server-side programming language with a runtime that can be used in more than one environment. When you run PHP at a terminal, the CLI SAPI handles command-line execution. When PHP participates in a web stack, another server-facing interface may be involved. PHP uses the term SAPI for Server Application Programming Interface: the layer that connects the PHP engine to the environment that invoked it.
The important beginner idea is that the same language can run in different contexts. A PHP file can behave like a command-line program, or PHP can generate output for an HTTP request. Understanding the runtime first makes later web concepts easier because you know that PHP itself is doing the code execution while the surrounding environment determines how input arrives and where output goes.

Verify The Runtime With php -v
After installing PHP, open a new terminal and begin with:
php -v
The -v option prints version information. PHP’s command-line documentation notes that this output also helps you see whether the executable is running as the CLI build. If your shell reports that php is not found, the problem is not yet your source code. First verify that PHP is installed and that the executable can be found through your environment.
That command-discovery step is the same general idea covered in What Is the PATH Environment Variable? and OSBash.001: Shell and Script Basics. Before debugging a language, prove that the operating system can launch its runtime.
Create A First PHP File
Create a plain-text file named hello.php:
<?php
echo "Hello from PHP!\n";
The opening <?php tag tells PHP where PHP code begins. The echo construct writes output. You do not need to understand PHP’s complete syntax yet; the purpose of this file is simply to verify that source code can travel through the runtime and produce visible output.
If you are using a code editor, syntax highlighting can make the file easier to read, but the runtime executes source code rather than editor colors or formatting.
Run PHP From The Command Line
From the directory containing the file, run:
php hello.php
PHP’s official command-line documentation also allows the explicit -f form:
php -f hello.php
Both forms tell the CLI runtime to execute the file. This is important because PHP is not limited to web pages. It can also power command-line utilities, maintenance tasks, queue workers, deployment scripts, data-processing jobs, and other programs that never render a browser page.
Run A Tiny Command With php -r
PHP can execute a short fragment without creating a file:
php -r 'echo "PHP is running\n";'
The -r option executes code supplied directly on the command line. PHP’s documentation notes that you do not include opening or closing PHP tags with -r. Shell quoting rules differ between environments, so treat this as a quick runtime check rather than your primary way to write real programs.
Inspect PHP Configuration
PHP behavior can be influenced by configuration files. A useful first diagnostic is:
php --ini
This reports configuration-file locations associated with the runtime. If two computers behave differently even though the source code matches, the difference may come from PHP versions, installed extensions, configuration files, environment variables, or the SAPI being used.
You can also inspect broad runtime information with:
php -i
The output is large, so use it as a diagnostic reference rather than something to memorize. Early on, the useful habit is simply knowing that runtime configuration exists and knowing where to look when behavior differs.
Start PHP’s Built-In Development Server
PHP’s CLI runtime includes a small built-in web server intended for local development, testing, and demonstrations. From a project directory, you can start it with:
php -S localhost:8000
Then create or keep hello.php in that directory and visit:
http://localhost:8000/hello.php
The browser sends an HTTP request to the development server. PHP executes the requested script and the response is sent back to the browser. This is a useful bridge between command-line PHP and later web development. It also connects to the broader idea of software interfaces described in What Is an API?.
Do not treat the built-in server as a production web server. PHP’s own manual explicitly describes it as a development aid and warns against exposing it as a full production server on a public network. Production PHP deployments normally use a hardened web stack, process management, logging, TLS, access controls, resource limits, monitoring, and other operational protections that are outside this first lesson.
CLI PHP And Web PHP Are The Same Language In Different Contexts
A common beginner misconception is that “PHP in the terminal” and “PHP on a website” are different languages. They are not. The language is PHP in both cases. What changes is the execution environment: command-line input and output versus an HTTP-oriented server context.
This distinction becomes important later. Web PHP receives request information, may work with browser cookies, often talks to a relational database, and may return HTML or JSON. CLI PHP can perform maintenance, data processing, scheduled tasks, and automation without an HTTP request at all.
The First Troubleshooting Order
- Can the shell find PHP? Run
php -v. - Are you in the correct directory? Confirm the PHP file is actually present.
- Does the filename match? Watch capitalization on case-sensitive systems.
- Does the file parse? Read the first useful PHP error instead of only the final line.
- Which configuration is active? Run
php --ini. - Which PHP build are you using? Compare
php -vacross terminals, containers, servers, or development machines. - Does CLI work but the browser fail? Separate runtime problems from web-server routing, ports, document roots, firewalls, and HTTP configuration.
- Does the built-in server bind successfully? If port 8000 is already in use, stop the competing process or choose another local port.
Mini Lab
- Run
php -vand record the PHP version. - Run
php --iniand identify the configuration-file locations. - Create
hello.phpwith oneechostatement. - Run it with
php hello.php. - Run a tiny one-line test with
php -r. - Start the development server with
php -S localhost:8000. - Open
hello.phpthrough the local server in a browser. - Stop the server with Ctrl+C.
- Introduce one deliberate syntax error, run the file, read the diagnostic, and fix it.
- Commit the working example if you are practicing Git and GitHub.
Common Beginner Mistakes
- Assuming PHP only runs inside a browser: PHP also has a command-line runtime.
- Debugging source before verifying the runtime: first prove
php -vworks. - Confusing the PHP runtime with the web server: they are related pieces of a web stack, not the same thing.
- Using the built-in PHP server as production infrastructure: it is intended for development and testing.
- Ignoring configuration differences: two PHP installations can load different configuration files or extensions.
- Mixing CLI and web assumptions: request data available in a browser-driven execution may not exist in the same form in CLI execution.
- Changing multiple settings after one error: isolate the runtime, file, configuration, server, and network layers one at a time.
Exercises
- Explain the PHP runtime in one sentence.
- Explain the difference between PHP CLI execution and HTTP-based execution.
- Create a PHP file that prints two separate lines.
- Use
php -rto print a short message without creating a file. - Use
php --iniand identify the loaded configuration path. - Start the built-in development server on port 8080 instead of 8000.
- Explain why the built-in server should not be used as a public production server.
- Describe one situation where CLI PHP would be useful without a browser.
Knowledge Check + Answers
- What does
php -vdo? It prints PHP version information and helps confirm that the command-line runtime is available. - What is PHP CLI? The command-line interface SAPI used to run PHP from a terminal or shell.
- How do you run a file named
hello.php?php hello.php. - What does
php -rdo? It executes a short PHP code fragment supplied directly on the command line. - What does
php --inihelp you inspect? PHP configuration-file locations. - What does
php -S localhost:8000do? It starts PHP’s built-in development web server on local port 8000. - Should the built-in server be used as a public production server? No. PHP documents it as a development and testing tool rather than a full production server.
- Is command-line PHP a different language from web PHP? No. It is the same PHP language running in a different execution context.
Primary References
- PHP Manual — Using PHP from the Command Line
- PHP Manual — Command-Line Options
- PHP Manual — Executing PHP Files
- PHP Manual — Built-In Web Server
- PHP Manual — Your First PHP-Enabled Page
Elementary Review
Before learning PHP syntax, prove that the PHP runtime works. Use php -v to verify the executable, php hello.php to run a source file, php --ini to locate configuration, and php -S localhost:8000 to test a PHP page through a local development server. Once that loop is reliable, the language itself becomes much easier to learn and troubleshoot.
The next canonical PHP lesson is OSPHP.002: Syntax, where the track will focus on PHP tags, statements, semicolons, comments, expressions, output, and the basic shape of a PHP program.
Editor’s Note
The featured image is an original 1200×630 BitcoinVersus.Tech color-pencil illustration created specifically for OSPHP.001 and is not reused in the body. The lesson uses a separate original 1200×675 photograph. The three YouTube videos use responsive native Gutenberg 16:9 embed blocks, and the social item uses a responsive native Gutenberg embed. Ordinary lesson prose is not placed inside bordered, shaded, card, callout, panel, or fixed-width text boxes.
BitcoinVersus.Tech content is provided for informational and educational purposes.

Leave a Reply