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.
Keep in touch
An experienced Web Developer with years of experience in design, programming and server administration.
© 2024 by Frank Perez.
PHP articles and tutorials ranging from new language features to using interesting packages.
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 a simple way to partition a set of data or return a single value. It is super powerful and worth spending time learning.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
As of PHP 7, you can now group your imported classes, functions, and constants from under the same namespace.
One of my new favorite additions to PHP 7, is the Null Coalesce 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.
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.
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.
Starting with PHP 7.0, it’s now possible to declare scalar type hints for your method arguments. Previously, we we’re able to use array and callable, but now with PHP 7+, we have much more control.