Lab 01: Hello World & PHP Basics

🎯 Objective

Learn to write your first PHP program, understand PHP syntax, use echo for output, and work with variables.

πŸ“š Background

PHP (Hypertext Preprocessor) is a widely-used server-side scripting language. PHP code is embedded within <?php and ?> tags. Variables start with $ and are dynamically typed, meaning you don't need to declare their type upfront.

⏱️ Estimated Time

20 minutes

πŸ“‹ Prerequisites

  • Basic understanding of programming concepts

  • Docker installed and innozverse-php:latest image available

πŸ› οΈ Tools Used

  • PHP 8.3 CLI

  • Docker (innozverse-php:latest)


πŸ”¬ Lab Instructions

Step 1: Your First PHP Script

Create a file /tmp/lab01.php:

Run it:

πŸ“Έ Verified Output:

πŸ’‘ echo outputs text to the screen. The \n is a newline character.


Step 2: Variables

πŸ“Έ Verified Output:

πŸ’‘ Variables are prefixed with $. PHP is loosely typed β€” no type declaration needed.


Step 3: Variable Types and var_dump

πŸ“Έ Verified Output:

πŸ’‘ var_dump() shows both the type and value β€” great for debugging.


Step 4: String Concatenation

πŸ“Έ Verified Output:


Step 5: Constants

πŸ“Έ Verified Output:

πŸ’‘ Constants don't use $ and cannot be changed after definition.


Step 6: PHP Comments

πŸ“Έ Verified Output:


Step 7: Print and Printf

πŸ“Έ Verified Output:


Step 8: A Complete Mini Program

πŸ“Έ Verified Output:


βœ… Verification

Run all snippets through Docker successfully. Confirm:

  • echo outputs text

  • Variables hold different types

  • String interpolation works with "$var" and "{$var}"

  • Constants are defined with define() or const

🚨 Common Mistakes

Mistake
Fix

Forgetting $ on variables

Always prefix variables with $

Using single quotes for interpolation

Use double quotes: "Hello $name"

Missing semicolons

Every statement must end with ;

<? without php

Use full <?php opening tag

πŸ“ Summary

  • PHP scripts begin with <?php

  • Variables start with $ and are dynamically typed

  • Use echo or print for output

  • String interpolation works in double-quoted strings

  • var_dump() shows type and value

  • Constants use define() or const

πŸ”— Further Reading

Last updated