Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
PHP 8 Programming Tips, Tricks and Best Practices

You're reading from   PHP 8 Programming Tips, Tricks and Best Practices A practical guide to PHP 8 features, usage changes, and advanced programming techniques

Arrow left icon
Product type Paperback
Published in Aug 2021
Publisher Packt
ISBN-13 9781801071871
Length 528 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Doug Bierer Doug Bierer
Author Profile Icon Doug Bierer
Doug Bierer
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Preface 1. Section 1: PHP 8 Tips
2. Chapter 1: Introducing New PHP 8 OOP Features FREE CHAPTER 3. Chapter 2: Learning about PHP 8's Functional Additions 4. Chapter 3: Taking Advantage of Error-Handling Enhancements 5. Chapter 4: Making Direct C-Language Calls 6. Section 2: PHP 8 Tricks
7. Chapter 5: Discovering Potential OOP Backward-Compatibility Breaks 8. Chapter 6: Understanding PHP 8 Functional Differences 9. Chapter 7: Avoiding Traps When Using PHP 8 Extensions 10. Chapter 8: Learning about PHP 8's Deprecated or Removed Functionality 11. Section 3: PHP 8 Best Practices
12. Chapter 9: Mastering PHP 8 Best Practices 13. Chapter 10: Improving Performance 14. Chapter 11: Migrating Existing PHP Apps to PHP 8 15. Chapter 12: Creating PHP 8 Applications Using Asynchronous Programming 16. Other Books You May Enjoy

Incorporating match expressions into your program code

Among the many incredibly useful features introduced in PHP 8, match expressions definitely stand out. Match expressions are a more accurate shorthand syntax that can potentially replace the tired old switch statement that came directly from the C language. In this section, you will learn how to produce cleaner and more accurate program code by replacing switch statements with match expressions.

Match expression general syntax

Match expression syntax is much like that of an array, where the key is the item to match and the value is an expression. Here is the general syntax for match:

$result = match(<EXPRESSION>) {
    <ITEM> => <EXPRESSION>,
   [<ITEM> => <EXPRESSION>,]
    default => <DEFAULT EXPRESSION>
};

The expression must be a valid PHP expression. Examples of expressions could include any of the following:

  • A specific value (for example, "some text")
  • An operation (for example, $a + $b)
  • An anonymous function or class

The only limitation is that the expression has to be defined in a single line of code. Major differences between match and switch are summarized here:

Table 1.1 – Differences between match and switch

Table 1.1 – Differences between match and switch

Other than the differences noted, match and switch both allow case aggregation, as well as providing support for a default case.

switch and match examples

Here is a simple example that renders a currency symbol using switch:

// /repo/ch01/php7_switch.php
function get_symbol($iso) {
    switch ($iso) {
        case 'CNY' :
            $sym = '¥';
            break;
        case 'EUR' :
            $sym = '€';
            break;
        case 'EGP' :
        case 'GBP' :
            $sym = '£';
            break;
        case 'THB' :
            $sym = '฿';
            break;
        default :
            $sym = '$';
    }
    return $sym;
}
$test = ['CNY', 'EGP', 'EUR', 'GBP', 'THB', 'MXD'];
foreach ($test as $iso)
    echo 'The currency symbol for ' . $iso
         . ' is ' . get_symbol($iso) . "\n";

When this code is executed, you see the currency symbols for each of the International Organization for Standardization (ISO) currency codes in the $test array. The same result as that shown in the preceding code snippet can be obtained in PHP 8, using the following code:

// /repo/ch01/php8_switch.php
function get_symbol($iso) {
    return match ($iso) {
        'EGP','GBP' => '£',
        'CNY'       => '¥',
        'EUR'       => '€',
        'THB'       => '฿',
        default     => '$'
    };
}
$test = ['CNY', 'EGP', 'EUR', 'GBP', 'THB', 'MXD'];
foreach ($test as $iso)
    echo 'The currency symbol for ' . $iso
         . ' is ' . get_symbol($iso) . "\n";

