Independent Consultant
Course Description
Learn the language powering over 70% of the web and navigate CMSs like WordPress with ease! Get a quick introduction to PHP basics, including syntax, variables, loops, and functions. Work with forms, build APIs, and connect to an SQLite database to craft dynamic, data-driven web applications!
This course and others like it are available as part of our Frontend Masters video subscription.
Preview
CloseCourse Details
Published: August 19, 2024
Learning Paths
Learn Straight from the Experts Who Shape the Modern Web
Your Path to Senior Developer and Beyond
- 200+ In-depth courses
- 18 Learning Paths
- Industry Leading Experts
- Live Interactive Workshops
Table of Contents
Introduction
Section Duration: 25 minutes
- Maximiliano Firtman begins the course by sharing his web development journey and discussing the topics covered. The prerequisites for the course include basic programming experience and familiarity with object-oriented programming concepts. Experience writing HTML will also be helpful.
- Maximiliano explains that 76% of websites use PHP. While several websites use WordPress, the most popular content management system, companies like Meta, Microsoft, and Wikipedia still have PHP as part of their infrastructure. This presents a large opportunity for future PHP developers.
- Maximiliano shares a brief history of PHP, from its early beginnings in 1993 through the current version 8, which was released in 2020. This lesson also explains the PHP elephant mascot.
PHP Syntax
Section Duration: 1 hour, 11 minutes
- Maximiliano walks through some of the language features of PHP. Since PHP is an interpreted language, the source code is not compiled but shipped directly to the server.
- Maximiliano discusses the local and production requirements for PHP. Locally, a PHP interpreter is required. The interpreter can be installed individually or as part of a full LAMP stack, which includes Linux, Apache, MySQL, and PHP. In production environments, most shared hosting services support PHP or can be run in a cloud provider through a Docker image.
- Maximiliano creates a simple PHP file and demonstrates PHP tags. These tags contain the executed PHP code. Anything outside a PHP tag is considered string output. The closing tag can be omitted when a file only contains PHP code, and static string output is not wanted.
- Maximiliano demonstrates how to create variables in PHP. Variables are prefixed with a dollar sign and are dynamically typed, meaning you can change the type assigned to the variable at any time. The concept of "variable variables" is introduced. They allow the name of a variable to be stored in a variable and a value assigned with a double dollar sign.
- Maximiliano highlights the difference between single, double, and multi-line use cases when creating String values in PHP. Strings can be concatenated with the ".", not the "+" like in other languages. Strings using double quotes support variable interpolation.
- Maximiliano demonstrates how to create arrays in PHP. The array() constructor accepts the list of items to build the array. Square bracket notation is also supported. Keys can be provided to create an associative array like a dictionary or map. The global function count() will return the length of an array.
- Maximiliano demonstrates the while, do while, for, and foreach loops. Like other languages, curly braces contain the body of the loop. PHP has an alternative syntax to add a colon at the beginning of the loop and an identifier like endforeach to terminate the loop.
- Maximiliano introduces PHP functions. The function keyword is used in the declaration. Arguments are prefixed with a dollar sign like with variables and can have a type specified. This lesson also demonstrates the new named argument feature of PHP 8.
Building Web Applications with PHP
Section Duration: 33 minutes
- Maximiliano starts a development server from the terminal and specifies the host and port. The development server will serve static files like any other web server. PHP files are treated like HTML files, so any HTML content is rendered in the browser. Because of the ".php" extension, PHP code will be rendered on the server, and only the output will be returned to the browser.
- Maximiliano begins the Cryptomasters example by creating an index.php file and coding a basic HTML form. The form will take a US dollar value as input and convert it to an equivalent cryptocurrency value.
- Maximiliano introduces superglobal variables that give you access to GET and POST data sent from a form. There are other superglobal variables for accessing cookies, session, and other server-related data. In this lesson, the GET superglobal variable accesses the amount and crypto variables in the URL.
- Maximiliano demonstrates the difference between the GET and POST superglobal variables. POST data is sent from the form through the headers in the request rather than on the URL. The "issset" method is used to ensure the data is sent before it's used on the page.
- Maximiliano reviews a few additional superglobal variables. Server variables and the "phpinfo" function are also demonstrated and tips for debugging more complex data types like arrays are also shared.
Object Oriented Programming in PHP
Section Duration: 39 minutes
- Maximiliano introduces object-oriented programming concepts like classes, properties, methods, and constructors. A class is created with a public property and a public method. A constructor is used to set the public property when an instance of the class is created.
- Maximiliano demonstrates how to use the include method to import a class into another PHP file so the class can be instantiated. This lesson also discusses the difference between include, include_once, require, and require_once.
- Maximiliano uses the file_get_contents method to load data from an external API. After receiving the data, the JSON is decoded, and the converted currency value is returned from the class to the converter.php file.
Creating an API Endpoint
Section Duration: 16 minutes
- Maximiliano begins building an API endpoint for the Crypto Converter project. This endpoint can be used by an existing application or a client-side JavaScript application. Two new operators for handling null values and safe function calls are also introduced.
- Maximiliano completes the API endpoint example and demonstrates how to format and return JSON data. Headers for setting the Content-type and some CORS configuration are added. This lesson also provide some tips for avoiding sending any content prior to the headers being sent.
Databases, Testing & Debugging
Section Duration: 1 hour, 7 minutes
- Maximiliano introduces the Frontend Museum project which is a static HTML project that displays a list of museum exhibits from a data feed. The project will be refactored to use PHP to populate the list rather than JavaScript.
- Students are instructed to modify the markup inside the foreach loop so the information for each exhibit is dynamically generated from the data.
- Maximiliano begins refactoring the application to load data from a database rather than a static PHP file. SQLite is introduced and a DB class is created. The data in the SQLite DB can be viewed with a SQLite viewer extension.
- Maximiliano creates the database connection and writes an SQL statement to select everything from the Exhibits table in the database. The results are returned as an associative array which can be iterated through to maintain the same foreach loop functionality in the application.
- Maximiliano introduces spl_autoload_register, which registers any number of autoloaders, enabling for classes and interfaces to be automatically loaded if they are currently not defined.
- Maximiliano modularizes the application by creating header and footer include files for the page. This way the common headers and footers can be reused on each page of the application. The details page is created and the index page is refactored to include a list of exhibits.
- Maximiliano finishes the master-detail example for the Frontend Museum project. The GET superglobal variable accesses the index property passed in the URL. The index is used to display the details for the exhibit.
- Maximiliano shares some tips for handling errors in PHP applications. Try/Catch blocks can hand exceptions. The PHP server settings can be modified through the "ini_" functions to change the error reporting levels.
Wrapping Up
Section Duration: 4 minutes
- Maximiliano concludes the course with a summary of the topics covered and by sharing a few additional resources.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops