Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

TutsPlus.com:
Understand Arrays in PHP
Sep 05, 2018 @ 10:48:04

In a recently posted tutorial on the TutsPlus.com site, author Sajal Soni gets back to the basics and covers the use of arrays in PHP.

In this post, you'll learn the basics of arrays in PHP. You'll learn how to create an array and how to use associative and multidimensional arrays, and you'll see lots of examples of arrays in action.

The tutorial then starts with a brief introduction to what arrays are and how they're defined. From there it shows:

  • how to access array elements
  • types of arrays
  • some useful array-related functions

Each item (and function example) comes with a code snippet to show it in use as well as a brief summary of what's happening.

tagged: array introduction basics tutorial function types

Link: https://code.tutsplus.com/tutorials/understand-arrays-in-php--cms-31738

Laravel News:
New Outer Array Functions Coming to PHP 7.3
Jul 18, 2018 @ 12:47:44

On the Laravel News site they've shown a spotlight on a new feature that's coming with the next major release of the PHP language (v7.3): outer array functions.

PHP 7.3 introduces two new array functions for working with the “outer” keys of an array. The RFC proposal included four new functions for both keys and values, but only the array key functions were accepted: array_key_first() and array_key_last().

[...] Although the outer array value functions were declined, at least new functions will be available for getting the outer keys of an array.

They then provide some code examples of this new functionality, showing how use these new functions to extract values. It also includes examples of the two functions - array_value_first and array_value_last - that were rejected from the RFC when voting happened.

tagged: array outer function php73 feature key value

Link: https://laravel-news.com/outer-array-functions-php-7-3

Laravel News:
PHP 7.3: Trailing Commas in Function Calls
Jun 15, 2018 @ 10:46:51

In a post in the Laravel News site, they quickly cover one of the many changes coming with the next jump in versions of the PHP language (v7.3): trailing commas in function calls.

Well PHP 7.3 won’t have arrow functions (that would be dreamy). However, trailing commas in function calls is an excellent addition coming to PHP 7.3.

In PHP 7.3, trailing commas in function calls will be valid syntax. That is to say, you can use trailing commas when calling functions, but not defining them.

They then include two places where these trailing commas could be useful: in the use of variadic functions and in PHP array definitions. The remainder of the post shows the concept of "trailing commas" in other languages including Javascript, Python, and Haskell. The first alpha release of PHP 7.3 has been released if you'd like to try this out with your own code.

tagged: trailing comma function call php73 feature alpha tutorial

Link: https://laravel-news.com/php-trailing-commas-functions

Exakat Blog:
The 100 PHP functions that you have to know
May 31, 2018 @ 09:54:33

On the Exakat blog they have a new post with the results from their evaluation of a wide range of PHP applications showing the Top 100 PHP functions in use.

Here is the top 100 PHP functions : it is the list of the most often used PHP native functions.

The functions are named, and ranked from 1 to 100. The other 4500 functions are not ranked now. The frequency column represents how often this function is used across PHP code : the reference corpus is a list of 1900 PHP open source projects. They were audited with Exakat static analysis engine, version 1.2.5. The average is the number of time a function is called within one project. Some function are used in isolation, and others are a staple.

The top 5 on the list are:

Each item on the list comes with "frequency" and "average" scores (that mostly relate directly). Check out the post for the full list and a list of some insights they worked up as they generated the listing.

tagged: top100 list function language ranking usage

Link: https://www.exakat.io/top-100-php-functions/

Exakat Blog:
How many parameters is too many?
May 01, 2018 @ 11:55:47

In a new post to the Exakat blog they try to answer the question "how many parameters is too many" when it comes to the structure of the methods and functions in your application.

Now, that is a classic question, that is often a minefield for anyone writing an increasing long list of argument in a method, or simply trying to set up auditing tools.

Obviously, the answer is not immediate. Parameters may be needed, but on the other hands, currying functions allows to reduce the amount of parameter to one for every function. In between, probably exists a reasonable level that is a golden rule, and also very elusive. So, we decided to check the current practice in PHP code.

They started the research with some of PHP's own native functions that took in specific arguments, ignoring those that took an arbitrary number. Next they made a survey of 1900 open source projects to determine the common practice for parameters by function. The results showed that methods without at least one parameter were "less useful" and that a seemingly reasonable amount of parameters is 5. The post finishes with a spotlight of two they found during their research that had the most parameters: a generated class for database interaction and a dependency injection class.

tagged: parameters count statistics userland native function method results

Link: https://www.exakat.io/how-many-parameters-is-too-many/

Tomas Vortuba:
How I Got into Static Trap and Made Fool of Myself
Apr 30, 2018 @ 10:38:38

Tomas Vortuba has a new post to his site sharing some of his experiences with static function analysis (and fixing) and the trap he got himself into when evaluating the current state of his code. This is a continuation of his series covering the use of the SymfonyCodingStandard package.

PHP story with code examples, copy-cat killers, just a little bit of static, consistency, sniffs and way to prevent all that ever happening ever again.

