HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to PHPVariablesData TypesOperatorsStringsArraysAssociative ArraysConditions (if / else)Loops (for / foreach / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array
🔁 Condition & Loop
🧩 Function
🔄 Type & Conversion
PHP›Introduction to PHP

Introduction to PHP

First step into the server-side language PHP: printing with echo, variables with $, and embedding variables into text.

Simple enough for a 5-year-old to understand

Print with echo

PHP code starts with <?php. echo is used to output to the screen.

Your first program
1
<?php
PHP code starts here.
DefinitionPHP code starts here.
2
echo "Hello!";
Prints the text inside quotes. Ends with ;.
Output / InputPrints the text inside quotes. Ends with ;.
DefinitionOutput / Input
💡 Hint: Every statement ends with a semicolon (;) — this is mandatory in PHP.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (2 left)

1.Which one is used to output to the screen in PHP?
2.Print exactly Hello World to the screen.

🎯 Expected output: Hello World

Loading editor…

About this topic

PHP is a language designed for the web, and this design choice is visible everywhere. A PHP file is essentially an HTML template; code starts where you type the opening tag, and it switches back to HTML when you close it. Therefore, you don't need to learn a separate template engine to learn PHP.

PHP runs on the server. The browser never sees the PHP code; it only receives the HTML generated by the code. This naturally hides the source code and database information — but it also means debugging in the browser doesn't work, and you need to track errors from the server log.

Modern PHP is noticeably different from PHP of a decade ago: type declarations, namespaces, the Composer package manager, and a serious performance boost have arrived. Most patterns from old tutorials are no longer recommended.

What you will learn

  • Writing PHP tags and nesting them with HTML
  • Producing output with echo
  • The necessity of the semicolon
  • Comment line formats
  • That code runs on the server and its implications

When to use?

echo is the fundamental way to output values into HTML. In files containing only PHP, it is recommended NOT to write the closing tag; an invisible space after the tag causes a "headers already sent" error when you try to send headers.

Enabling error display during development and disabling it in production is standard practice: an error message showing file paths and database details to the user is a security vulnerability.

Common mistakes

Wrong

<?php
echo "Hello"
?>

Semicolon missing; PHP throws "Parse error: syntax error, unexpected end of file". Because it's a parsing error, the page doesn't run at all, and you see a blank screen.

Correct

<?php
echo "Hello";

Wrong

<?
echo "Hello";
?>

The short opening tag depends on the server configuration and is disabled in most setups; the code won't run, it gets printed to the browser as raw text. The full tag should always be used.

Correct

<?php
echo "Hello";

Wrong

<?
echo "Hello";
?>
   

The space and newline after the closing tag go directly into the output. These invisible characters cause a "Cannot modify header information — headers already sent" error when you want to send a header or redirect later. In files containing only PHP, the closing tag is omitted.

Correct

<?php
echo "Hello";

Frequently asked questions

Why does my PHP code show as code when I open it in the browser?

Because you opened the file directly from the file system, not through a server. PHP requires a web server or PHP's built-in server to run; you should see a server address in the address bar, not a file path.

What is the difference between echo and print?

Practically none. echo can take multiple values separated by commas and is slightly faster; print takes one value and returns 1, so it can be used within expressions. echo is preferred in everyday code.

I see a blank white page, what happened?

Almost always, there is a fatal error and error display is turned off. Turn on error display in your development environment or check the server's error log. A blank page is not a PHP behavior, but a hidden error message.

Summary

PHP runs on the server and is nested with HTML; the browser only sees the generated output. Always use the full opening tag, omit the closing tag in files containing only PHP, and a blank page is a sign of a hidden error.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code