PHP Tutorial
The server side language behind much of the web.
Getting started4 lessons
Basics20 lessons
- 5VariablesNames that start with $.
- 6Data Typesstring, int, float, bool, array, null.
- 7StringsWorking with text.
- 8Numbers and Mathint, float and math functions.
- 9OperatorsComparison, logic and null handling.
- 10echo, print and printfecho prints several values, print returns 1, and printf and sprintf format with %s %d %f.
- 11PHP Constants and Magic Constantsdefine() and const create constants.
- 12PHP Type Juggling and CastingPHP converts types automatically.
- 13null, isset and emptynull means no value.
- 14More String Functionstrim, ltrim, rtrim, ucwords, wordwrap, nl2br, str_pad, str_repeat, strrev, similar_text, l.
- 15PHP Math and Random NumbersFunctions: abs, ceil, floor, round, sqrt, pow, fmod, max, min, pi, rand, mt_rand and rando.
- 16PHP Tags and StatementsPHP code lives between <?php and ?>.
- 17Variables in DepthVariables start with $ and are case sensitive.
- 18String Interpolation and HeredocDouble quoted strings read variables inside.
- 19Integers and FloatsPHP has int and float.
- 20Casting and ConversionCast with (int), (float), (string), (bool) and (array).
- 21== and ===== compares after type juggling, === compares value and type.
- 22Null Coalescing?? returns the left value if it exists and is not null, otherwise the right.
- 23Constants and Magic Constantsdefine() and const create constants.
- 24Operators SummaryArithmetic, assignment (+=, .=), comparison, logical (&&, ||, and, or), string (.) and inc.
Control flow10 lessons
- 25If, Else and MatchMaking decisions.
- 26Loopswhile, for and foreach.
- 27Switch and Match In Depthswitch falls through without break and compares loosely.
- 28break, continue and Alternative Syntaxbreak exits a loop and continue skips a round.
- 29Ternary, Spaceship and Null SafeThe ternary ?: shortens if else.
- 30if, elseif and elseRun code by condition.
- 31match Expressionmatch returns a value, compares strictly and throws if nothing matches unless you add defa.
- 32for and while Loopsfor has init, condition and step.
- 33foreachforeach walks arrays with values or keys and values.
- 34break and continuebreak stops a loop and continue skips to the next round.
Arrays and functions28 lessons
- 35ArraysIndexed and associative arrays.
- 36Array Functionsmap, filter, reduce and sorting.
- 37FunctionsReusable code.
- 38Variable ScopeLocal, global and static.
- 39Sorting Arrayssort and rsort reorder values, asort and arsort keep keys, ksort and krsort sort by key an.
- 40Multidimensional ArraysArrays can contain arrays.
- 41More Array Functionsarray_merge, array_combine, array_flip, array_unique, array_slice, array_splice, array_sea.
- 42Spread and DestructuringThe .
- 43Closures and CallablesAnonymous functions can capture variables with use.
- 44Recursion and GeneratorsA function can call itself.
- 45ArraysPHP arrays are ordered maps.
- 46Adding and Removing Itemsarray_push, array_pop, array_shift, array_unshift, unset and array_splice change arrays.
- 47Searching Arraysin_array tests presence, array_search finds the key, array_key_exists checks a key and iss.
- 48array_map, array_filter, array_reduceTransform, select and fold arrays with callbacks.
- 49array_slice, merge and combinearray_slice takes a part, array_merge joins arrays, array_combine pairs keys and values an.
- 50Unique, Count and Statisticsarray_unique removes duplicates, array_count_values counts, array_sum and max compute tota.
- 51Sortingsort and rsort sort values, asort and arsort keep keys, ksort sorts by key and usort uses .
- 52implode, explode and str_splitexplode splits text into an array and implode joins an array into text.
- 53FunctionsDefine with function.
- 54Named and Variadic ArgumentsNamed arguments choose parameters by name.
- 55Arrow Functions and Closuresfn creates short functions that capture outer variables by value.
- 56Variable Scope and staticVariables inside functions are local.
- 57Type DeclarationsAdd types to parameters and returns.
- 58More String Functionsstrlen, strtoupper, ucfirst, trim, str_replace, substr, strpos and str_contains cover most.
- 59Checking Stringsstr_contains, str_starts_with and str_ends_with give clear answers.
- 60Formatting Numbersnumber_format adds separators and decimals.
- 61Datesdate() formats a timestamp, mktime and strtotime create timestamps and DateTime is the mod.
- 62Math Functionsabs, sqrt, pow, max, min, pi, round and random_int help with numbers.
Object oriented PHP16 lessons
- 63Classes and ObjectsProperties, methods and constructors.
- 64Inheritance and InterfacesExtending and contracts.
- 65Static Members, Constants and TraitsClass level things and reuse.
- 66Constructors, Readonly and EnumsConstructor promotion declares properties in the constructor.
- 67Abstract Classes and InterfacesAn abstract class cannot be instantiated and can mix implemented and abstract methods.
- 68Magic Methods__construct, __destruct, __toString, __get, __set, __call, __invoke and __clone run in spe.
- 69Namespaces and AutoloadingNamespaces avoid name clashes and use imports them.
- 70Type Declarations and Union TypesDeclare parameter, return and property types.
- 71Classes and ObjectsA class holds properties and methods.
- 72public, protected, privatepublic is open, protected is for the class and children, private is for the class only.
- 73Inheritanceextends inherits members and parent:: calls the parent.
- 74InterfacesAn interface lists methods a class must write.
- 75Static Members and Class Constantsstatic belongs to the class and const holds fixed values.
- 76EnumsEnums (PHP 8.1) define a fixed set of cases, optionally backed by values.
- 77readonly Propertiesreadonly properties can be set once, then never changed.
- 78Exceptionsthrow an Exception and catch it with try catch.
Web features and data24 lessons
- 79Forms and Superglobals$_GET, $_POST and $_SERVER.
- 80Sessions and CookiesRemembering visitors.
- 81JSONjson_encode and json_decode.
- 82FilesReading and writing.
- 83Databases with PDOSafe queries with prepared statements.
- 84Errors and Exceptionstry, catch and throw.
- 85Dates and Regular Expressionsdate(), DateTime and preg functions.
- 86Form Handling and ValidationCheck request method, validate every input with filter_var and rules, show errors and esca.
- 87File UploadsForms with enctype multipart/form-data fill $_FILES.
- 88Passwords and SecurityHash passwords with password_hash and check them with password_verify.
- 89HTTP Requests with cURLcURL fetches URLs.
- 90Headers, Redirects and Status Codesheader() sends HTTP headers before any output.
- 91Regular Expressions In Depthpreg_match with named groups, preg_replace_callback, preg_split and preg_quote.
- 92DateTime In DepthDateTimeImmutable never changes in place.
- 93JSONjson_encode turns data into JSON and json_decode reads it.
- 94Superglobals$_GET, $_POST, $_SERVER, $_COOKIE, $_SESSION and $_FILES carry request data.
- 95Escaping Outputhtmlspecialchars stops XSS by turning < and > into entities.
- 96Validating Input with filter_varfilter_var validates emails, URLs and integers and sanitises values.
- 97Reading and Writing Filesfile_put_contents and file_get_contents are the quick way.
- 98Regular Expressionspreg_match tests a pattern, preg_replace replaces and preg_split splits.
- 99Password Hashingpassword_hash makes a salted hash and password_verify checks it.
- 100Practice: Word CounterCombine strings and arrays to count words.
- 101Practice: FizzBuzzLoop with conditions.
- 102Practice: Simple CartUse arrays of arrays and array_sum with array_column.
Advanced PHPPremium6 lessons
- 103Composer and PackagesComposer manages PHP dependencies.
- 104PDO Transactions and Fetch ModesWrap related writes in beginTransaction and commit, and roll back on error so the data sta.
- 105Preventing SQL InjectionNever put user input into SQL strings.
- 106Authentication FlowA login form posts credentials, the server checks password_verify, regenerates the session.
- 107Testing with PHPUnitPHPUnit is the standard test framework.
- 108Performance and OPcacheOPcache keeps compiled PHP in memory.
PHP in practicePremium6 lessons
- 109Building a REST APIReturn JSON with proper status codes, route by method and path, validate input and version.
- 110Laravel OverviewLaravel is the most popular PHP framework.
- 111WordPress and PHPWordPress is written in PHP.
- 112Templates and Separation of ConcernsKeep logic and HTML apart.
- 113Logging and Error ReportingShow errors in development only.
- 114PHP Best PracticesUse strict types, type declarations, PSR-12 style, Composer, prepared statements, password.