PHP Tutorial

Welcome to the modern PHP tutorial! This PHP tutorial helps you learn how to develop dynamic websites and web applications using PHP from scratch.

PHP is one of the most popular programming languages for web development.

PHP allows you to develop various web applications, including blogs, content management systems (CMS), and online stores.

Section 1. Getting Started with PHP

  • What is PHP – quickly introduce you to PHP and its ecosystem.
  • Install PHP – learn how to install a PHP development environment locally on your computer.
  • Hello, World – walk you through the steps of writing and running the first PHP script.

Section 2. PHP Fundamentals

  • Syntax – introduce you to the basic PHP syntax, including sensitivity, statements, whitespace, and linebreak.
  • Variables – show you how to use variables to store data in PHP.
  • Constants – define constants that hold a value that doesn’t change throughout the script.
  • Comments – learn how to document your code effectively using comments.
  • var_dump – learn how to dump the information about a variable.

Section 3. PHP Types

  • Data types – give you a quick overview of PHP types, including scalar types, compound types, and special types.
  • Boolean – learn how to represent a truth value using the boolean type.
  • Integer – explain to you how to work with integers in PHP.
  • Float – guide you on how to represent floating-point numbers.
  • String – learn about various forms of strings in PHP, including single-quoted and double-quoted strings.
  • Null – introduce to you the NULL type and how to check if a variable is NULL or not.
  • Type Casting – learn how to cast a value of one type to another.
  • Type Juggling – explain to you how the type juggling works in PHP.

Section 4. Operators

This section covers the most commonly used operators in PHP including logical and comparison operators.

  • Assignment operators – learn about the most commonly used assignment operators effectively.
  • Comparison operators – introduce to you the comparison operators that allow you to compare two values of the same or different types.
  • Logical AND operator (&&) – show you how to use the logical AND operator to build complex logical expressions.
  • Logical OR operator (||) – learn how to use the logical OR operator to form complex logical expressions.
  • Logical NOT operator (!) – learn how to negate a Boolean expression.
  • Operators – explain to you the available operators in PHP and help you apply them effectively.

Section 5. Control flow

  • if – learn how to execute a code block based on a condition.
  • if-else – show you how to run a code block if a condition is true and another code block if the condition is false.
  • if-elseif – show you how to execute the corresponding code block based on multiple conditions.
  • Ternary operator – guide you on using the ternary operator to make your code shorter and more readable.
  • switch – learn how to use the switch statement effectively.
  • for – execute a code block a fixed number of times.
  • while – explain to you how to run a pretest loop.
  • do-while – understand the posttest loop and learn when to use it in the script.
  • break – learn how to break out of a loop.
  • continue – show you how to skip the current loop iteration and start the new one from the beginning.

Section 6. Functions

  • Functions – show you how to define user-defined functions.
  • Function parameters – learn about function parameters, passing arguments by value and by reference.
  • Default parameters – show you how to set a default value for a parameter.
  • Named arguments – explain to you the named arguments that allow you to pass arguments to a function using the parameter names rather than parameter positions.
  • Variable scopes – learn about the variable scopes, including global, local, static, and function parameters.
  • Type hints – show you how to declare types for function parameters and return value.
  • Strict typing – learn how to enable strict mode using the strict typing directive.
  • Variadic functions – learn how to define function that accepts a variable number of arguments.

Section 7. Array

  • Arrays – show you how to handle a list of items effectively with Array data type.
  • Associative Arrays – learn how to use the associative arrays to reference elements by names rather than numbers.
  • foreach – show you how to use the foreach statement to iterate over elements of an index array or an associative array.
  • Multidimensional Array – guide you on how to define a multidimensional array and manipulate its elements effectively.
  • array_unshift() – add one or more elements to the beginning of an array.
  • array_push() – add one or more elements to the end of an array.
  • array_pop() – remove an element from the end of an array and return it.
  • array_shift() – remove an element from the beginning of an array and return it.
  • array_keys() – get the keys of an array.
  • array_key_exists() – check if a key exists in an array.
  • in_array() – check if a value exists in an array.
  • array_reverse() – reverse the order of elements in an array.
  • array_merge() – merge multiple arrays into one.
  • Spread operator – show you how to use the spread operator with an array expression.
  • list – learn how to use the list construct to assign the array elements to multiple variables in a single operation.
  • Array destructuring – show you how to unpack array elements to multiple variables.

Section 8. Sorting Arrays

This section introduces to you various helpful functions to sort elements of an array.

  • sort – sort values of an array in the ascending order (or use the rsort() function to sort the values of an array in the descending order).
  • ksort() – sort array keys of an associative array.
  • usort() – sort an array with a user-defined function.
  • asort() – sort an associative array and maintain the index association.
  • uasort() – sort an associative array with a user-defined comparison function and maintains the index association.
  • uksort() – sort the keys of an array with a user-defined comparison function.

Section 9. Advanced Functions

Section 10. Variable constructs

  • isset – return true if a variable is set and not null.
  • empty – returns true if a variable does not exist or is false.
  • is_null – returns true if a variable does not exist or is null.

Section 11. Advanced Array Operations

  • Map – map array elements using the array_map() function.
  • Filter – filter the elements of an array by a callback using the array_filter() function.
  • Reduce – reduce an array to a single value by a callback function using the array_reduce() funtion.

