← Back to PHP Course | Chapter 1: Introduction & Basics | Lesson 4 of 13

PHP First Program

Your first PHP program is like your first Hello spoken out loud: a tiny piece of code that makes the page say something. PHP code goes inside special opening and closing markers so the computer knows where it starts and ends.
Syntax
php
<?php
    echo "text to output";
?>

Opening and Closing Tags

Every block of PHP code starts with <?php and ends with ?>.

The web server treats anything between those tags as PHP to execute, and anything outside them — including surrounding HTML — as plain text to pass through untouched, which is what lets a single file mix markup and logic.

Example: Opening and Closing Tags

php
<p>Before the tag: plain HTML</p>
<?php
// Print "Inside the tag: PHP executes this." to the output
echo "Inside the tag: PHP executes this.";
?>
<p>After the tag: plain HTML again</p>

The echo Statement

echo sends text (or the result of an expression) straight into the page's output. It's technically a language construct rather than a function, so you can call it without parentheses, and you can pass it several comma-separated values in one call.

Example: The echo Statement

php
<?php
// Print "Hello", " ", "World!" to the output
echo "Hello", " ", "World!";
?>

Handling Simple Strings

Text in PHP goes inside single or double quotes, but they behave differently: single quotes print almost everything literally (including a $variable reference as plain text), while double quotes actively evaluate variables and interpret escape sequences like \n for a newline.

Example: Handling Simple Strings

php
<?php
// Declare `$name`, set to "Alice"
$name = "Alice";
// Print 'Single quotes: $name stays literal' to the output
echo 'Single quotes: $name stays literal';
// Print "\n" to the output
echo "\n";
// Print "Double quotes: $name is substituted\n" to the output
echo "Double quotes: $name is substituted\n";
?>

Simple Math Output

PHP evaluates arithmetic expressions the moment it reaches them, so echo 4 + 6; doesn't print the text '4 + 6' — it computes the sum first and outputs 10.

This is the basis for anything more complex you'll do with numbers later, like calculating totals or formatting prices.

Example: Simple Math Output

php
<?php
// Print `4 + 6` to the output
echo 4 + 6;
?>

Running Your First Script

Once you write and run a script, check the output carefully against what you expected — a missing semicolon or an unclosed quote is the most common first-script error, and PHP's error message will usually point you to the exact line where it got confused.

Example: Running Your First Script

php
<?php
// Missing semicolon below would trigger: parse error, unexpected token
echo "Script ran successfully.";
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting the semicolon after echo Hello, which causes a parse error on the next line.
  2. Leaving out the opening <?php tag or writing a bare <? short tag, which many servers do not have enabled, so the code prints as plain text.
  3. Mixing up quotes, like echo Its here';, where the apostrophe ends the string early and causes a syntax error.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.