Both examples produce an identical output, as illustrated here:

The currency symbol for CNY is ¥
The currency symbol for EGP is £
The currency symbol for EUR is €
The currency symbol for GBP is £
The currency symbol for THB is ฿
The currency symbol for MXD is $

As mentioned previously, both code examples produce a list of currency symbols for the list of ISO currency codes stored in the $test array.

Complex match example

Returning to our CAPTCHA project, assume that we wish to introduce distortion to make the CAPTCHA characters more difficult to read. To accomplish this goal, we introduce a number of strategy classes, each producing a different distortion, as summarized in this table:

Table 1.2 – CAPTCHA distortion strategy classes

Table 1.2 – CAPTCHA distortion strategy classes

After randomizing the list of strategies to be employed, we use a match expression to execute the results, as follows:

  1. First we define an autoloader, import the classes to be used, and list potential strategies to employ, as illustrated in the following code snippet:
    // /repo/ch01/php8_single_strategies.php
    // not all code is shown
    require_once __DIR__ . '/../src/Server/Autoload/Loader.php';
    $loader = new \Server\Autoload\Loader();
    use Php8\Image\SingleChar;
    use Php8\Image\Strategy\ {LineFill,DotFill,Shadow,RotateText};
    $strategies = ['rotate', 'line', 'line',
                   'dot', 'dot', 'shadow'];
  2. Next, we generate the CAPTCHA phrase, as follows:
    $phrase = strtoupper(bin2hex(random_bytes(NUM_BYTES)));
    $length = strlen($phrase);
  3. We then loop through each character in the CAPTCHA phrase and create a SingleChar instance. The initial call to writeFill() creates the white background canvas. We also need to call shuffle() to randomize the list of distortion strategies. The process is illustrated in the following code snippet:
    $images = [];
    for ($x = 0; $x < $length; $x++) {
        $char = new SingleChar($phrase[$x], FONT_FILE);
        $char->writeFill();
        shuffle($strategies);
  4. We then loop through the strategies and layer distortions upon the original image. This is where the match expression comes into play. Notice that one strategy needs additional lines of code. Because match can only support a single expression, we simply wrap the multiple lines of code into an anonymous function, as follows:
    foreach ($strategies as $item) {
        $func = match ($item) {    
            'rotate' => RotateText::writeText($char),
            'line' => LineFill::writeFill(
                $char, rand(1, 10)),
            'dot' => DotFill::writeFill($char, rand(10, 20)),
            'shadow' => function ($char) {
                $num = rand(1, 8);
                $r   = rand(0x70, 0xEF);
                $g   = rand(0x70, 0xEF);
                $b   = rand(0x70, 0xEF);
                return Shadow::writeText(
                    $char, $num, $r, $g, $b);},
            'default' => TRUE
        };
        if (is_callable($func)) $func($char);
    }
  5. All that remains to be done is to overlay the image with the actual CAPTCHA phrase by calling writeText() with no arguments. After that, we save the distorted image as a Portable Network Graphics (PNG) file for display, as illustrated in the following code snippet:
        $char->writeText();
        $fn = $x . '_' 
             . substr(basename(__FILE__), 0, -4) 
             . '.png';
        $char->save(IMG_DIR . '/' . $fn);
        $images[] = $fn;
    }
    include __DIR__ . '/captcha_simple.phtml';

Here is the result, running the preceding example from a browser that points to the Docker container associated with this book:

Figure 1.1 – Distorted CAPTCHA using match expression

Figure 1.1 – Distorted CAPTCHA using match expression

Next, we'll have a look at another really great feature: named arguments.

Tip

You can see the original proposal for match expressions here: https://wiki.php.net/rfc/match_expression_v2

You have been reading a chapter from
PHP 8 Programming Tips, Tricks and Best Practices
Published in: Aug 2021
Publisher: Packt
ISBN-13: 9781801071871
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image