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

Leonid Mamchenkov:
Avoid complex arrays in PHP
Nov 30, 2018 @ 16:44:31

In a new post to his site Leonid Mamchenkov talks about complex arrays in PHP and links to two articles offering different opinions on their use.

Now that PHP 7+ sorted out a whole bunch of problems with type-hinting of parameters, return values, variables and properties, we turn our attention to somewhat deeper issues.

Array is a native citizen in PHP. Arrays are very convenient and are widely used. However, if you stop and think about the times where you had to figure out somebody else’s code, I’m pretty sure complex arrays will come to mind at some point.

The first article takes the positive approach, showing you how to create better handling for complex arrays in your applications. The second takes the opposite approach, showing how to avoid complex data structures in your code, opting for classes and objects instead. He notes that he thinks both articles have their good and bad points and references another blog post of his that provides yet another way to solve the need for complex data structures.

tagged: array complex datastructure tutorial opinion avoid

Link: http://mamchenkov.net/wordpress/2018/11/27/avoid-complex-arrays-in-php/

Laravel News:
Unwrapping array_wrap()
Nov 30, 2018 @ 15:34:32

On the Laravel News site they have a new post that "unwraps" the array_wrap helper that makes it easier to normalize values into arrays and removes the need for manually checking if a variable is an array or set before using it like one.

Laravel has a wrap method and array_wrap() helper to normalize values into an array. Raul @rcubitto shared this nice tip about it on Twitter and before seeing his Tweet I wasn’t aware of this method.

They include an example of the helper function in use and share some of the responses to his tweet including questions of how it's different than casting to an array. They answer this by talking about more complex values and how the helper is useful for null values and creating consistency in your application.

tagged: laravel helper function array arraywrap tutorial

Link: https://laravel-news.com/array_wrap

CodeWall:
5 Ways To Loop Through An Array In PHP
Sep 25, 2018 @ 16:04:25

On the CodeWall site Dan Englishby walks you through some of the basic functionality in the PHP language for working with arrays. In it he shows how to loop through an array using multiple tools including the usual control structures and others less widely used.

PHP, just like most other programming languages has multiple ways to loop through arrays. The most popular ways to do it usually is with a while, for and foreach operator, but, believe it or not, there more ways to do it with PHP. In this article I will walk-through each possibility for reading arrays whilst looping.

He breaks the article up into sections, one for each of the methods:

  • the white loop
  • the for loop
  • the foreach loop
  • the do/while loop
  • using the ArrayIterator

Each item in the list comes with a summary of how it works and some code showing it in action.

tagged: introduction tutorial language loop array top5 example

Link: https://www.codewall.co.uk/5-ways-to-loop-through-array-php/

TutsPlus.com:
Understand Arrays in PHP
Sep 05, 2018 @ 15: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

Larry Garfield:
PHP: Never type hint on arrays
Jul 30, 2018 @ 17:05:48

In a new post Larry Garfield makes an interesting suggestion related to the use of arrays in PHP. He suggests that you should never type hint arrays in your method definitions.

Let's be controversial: In modern PHP, you should never type-hint an array. Before you start throwing tomatoes, hear me out.

PHP allows you to specify the type of a function/method parameter or return value. These return values can be any legal PHP type. [...] PHP has a data type that it calls array, although it's not really an array as any other language would define it. [...] And you should almost never use array as a type hint. Why? Because there's always a better, more generic option.

He starts off talking about the use case where arrays are used as a "single complex value" and how,. more often than not, a class is actually a better option. He then covers the other main use of arrays: as an ordered sequence of values. To replace this he recommends a more structured collection that can apply some logic to its contents. With these other options out of the way, he then talks about what arrays are actually useful for and some other potential typehints to allow arrays and other potential inputs. He ends the post talking about array operations included in PHP and how, with a minimal amount of effort, they could be reproduced with simple methods for use on actual collection instances instead.

tagged: typehint array options class collection opinion

Link: https://steemit.com/php/@crell/php-never-type-hint-on-arrays

