Understand the idea
Start with a real question
A small stationery shop needs a total for four notebooks. You can calculate it in your head today, but the price or quantity may change tomorrow. Your first PHP program will make the inputs explicit, calculate their product and print a result someone can understand.
There is one important boundary to get clear first. On a typical production website, PHP runs on a server. In this practice workspace, an isolated interpreter runs in your browser so you can learn the language without renting a server. A successful exercise does not secretly create a hosted application or a database.
That limitation is useful for this first lesson. We can concentrate on variables, assignment, multiplication and output. You will have a complete small calculation to explain before adding networking, forms or persistence.
Understand the idea
PHP and the environment running it
PHP is a programming language commonly used on web servers. In a normal web application, a server runs the PHP program and sends its output to a browser. The browser does not run the server's PHP source merely because someone included it in an HTML file.
Our practice editor uses a PHP interpreter compiled to WebAssembly, running in your browser. This lets you practise language features without renting a server. It does not create a public PHP backend, connect to a production database or reproduce every web-server feature. Later lessons distinguish portable language examples from examples that require a separate server environment. Never put production credentials into a practice program.
See how it works
Example 1: produce plain text
PHP exampleRead the code, then try a change.
Try this example
<?php
echo "Welcome to PHP\n";
echo "Run one example at a time.\n";
Expected output:
ReferenceCompare this with the explanation.
Welcome to PHP
Run one example at a time.
<?php starts PHP code. echo writes output, and each semicolon ends a statement. The double-quoted \n escape produces a newline in this text output. In a file containing only PHP, leaving out the closing PHP tag is an established way to avoid accidentally emitting trailing material outside the program.
The output panel here displays text. In a web application, the context consuming output matters: an HTML response interprets markup and normally collapses ordinary whitespace. A newline in text output is not a universal substitute for an HTML element.
See how it works
Example 2: calculate before printing
PHP exampleRead the code, then try a change.
Try this example
<?php
echo 8 + 4;
echo "\n";
echo 8 * 4;
echo "\n";
Expected output:
ReferenceCompare this with the explanation.
12
32
Arithmetic expressions produce values before echo writes them. The plus sign performs addition here, and the asterisk performs multiplication. The numbers are not quoted strings. Later lessons explain types, numeric conversion and operator precedence; for now, use deliberate simple expressions so the result is unambiguous.
Predict the result after changing only the first expression to (8 + 4) * 2. Parentheses make that grouping explicit. It should print 24 on the first output line while the second calculation remains unchanged.
See how it works
Example 3: named values make calculations readable
PHP exampleRead the code, then try a change.
Try this example
<?php
$price = 120;
$quantity = 3;
$total = $price * $quantity;
echo "Total: ";
echo $total;
echo "\n";
Expected output:
ReferenceCompare this with the explanation.
Total: 360
PHP variable names begin with $. The assignment operator associates a value with the variable. Read this example as a sequence: establish a price, establish a quantity, calculate their product, then write the label and result. Variables make it possible to change an input value without hunting for unrelated repeated numbers.
Variable names are case-sensitive. $price and $Price do not identify the same variable. Use descriptive consistent names, and initialise each variable before reading it. A misspelled name can produce a diagnostic even when a similar-looking name was assigned earlier.
Spot the mistake
Debugging and boundaries
Start by checking the opening tag, matching quotes, variable spelling and semicolons. A parser error near a later line can originate from an earlier missing quote or semicolon. Change one fault at a time and run the program again. Do not hide diagnostic messages to make a broken example appear successful.
This lesson needs no file reads, database connection or network request. Keeping the first programs self-contained makes their behaviour reproducible in the browser interpreter and on a suitably configured PHP installation.
Your turn
Independent exercise
Declare a notebook price and a notebook quantity. Calculate a total and print a readable label followed by the total and a newline. Choose your own sensible positive values. Explain which lines set input values and which line calculates the result.
Show hint Every reference to a PHP variable needs its dollar sign. Keep the arithmetic separate from the output statements so you can inspect each step.
Show solution With $notebookPrice = 75; and $notebookQuantity = 4;, assigning their product to $notebookTotal and echoing that value produces 300. Add an explicit text label and a newline using separate echo statements.
See how it works
Connect a PHP statement to its output
The interpreter runs the PHP instructions, and echo writes the output they produce. The browser practice runtime is an interpreter, not a public web server. That distinction matters when reading examples elsewhere: a form, session or database example can depend on server configuration that this isolated exercise does not supply. Here the inputs are explicit variables, so you can reproduce each calculation without hidden environment state. Follow the dollar signs, statement boundaries and output calls rather than guessing from the final text.
Understand the idea
Slow down at the important part
Understand the idea
Follow the journey from input to printed result
Begin with two decisions: what is the price of one notebook, and how many notebooks are being bought? Store those as separate variables. The multiplication then expresses a relationship between named values rather than unexplained numbers copied into several places.
The dollar sign is part of PHP's variable syntax. Keep the same spelling and letter case every time you refer to a variable. A variable that has not been initialised is not an invitation to guess what PHP might substitute. Assign the intended value before the instruction that needs it.
Now distinguish calculation from output. Assigning a total does not itself print the total. An echo statement produces output. This separation is useful: you can inspect where a wrong value was calculated without blaming the instruction that merely displayed it.
Understand the idea
Punctuation has a job
The opening PHP tag identifies PHP code in the source used here. Semicolons terminate the simple statements in this lesson. Matching quotation marks identify a string. These symbols are easy to overlook when your eyes focus only on variable names and prices, so give punctuation its own review pass.
If you encounter a parse error, start with the indicated area and inspect the preceding statement too. A missing terminator can make the next line look suspicious even though the earlier line caused the problem. Repair one cause and run again rather than adding punctuation randomly until the message changes.
See how it works
Text output is not an HTML layout
The output panel shows the text your program emits. A newline character in a double-quoted string separates lines in that text. A browser rendering an HTML document treats whitespace differently, which is a separate concern. Do not use a successful text exercise to infer that every display environment handles newlines in the same way.
Choose a label that tells someone what the number measures. Printing 300 is a calculation result; printing a label followed by 300 makes it easier for a reader to recognise the total. Good output communicates the result rather than merely proving that echo executed.
Your turn
Build it, change it, explain it
Build the notebook total from a blank editor. Initialise a price and quantity, calculate a separate total, and print a descriptive label followed by that total and a newline. Predict the result before running. Then change the quantity and verify the new multiplication without editing the label by hand.
Next, place an input change after the total assignment. Does the earlier total update on its own? Explain the execution order, then perform the calculation again where the new result is needed. This is the same practical distinction between stored results and continuing spreadsheet formulas that appears in other languages.
Review your program as if another student will maintain it. Can they identify the two inputs? Is every variable initialised before use? Are all the simple statements terminated? Can they change the quantity without hunting through a printed string for a hard-coded answer?
Understand the idea
What this makes possible
The result is small but complete: named inputs, a calculation, and understandable output. That separation will remain useful when input eventually comes from a form or a file. Learn the language's behaviour here without confusing the practice interpreter with a production hosting service.