Skip to content
Kodivo is live on Product Hunt. Share your review

Your first PHP program: write, run and understand the code with AI

A message on the screen is enough to connect code with its result. Then change one value and predict what will happen.

Write a complete script

Create a practice folder and save the following code in hello.php. Use a plain text file in your code editor. The example requires PHP 8 or later and runs in a terminal without a database.

hello.php
<?php

$name = "Ada";
echo "Hello, " . $name . "!" . PHP_EOL;

Output

Hello, Ada!

$name is a variable containing the text Ada. The dot joins pieces of text. echo prints the result, and PHP_EOL adds the operating system's line ending. In a file containing only PHP, you can omit the closing ?> tag.

Ask the agent to explain the order in which the output is assembled. Then change the name yourself. A small example makes it easy to connect one change in a file with a visible change on the screen.

Run the file

Open a terminal in the folder containing the file. Check the PHP version, then execute the script:

Terminal
php -v
php hello.php

The expected output of the second command is Hello, Ada! followed by a new line. If the terminal cannot find the file, check the folder and filename, including its extension. If it cannot find php, revisit the runtime installation from the previous article.

Opening a PHP file with a double-click does not replace running the interpreter. A browser may show the file contents instead of executing them. This example uses the terminal. The PHP command-line documentation explains this execution mode.

Change the behavior with an agent

Request the next change: “Add a completed-lesson count set to 2. Print it on a second line as Completed lessons: 2. Keep one file and add no libraries.” This instruction defines the data, output and implementation boundaries.

Before executing the changed file, ask for the expected output. Compare it with an actual run. If the agent ran the command for you, read both the command and its result. Then change the count to zero and check whether the message still matches the requirement.

Continue in Kodivo

Kodivo shows code beside its expected output and explanation. The public demo covers null, so it introduces another topic rather than reproducing this program. An account lets you follow published lessons and save your position.

Through MCP, an agent can fetch a lesson and discuss its example. Running your local hello.php happens separately through your client's tools. Ask for an explanation of one variable and one result before extending the program.

Kodivo lesson card showing that null differs from false and zero, with PHP code and its output.
The “Null and missing values” sample lesson in Kodivo. Code and output sit beside the explanation.