Section 12. Organizing PHP files

  • Include – show you how to use the include construct to make PHP script files more organized.
  • include_once – show you how to include a file once using the include_once construct.
  • require & require_once – learn how to include files in a more robust way to a file with the require and require_once constructs.
  • Using __DIR__ with a file include – show you how to use the __DIR__ magic constant when including a file.
  • Variable variables – learn how to pass data to the included script using variable variables.

Section 13. State Management

  • Cookie – learn how to store a piece of data on web browsers.
  • Session – explain to you how sessions work.

Section 14. Processing Forms

  • PHP form – show you how to process HTML forms.
  • filter_has_var() function – check if a variable of a type exists.
  • filter_var() function – sanitize and validate a variable using a filter.
  • filter_input() function – sanitize and validate a variable of a type.
  • Form validation – walk you through the step of validating a form in PHP.
  • Checkbox – learn how to process checkboxes.
  • Multiple checkboxes – show you how to handle a form that has multiple checkboxes with the same name.
  • Radio button – learn how to handle radio buttons and radio groups.
  • Select – show you how to handle the select element.
  • CSRF – learn about CSRF attacks and how to prevent them by using a one-time token in PHP.
  • Flash messages – show you how to implement session-based flash messages for creating and displaying messages across pages.
  • Post-Redirect-Get (PRG) – learn how to use the PRG technique to prevent the double submit problem.
  • File Upload– guide you on how to upload a file to the server securely.
  • Upload multiple files – show you how to upload multiple files to the server securely.
  • Contact form – show you how to create a contact form that sends email and prevent spam.
  • Validation – learn how to build a reusable validate() function to validate inputs.
  • Sanitize input – learn how to implement a reusable sanitize() function to sanitize inputs.

Section 15. Working with Files

  • Open a file – learn how to open a file for reading and/or writing using the fopen() function.
  • Check a File Exists – show you how to check if a file exists using the file_exists(), is_readable(), and is_writable() functions.
  • Read a file – show you how to read the contents of a file into a string or read a file line by line into a string.
  • Read a file into a string – guide you on using the file_get_contents() function to read a file into a string.
  • Read a file into an array – show you how to read the contents of a file into an array, each line of the file is an array element.
  • Download a file – show you how to download a file from the webserver.
  • Copy a file – show you how to copy a file to a new one.
  • Delete a file – learn how to delete a file.
  • Rename a file – guide you on renaming a file to a new one and optionally move it to another directory.
  • Work with CSV Files – learn how to create and read CSV files in PHP.
  • Get the size of a file – show how to get the size of a file in bytes using the filesize() function.
  • File permissions – learn about the file permission and how to change it using the chmod() function.

Section 16. Working with Directories

  • Manage directory – show you how to manage directories in PHP.
  • glob – get files/directories in a directory that match a pattern.
  • dirname – get the directory name of a path.
  • basename – get the trailing name component of a path.
  • pathinfo – return the components of a file path including directory name, trailing name component, filename, and file extension.

Section 17. String operations

  • strlen – return the length of a string in bytes.
  • substr – search for a substring in a string.
  • strpos– locate the first occurrence of a substring in a string.
  • str_replace– replace all occurrences of a substring with a new one in a string.
  • implode – join an array of strings into a string using a separator.
  • explode – split a string by a separator into an array of strings.
  • trim – remove the whitespace characters or other characters from both ends of a string.
  • ltrim – remove the whitespace characters or other characters from the beginning of a string.
  • rtrim – remove the whitespace characters or other characters from the end of a string.
  • htmlspecialchars() – show you how to escape special characters in a string.
  • str_contains() – return true if a string contains with a substring.
  • str_starts_with() – return true if a string starts with a substring.
  • str_ends_with() – return true if a string ends with a substring.
  • strtolower() – accept a string and return a new string with all alphabetic characters converted to lowercase.
  • strtoupper() – accept a string and return a new string with all alphabetic characters converted to uppercase.
  • ucfirst() – return a new string with the first alphabetic character converted to uppercase.
  • ucwords() – return a new string with the first alphabetic character of each word converted to uppercase.

Section 18. Regular Expressions

  • Regular expressions – introduce you to the regular expressions and show you how to use the preg_match(), preg_match_all(), and preg_replace() functions.
  • Character Classes – match a character in a character set (\d, \w, \s)
  • Anchors – match at the beginning (^) and/or end ($) of a string or line.
  • Word Boundary – match the whole word using the word boundary (\b).
  • Sets & Ranges – match a character in a set ([abc]) or range of characters ([a-z]).
  • Quantifiers – match a number of instances of a character or character class using quantifiers ( \d{2,4}, \w+…).
  • Greedy Quantifiers – learn how the greedy quantifiers work.
  • Non-greedy (or lazy) Quantifiers – show you how to turn a greedy quantifier into a non-greedy quantifiers and explain how the non-greedy quantifiers work.
  • Capturing groups – include a part of a match in the matches array and assign it a name using a named group.
  • Backreferences – learn about the regex backreferences.
  • Alternation – show you how to use the alternation, which is simply an OR operator in regular expressions.
  • Lookahead – learn how to match A only if followed by B.
  • Lookbehind – learn how to match A only if there’s is B before it.
  • preg_match() – search for a match in a string using a regular expression.
  • preg_match_all() – search for all matches in a string using a regular expression.
  • preg_replace() – search and replace strings using a regular expression.

Section 19. PHP Date & Time

  • Time– learn how to work with time.
  • Date – show you how to use the date() function to format a timestamp.