Introduction to PHP
PHP (PHP: Hypertext Preprocessor) is a widely used open-source server-side scripting language designed for web development. Created by Rasmus Lerdorf in 1994, PHP powers a large fraction of the world's websites, including WordPress, Wikipedia, and Facebook's original codebase.
What PHP Does
PHP runs on the server before a page is sent to the browser. A request arrives; PHP executes, producing HTML; the HTML is returned. Because PHP runs server-side, it can access databases, file systems, and network resources that client-side JavaScript cannot. PHP and HTML can be freely mixed in the same file.
Setting Up
A typical development stack is LAMP (Linux, Apache, MySQL, PHP), WAMP (Windows), or XAMPP for cross-platform. Files with the .php extension are parsed by the PHP interpreter. <?php ... ?> tags mark PHP code inside any file.
Syntax Basics
PHP is loosely typed; variables begin with $. Statements end with a semicolon. Comments use //, #, or /* ... */. The echo and print constructs send output to the response body. <?= $name ?> is shorthand for <?php echo $name; ?>.
Data Types
PHP has scalar types (int, float, bool, string), compound types (array, object), and special types (null, resource). Type juggling converts values automatically, often surprising newcomers. PHP 7+ supports scalar type hints and return types for safer code.
Operators
Arithmetic, comparison, logical, assignment, and string operators are mostly familiar. The concatenation operator is .. Loose equality == converts types; strict equality === does not and is preferred. The null coalescing operator ?? simplifies default-value assignment.
Control Structures
PHP supports if/else, switch, while, do ... while, for, and foreach. The alternative syntax (if ... endif) suits HTML templating. break, continue, and match (since PHP 8) provide flow control.
Arrays
Arrays in PHP are ordered maps, doubling as lists and associative arrays. array() or [ ] literal syntax creates them. Keys may be integers or strings. Built-in functions like count, array_map, array_filter, and sort manipulate them. Multidimensional arrays model tables and trees.
Functions
Functions are declared with function and may have default parameter values, variadic arguments, and type declarations. Anonymous functions (closures) and arrow functions support functional style. Functions have their own scope; globals require the global keyword or the $GLOBALS array.
Strings
Double-quoted strings interpolate variables and escape sequences; single-quoted do not. Heredoc and nowdoc syntax handle multiline text. Functions like strlen, strpos, str_replace, explode, and sprintf cover most string work.
Include and Require
include, require, and their _once variants load PHP from other files. This supports modular code. Modern projects prefer Composer autoloaders following PSR standards instead of manual includes.
Summary
PHP offers a low-barrier entry to web development with rich built-in functions and easy database integration. Mastering its syntax, types, control structures, and array handling is the basis for every further topic in the course.