Your First PHP Program
Every PHP tutorial has to start somewhere, but printing the words "Hello, World" and moving on skips the part that actually teaches you anything: how a script takes some numbers, does a calculation, and formats the result for a human to read. This page builds one small, genuinely useful script — a compound interest calculator — and runs it two different ways: from a terminal, and from a web browser. By the end you will have written real PHP that mixes variables, arithmetic, string interpolation, and number formatting, and you will understand what changes (and what does not) between running a script directly versus serving it over HTTP.
Setting up the script
Create a new file called interest.php. Every PHP file starts the same way — an opening <?php tag switches the parser into "code mode." The script below declares three variables (principal, annual rate, and number of years), computes compound interest using the standard formula, and prints a short, nicely formatted summary.
interest.php
<?php
$principal = 5000; // starting amount, in dollars
$annualRate = 0.045; // 4.5% interest per year
$years = 10;
// Compound interest formula: A = P * (1 + r)^t
$finalAmount = $principal * pow(1 + $annualRate, $years);
$interestEarned = $finalAmount - $principal;
echo "Compound Interest Calculator\n";
echo "----------------------------\n";
echo "Principal: \$" . number_format($principal, 2) . "\n";
echo "Annual rate: " . ($annualRate * 100) . "%\n";
echo "Years invested: {$years}\n";
printf("Final amount: \$%s\n", number_format($finalAmount, 2));
printf("Interest earned: \$%s\n", number_format($interestEarned, 2));
A few things worth noticing right away. pow(1 + $annualRate, $years) raises (1 + rate) to the power of years — PHP has no
special requirement here; the ** operator works identically if you
prefer infix notation, but pow() reads clearly for a formula lifted
straight from a math textbook. The double-quoted strings use two
different techniques to insert a variable: {$years} is curly-brace
interpolation, which works inside any double-quoted string, while
number_format() is a function call, so its result has to be
concatenated with . rather than interpolated directly — PHP does
not evaluate function calls inside a double-quoted string.
Running it from the command line
With PHP installed, open a terminal in the same folder as the file and run it directly with the php command. There is no server, no browser, and no HTML involved — PHP reads the file, executes it top to bottom, and prints whatever your echo/printf calls produce straight to the terminal.
Terminal
php interest.php
Compound Interest Calculator ---------------------------- Principal: $5,000.00 Annual rate: 4.5% Years invested: 10 Final amount: $7,764.12 Interest earned: $2,764.12
Notice the output has no HTML tags anywhere — just plain text, in the exact order the echo and printf statements ran. This is the CLI SAPI (the command-line "server API" build of PHP): its only job is to run your script and send output to standard output, the same place a Python or Node script would print to.
Running it from a browser
The same script also works over HTTP with zero code changes — that is one of PHP's defining traits. Copy interest.php into your web server's document root (for a local setup this is often something like C:/xampp/htdocs/interest.php or /var/www/html/interest.php), make sure Apache/Nginx and PHP are running, and visit it in a browser.
Browser address bar
http://localhost/interest.php
The browser will render the same six lines of text — but this time they arrive as an HTTP response with a Content-Type: text/html header attached automatically by the web server SAPI, and every \\n newline in the script is invisible in the rendered page (HTML collapses plain whitespace), so all six lines can visually run together unless you switch to <br> tags or wrap the output in <pre>. The PHP logic did not change at all; only the transport and the surrounding presentation layer did.
interest.php — a browser-friendly version
<?php
$principal = 5000;
$annualRate = 0.045;
$years = 10;
$finalAmount = $principal * pow(1 + $annualRate, $years);
$interestEarned = $finalAmount - $principal;
?>
<!DOCTYPE html>
<html>
<body>
<h1>Compound Interest Calculator</h1>
<ul>
<li>Principal: $<?= number_format($principal, 2) ?></li>
<li>Annual rate: <?= $annualRate * 100 ?>%</li>
<li>Years invested: <?= $years ?></li>
<li>Final amount: $<?= number_format($finalAmount, 2) ?></li>
<li>Interest earned: $<?= number_format($interestEarned, 2) ?></li>
</ul>
</body>
</html>This version drops the echo/printf calls in favor of short echo tags (<?= ... ?>) sprinkled directly into HTML markup — the natural style once a script's output needs to be an actual web page rather than plain lines of text.
What actually differs between the two contexts
Output destination:CLI writes to the terminal (standard output); the web SAPI writes to an HTTP response body that a browser parses as HTML.Headers:the web SAPI sends an HTTPContent-Typeheader (text/html; charset=UTF-8by default) before your output — the browser needs this to know how to render the response. CLI output has no headers at all, since there is no HTTP request to respond to.Whitespace handling:a\nin anechomatters in a terminal but is invisible in rendered HTML, since browsers collapse plain whitespace into a single space.Wrapping markup:CLI scripts almost never need<!DOCTYPE html>/<html>/<body>— there is no browser to interpret them. A page meant to be viewed does.