Laravel News:
New Outer Array Functions Coming to PHP 7.3
Jul 18, 2018 @ 17: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

Larry Garfield:
PHP: Use associative arrays basically never
Jul 02, 2018 @ 15:50:59

In a new post Larry Garfield suggests and interesting approach to arrays in PHP: stop using associative arrays (or at least "basically never").

The other day I was working on some sample code to test out an idea that involved an object with an internal nested array. This is a pretty common pattern in PHP: You have some simple one-off internal data structure so you make an informal struct using PHP associative arrays. Maybe you document it in a docblock, or maybe you're a lazy jerk and you don't. (Fight me!) But really, who bothers with defining a class for something that simple?

But that got me wondering, is that common pattern really, you know, good? Are objects actually more expensive or harder to work with than arrays? Or, more to the point, is that true today on PHP 7 given all the optimizations that have happened over the years compared with the bad old days of PHP 4?

So like any good scientist I decided to test it: What I found will shock you!

He starts by describing his test environment (a local environment, not a cloud one) and the code for his baseline tests. The code generates an array of one million items where each item is an associative array of an integer/string combo. He wants to see what kind of memory consumption is involved in the creation and processing of this data set via sorting. His second test evaluated the serialization size (again, code provided) again checking the memory consumption. He shares the results of these tests and then moves on to similar tests on:

  • stdClass instances
  • objects with public properties
  • objects with private properties
  • anonymous classes

The post ends with a summary showing the results of all tests side-by-side with some interesting results (but you'll have to check out the post for yourself if you want to see those).

tagged: associative array never benchmark object class anonymous results statistics

Link: https://steemit.com/php/@crell/php-use-associative-arrays-basically-never

Frank de Jonge:
Array destructuring in PHP
May 01, 2018 @ 14:48:15

Frank de Jonge has a post to his site sharing some helpful hints around the use of arrays in PHP. In this quick post he covers some of the array "superpowers", list assignments and nested destructuring.

One of the things I like the most about JavaScript, and PHP also to some extent, is how flexible and expressive they CAN be (but not always are). I also believe that JavaScript, PHP, and Python have a number of features that make them a good fit for serving the forefront of the web.

[...] The most versatile type of PHP, to me, is the array. The array can be used to act like many different classic data types. You can use them as a List, a Set (although that requires some specific handling), a HashMap, just to name a few.

He starts with the "superpowers" that arrays gained in PHP 7.1 (the square bracket syntax for destructuring) and some code examples showing it in action. He then moves on to show how this update works for list assignment. He finishes up the list with a look at nested destructuring and referencing values from subarrays with this same square bracket syntax.

tagged: array destructure language square bracket php71 tutorial

Link: https://blog.frankdejonge.nl/array-destructuring-in-php/

Pineco.de:
Little Snippets to Keep Your Code Cleaner
Mar 01, 2018 @ 15: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/

Larry Garfield:
Short and safe array iteration
Oct 26, 2017 @ 15:41:19

Larry Garfield has a new post to his site sharing a method for short and safe array iteration based on a "neat trick" he picked up reading a mailing list.

PHP's largely loose, dynamic typing has plenty of both pros and cons. One con in particular is that you don't always know for sure if a value you're trying to use has been set yet, or is non-null. PHP will dutifully whine at you if you try to use a null value, sometimes fatally. (Yet another reason to structure your code to avoid nulls, period.)

One place this comes up in particular is in foreach() loops, especially when working with nested array structures. (PHP lacks a struct type, but makes anonymous hash maps so easy that they get used as the uber data type, for better or worse.)

He gives an example of looping through a dataset with a foreach where the array index reference is used to reference the source array. While you could always wrap the loop in an if statement to check first, he has another interesting method to do the same thing. With the help of the null-coalesce operator (??) in PHP 7, you can essentially say: "if the array index referenced is null/does not exist, use an empty set". Check out the rest of the post for code examples putting this method to use.

tagged: array iteration nullcoalesce operator array null tutorial

Link: https://www.garfieldtech.com/blog/short-array-iteration


Trending Topics: