Enumeration (Enum) is a data type that you can use to store a value from a custom set. They’re perfect for representing the kind of options you might want to show in a drop-down list.
PHP 8.1 introduced support for enumerations. These enums are easy to use, but they provide additional features borrowed from object-oriented programming.
What does arithmetic do
Enumerations are useful when you want to work with a fixed set of related values. For example, you can use an enum to represent the suit in a pack of cards or the type of vehicle.
PHP’s enums are quite complex, with some features borrowed from classes. You can use them in conjunction with type hinting to write more robust, predictable code.
Basic enums
Enums typically store scalar values. These correspond to C’s primitive data types, although PHP’s enums can store string values as well as integers. However, by default, PHP’s enumerated values do not have a scalar equivalent.
Note how PHP reuses the case keyword which you’ll also see in switch statements. The values that this enum represents are just the names of the cases.
Calculation methods
PHP enums can also contain methods, which makes them like cut-down classes. This reflects PHP’s position somewhere between a procedural language and an object-oriented language.
For example, you can add this simple method to a Month enum to get the number of days from a specific value.
Note that you can use $this to refer to a specific enum value, just as you use $this to refer to a specific object. You can also use the standard equality operator == to compare enum values.
Enums also support static methods. You probably won’t have much use for these, but they can be helpful if you need to write something that acts like a constructor, for example.
Better calculation
Enumeration is a common feature of many languages, including PHP’s ancestor, C. They are one of the key features of the 8.1 release, bringing the language up-to-speed with many of its competitors.
PHP already has many features that make it suitable for web development. As a language matures, it borrows from others and innovates on core features.
If you gain a basic understanding of how to build a website using PHP, you will have knowledge of file inclusion and producing output – the basics of customizing a web page. Maybe you’re wondering where to start, or what next steps to take when building more functionality into your site.
In this tutorial, I present a simple site structure that allows for direct updates. It also showcases some useful PHP functions to add value.
The website you will be building in PHP
The sample website—available in a GitHub repository—is a simple site about birds, with attributes such as their feathers. It has some sections, some information pages and a home page. The final site isn’t as important as the techniques you’ll read about, and the ideas they may inspire.
Each page corresponds to a PHP script in the site directory. It introduces some redundancy, but it keeps things simple. When setting up your web server, make sure you point your DOCROOT to the site directory.
How the site works
Bootstrap is a front-end framework for building websites. It has built-in styling and JavaScript functionality, which covers most common web dev needs. This is a great way to get a site up and running quickly before you spend time fine-tuning the design.
You can install and host the Bootstrap files on your own server, but for maximum speed, you can reference them from a CDN.
Basic templating
Start with site/index.php. This is the file that represents the home page of the site. Depending on how your web server is set up, you should be able to access it at http://yoursite.example.org/ or, failing that, http://yoursite.example.org/. org/index.php.
Note that both are included at the beginning of this file: funcs.php and TPL_DIR.”/home.php”. The funcs.php file defines that TPL_DIR constant as the full absolute path to the tpl directory at the top level of the site.
Take a look at tpl/home.php. This is the outermost structure of an html document: it consists only of a doctype and an html element. Within the HTML element, it includes two for templates representing the head and body.