php array; insert new item at specific index

The php function array_splice can be used to insert new items. At specific places.

<?php
$breadcrumbs = [
    'home',
    'year',
    'month',
    'day',
];
$new_crumb = [
    'category'
];
array_splice($breadcrumbs, 1, 0, $new_crumb);

The result is that category is inserted at the second place.

array (
    0 => 'home',
    1 => 'category',
    2 => 'year',
    3 => 'month',
    4 => 'day',
);

Running php from command line

This is one of those “I know this is possible, but don’t know how”.

php -r 'echo "hello world";'

Only useful for simple onelines. You’re very likely to be better of putting code in a php file and run that script like:

php ./helloworld.php

PHP interface type hinting

In interfaces you can force the return type

interface InterfaceTest {
    function GetSingleEntity(): Entity;
    function GetEntitySet() : Entity[];
}

GetEntitySet should return a array containing Entities. But this kind of syntax is not allowed.
You could just change it to only return an array, but the forcing of the class is the most powerful.
The best is to create a wrapper class for the entities, best would be to use an Iterator.

Behat functional tests

During WordCamp Rotterdam I worked on a small bug in WP-CLI. A bug I found roughly six months ago.
The bug itself was a one line fix.
I was asked to create functional tests using behat which I didn’t even heard off.

So after checking the handbook and with help of a few others. It resulted in this pull request.
And now we wait.

The order of arguments in an if statement

Consider this dummy snippet:

if ( true === $skip || heavy_check() ){
    continue;
}

The $skip is checked first. It is very quick as it is just a simple variable check. and since this is an ‘or’ statement. As soon as $skip is true it won’t bother with the heavy_check.

Something I always suspected but today I tested this and confirmed it.