In his post he uses an example "fixer" for testing DocBlock comments to see if they provide much useful information over what the PHP itself provides. He dives "under the surface" and looks at the steps it takes to accomplish this. He then gets to the "trap" part, showing the code behind the fixer and how depending on 3rd party packages and their parsing lead to code mistakenly being "fixed". He finishes up the post with a look at a few solutions that could help make these "traps" easier to fix including dependency injection and preventing the issue specifically with static functions.

tagged: static function analysis fixer codingstandard tutorial

Link: https://www.tomasvotruba.cz/blog/2018/04/26/how-i-got-into-static-trap-and-made-fool-of-myself/

Pineco.de:
Little Snippets to Keep Your Code Cleaner
Mar 01, 2018 @ 09:45:25

The Pineco.de blog has a post sharing some little snippets of code that can help to keep things cleaner and perform some common operations.

Sometimes it’s harder to keep your code clean and readable than to implement some architecture in your application. We collected some snippets that may help you to refactor your code.

Their list includes code to help with:

  • array casting
  • type checking
  • removing unnecessary "if" statements

They also have several others for different languages on the snippets page of their site for Javascript, Laravel, WordPress and even an .htaccess configuration.

tagged: cleaner code snippet function array typecheck refactor tutorial

Link: https://pineco.de/little-snippets-keep-code-cleaner/

Rob Allen:
Getting started with Serverless PHP
Aug 24, 2017 @ 09:19:19

In a new post to his site Rob Allen talks about getting up and running with "serverless PHP" using the Apache OpenWhisk cloud platform that executes as events are fired rather than sitting as an entire application.

I've been interested in Apache OpenWhisk for a little while now and recently submitted a new feature to add PHP support to the project. As OpenWhisk is a serverless environment, most users do not run their own copy and instead use a commercial provider with IBMs Bluemix available now along with Adobes I/O Runtime and RedHat coming soon. As a result, my contribution, isn't practically useful until it's in production with a provider.

Fortunately, and remarkably quickly, IBM have added support for PHP to the Bluemix "IBM Cloud Functions" platform, so now we can use PHP to develop serverless applications and deploy them into the wild! This is a rebranding, so you'll see this referred to as "Bluemix OpenWhisk" around the web too.

He walks you through the setup of an account on the bluemix.net service and how to install OpenWhisk locally rather than the IBM "Bluemix CLI" tool. He then shares the code to create a simple "Hello World" function in PHP (7.1) and push it up to your account. He then executes the action from the command line, returning his JSON formatted greeting. Next he shows how to set this function up as a web action and give it a URL for the outside world to access. He finishes the post with some examples of calling the URL and how it would receive parameters via a POST or a GET.

tagged: serverless development ibm bluemix function web cli tutorial

Link: https://akrabat.com/getting-started-with-serverless-php/

Paragon Initiative:
Libsodium Quick Reference: Similarly-Named Functions and Their Use-Cases
Jun 13, 2017 @ 12:17:55

In a new post to the Paragon Initiative blog Scott Arciszewski has shared a helpful reference for those that might be a little confused by the naming and functions involved in the use of libsodium in PHP. In this quick reference he lists out each of the functions, what type they are and an example use case.

Last Friday at Day Camp 4 Developers, I presented a talk titled Cooking with Sodium in PHP 7.2, which was largely live-demoing the various cryptography features provided by libsodium. One of the questions I was asked by attendees was about knowing which feature to use to solve specific problems. This is the sort of problem that I suspect many people run into, so here's a quick reference table followed by a detailed explanation.

In the table below, all encryption modes utilize authenticated encryption.

Following the quick reference table he then gets into more detail on each of the functions, describing in depth what they're for and examples of how to most effectively use them.

tagged: quickreference libsodium function guide usecase detail

Link: https://paragonie.com/blog/2017/06/libsodium-quick-reference-quick-comparison-similar-functions-and-which-one-use

Sebastian De Deyne:
The List Function & Practical Uses of Array Destructuring in PHP
May 15, 2017 @ 10:26:37

Sebastian De Deyne has written up a post to his site spotlighting PHP's list function and showing how it can be used for "array destructuring" and how recent changes in PHP 7.1.x make it more useful.

PHP 7.1 introduced a new syntax for the list() function. I've never really seen too much list() calls in the wild, but it enables you to write some pretty neat stuff.

This post is a primer of list() and it's PHP 7.1 short notation, and an overview of some use cases I've been applying them to.

He starts with a basic introduction to the list function and how it assigns out variables based on an array. He then shows examples of the updates that came with PHP 7.1, allowing you to specify the key from an array to more selectively extract only the value you want. Three "exhibits" are then provided, showing actual use cases for this functionality: basicunpacking examples, creating tuples and handling multiple return values.

tagged: list function use array destructuring php71 functionality tutorial tuple returnvalue

Link: https://sebastiandedeyne.com/posts/2017/the-list-function-and-practical-uses-of-array-destructuring-in-php