PHP
18 articles
FizzBuzz in PHP: A Fresh Approach
FizzBuzz is a very popular programming question that tests your logic to see if you can build a simple program.
PHP's array_reduce is not only for outputting single values
PHP's array_reduce is a simple way to partition a set of data or return a single value. It is super powerful and worth spending time learning.
Support for keys in list(), or its new shorthand syntax [] in PHP
Now as of PHP 7.1, you can define the keys of your array that will be parsed when destructuring your arrays. Prior to PHP 7.1, you could only use arrays with numeric indexes. Now with this new addition, our lives just got easier.
Type Hinting With The Iterable pseudo-type In PHP
As of PHP 7.1, you can now type hint your method/function arguments with the keyword iterable for handling arrays or even objects that implement the Traversable interface.
Type Hinting Callable Functions in PHP
As of PHP 5.4, you can type hint your method arguments with the callable keyword allowing you to enforce the type of data that is passed via your arguments.
Setting Visibility for Your Class Constants in PHP
Now in PHP 7.1+, you can set different visibility modifiers for each of your class constants. The available visibility modifiers consist of public, protected, and private.
Using Anonymous Classes in PHP
As of PHP 7, you can now create quick throwaway objects for use within your projects. This can be especially useful for your automated tests, for instance, with allowing you to create quick implementations of your interfaces.
Symmetric Array Destructuring in PHP
As of PHP 7.1, you can now use the shorthand array syntax to destructure your arrays for assignment. Previously you would have had to use a function like list, but now you can use the simple new array shorthand syntax.
Using PHP's array_map to format your arrays without loops
So let's face it, loops are a bit boring. So how can we mix it up? Let's assume we have a case where we have a CSV file that we want to quickly parse.
SOLID Principles in PHP
The 5 basic principles for Object-Oriented Design, SOLID, were first created in an effort to improve maintainability in our code bases. SOLID is a mnemonic acronym that stands for each of the following principles: Single Responsibility, Open-Closed, Liskov Substitution, Interface Segregation, and Dependency Inversion.
Filtering Arrays Without Using Loops in PHP
PHP has a built-in function called array_filter that allows you to filter through your arrays without the need for a loop. Personally, this approach feels much cleaner to me and simpler to comprehend.
Void Return Types in PHP
As of PHP 7.1, we can now use void return types within our methods. This is useful for cases where you have methods that are just setting or processing data without the need of returning any values.
Type Hinting with Nullable Types in PHP
As of PHP 7.1, you can now set your type declarations as nullable by simply prefixing them with a question mark ?. In doing so a null value can be passed in as a parameter or returned as a value for your methods.
PHP Group Multiple use Declarations
As of PHP 7, you can now group your imported classes, functions, and constants from under the same namespace.
PHP Null Coalescing Operator
One of my new favorite additions to PHP 7, is the Null Coalescing Operator. It cleans up your code by removing a tedious step of checking if some value is isset() and not NULL and returning it or if not setting a default.
PHP Spaceship Operator
One of the new features to hit PHP 7 is the Spaceship Operator. This new trick helps improve the way you'd compare 2 expressions. In short, the comparison returns 1 of 3 values (-1, 0, or 1) depending on the result of the comparison.
Return Type Declarations in PHP
PHP 7 now makes it possible to declare return types for your methods. This allows you better control over the data that will be returned from each method in your application.
Scalar Type Hints in PHP
Starting with PHP 7.0, it's now possible to declare scalar type hints for your method arguments. Previously, we were able to use array and callable, but now with PHP 7+, we have much more control.