Sunday, 9 February 2025

Exploring Enumerations in PHP: Workarounds and Native Support in PHP 8.1


Enumerations (enums) are a popular feature in many programming languages like Java, allowing developers to define a set of named values. However, until recently, PHP did not have native support for enums, which led developers to create various workarounds to mimic their behavior. This blog post explores different approaches to using enums in PHP, from traditional workarounds to the native support introduced in PHP 8.1.

The Challenge: Enums in PHP Before 8.1

Before PHP 8.1, developers who were used to the enum functionality from languages like Java faced challenges in PHP. Enums are beneficial because they allow for predefined, immutable sets of values that can be used for things like status codes, types, or other categories. However, PHP’s lack of native enum support led to several issues:

Read more »

Labels:

Sunday, 8 June 2025

How to Catch a PHP Fatal (E_ERROR) Error

In PHP, fatal errors (such as E_ERROR) can cause a script to terminate immediately, making it challenging to capture and handle these errors. While PHP’s set_error_handler() function allows catching many types of errors, it doesn’t work for fatal errors. In this post, we’ll explore different ways to handle fatal errors effectively, especially in older versions of PHP, and how PHP 7+ offers a more structured approach to error handling.

Problem with Catching Fatal Errors

Fatal errors in PHP, like calling a non-existent function or running out of memory, cannot be caught by set_error_handler() because these errors cause the script to terminate before reaching the handler. For example, this code will not catch a fatal error:

Read more »

Labels:

Wednesday, 2 October 2024

Detecting Request Type in PHP (GET, POST, PUT, or DELETE)

When building web applications, it’s important to handle different types of HTTP requests—such as GET, POST, PUT, and DELETE. These methods are used for different operations: retrieving data, submitting forms, updating records, or deleting them. In PHP, detecting the request type is a common task, especially when creating RESTful APIs or handling complex form submissions.

Here’s a post detailing how to detect the request type in PHP and how to handle it in different ways.

1. Using $_SERVER['REQUEST_METHOD']

The most straightforward way to detect the request method in PHP is by using the $_SERVER superglobal. This variable contains server and execution environment information, including the request method.

Read more »

Labels: , , ,

Wednesday, 25 September 2024

Automatically Updating Year in PHP for a Copyright Notice

When managing websites, it’s common to add a copyright notice in the footer. However, hardcoding the year can make your site look outdated if it isn’t updated annually. Luckily, with PHP, you can ensure the year updates dynamically without having to adjust the code each year.

Here’s how you can automatically update the year in your copyright notice using PHP.

Getting the Current Year in PHP

In PHP, the easiest way to get the current year is by using the built-in date() function. The date() function takes formatting parameters, and for the year, you simply pass 'Y' (for the four-digit year format).

Read more »

Labels:

Wednesday, 4 June 2025

Secure Hashing and Salt for PHP Passwords: A Modern Approach


When considering password protection in PHP, it’s crucial to employ up-to-date techniques to ensure both security and performance. The original question was raised in 2008, but PHP has evolved since then, providing built-in functions for safe password handling. Here’s a modern take on the subject, utilizing best practices and avoiding deprecated approaches like MD5 or SHA1.

Read more »

Labels:

Tuesday, 29 April 2025

How to Add HTML and CSS to a PDF in PHP

If you’re looking to convert an HTML page with CSS into a PDF document, there are several tools and libraries available. Depending on your project’s requirements, you may choose a library that fits your needs in terms of speed, compatibility with CSS, or ease of integration into your PHP application.

This blog post will cover some popular tools for converting HTML and CSS into PDF using PHP, including solutions like wkhtmltopdf, mPDF, and more.

Read more »

Labels:

Saturday, 2 November 2024

UTF-8 All the Way Through: Ensuring Full UTF-8 Support in Your Web Application

 Setting up full UTF-8 support in a web application is essential for handling multilingual content reliably. This guide covers all the key areas—MySQL, PHP, Apache, and HTML—to help you achieve a seamless UTF-8 experience across your stack. Here’s a checklist to ensure UTF-8 is correctly set up at every layer of your web application.

1. Configuring MySQL for UTF-8

To support a full range of Unicode characters, including emojis, configure MySQL to use utf8mb4 rather than utf8, as MySQL’s utf8 only supports up to three bytes (limited to basic multilingual characters).

  • Database and Table Configuration:

    CREATE DATABASE your_database CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
    ALTER TABLE your_table CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
    
  • Column Configuration:
    Set each text column to utf8mb4 to ensure character data is stored correctly:

    ALTER TABLE your_table MODIFY column_name VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
    
  • Connection Settings:
    Set the character set for connections to utf8mb4. This way, data exchanged between MySQL and your application retains its UTF-8 encoding. Use the following configuration depending on your PHP extension:

Read more »

Labels:

Saturday, 22 March 2025

How to Properly Sanitize User Input in PHP

Sanitizing user input is critical to protecting web applications from threats such as SQL injection and cross-site scripting (XSS). However, it is important to understand that there is no “catchall” function for all types of input sanitization in PHP, as different contexts require different approaches.

This blog post explores various methods for sanitizing input and securing your PHP application, covering SQL injection, XSS prevention, and safely handling user input in different contexts.

Read more »

Labels: