Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Leading Nor Trailing

By Lincoln Russell

<?php
  
 $dst = 'a=b';
 // $dst has no = leading or trailing or missing
 if (!in_array(strpos($dst, '='), [0, false, strlen($dst) - 1])) {
    print $dst; 
 }


if (str_contains($dst, '=') && !str_starts_with($dst, '=')  && !str_ends_with($dst, '=')) {
    print $dst;
}

Occasionally, it’s a drawback to be fluent in the old ways. Wrote this too-clever PHP code and died a little inside:

if (!in_array(strpos($dst, '='), [0, false, strlen($dst) - 1]))

Then a little voice inside me said “didn’t PHP 8 add some string functions…”

It did! Thus:

if (str_contains($dst, '=') && !str_starts_with($dst, '=') && !str_ends_with($dst, '='))

Yay for code that doesn’t require a PhD in wtf were you thinking 😂.

See Also

PHP Features

Last updated: 11 October 2026