dseguy, French
@dseguy@phpc.social avatar

TIL file_put_contents() accepts an array as second argument, and will do an implode("", $array) on it when writing.

Who is using this feature?

cc /@Girgias

https://php-tips.readthedocs.io/en/latest/tips/file_put_array.html

amarok,
@amarok@mastodonczech.cz avatar

@dseguy
I use file_put_contents only very rarely with just 1 string argument. I usually prefer fwrite.
Passing an array to f_p_c to have it joined at put into a text file, hmmm, I wonder in what cases this might be useful 🤔 Not really a friend of those combined behavior, it's hard to read because you must have a deeper knowledge of those functions and types of arguments... Using implode separately would be my prefered way to go.

dseguy,
@dseguy@phpc.social avatar

@amarok So far, it looks like an old style ( 3/4) feature, and it is pretty much forgotten.

I use file_put_contents() by default, and when performances are important, move to fwrite(). And that's rare.

jaapio,
@jaapio@phpc.social avatar

@dseguy why... 😳

wyri,
@wyri@haxim.us avatar

@jaapio @dseguy does it accept arrays or iterable's?

Girgias,
@Girgias@phpc.social avatar

@wyri @jaapio @dseguy Only arrays, in peak PHP fashion

wyri,
@wyri@haxim.us avatar

@Girgias @jaapio @dseguy that's a missed opportunity 😕

Girgias,
@Girgias@phpc.social avatar

@wyri @jaapio @dseguy I'm not sure if it should accept an array in the first place, I only learnt of this yesterday. So I wonder how many people know about this "feature".

wyri,
@wyri@haxim.us avatar

@Girgias @jaapio @dseguy It's in the docs, so in theory enough, but still why an array. An iterable I would understand somewhat if you'd stream data using generators to keep memory low.

jaapio,
@jaapio@phpc.social avatar

@wyri because the logic of php internals is not what we think of 😋@Girgias @dseguy

dseguy,
@dseguy@phpc.social avatar

@jaapio @wyri @Girgias I'd say it looks like an old feature-hidden-in-the-argument style, pretty much PHP 3/4, and never removed.

I started monitoring this usage, but so far, nothing. (that's good?)

Also, you can use a LOT of types with that argument, and often, just nothing happens. Like closure, iterators..

Girgias,
@Girgias@phpc.social avatar

@jaapio @wyri @dseguy Actually... this somewhat has a "good" reason. And this problem exists in a couple of places when objects are just interpreted as a property array.

But string|array as a union type is usually incompatible with objects because how do you handle an object that is both Stringable and Traversable? Do you think of it as a string? Do you think of it as an array?

http_build_query() is basically in this situation and causes pain and PTSD https://github.com/php/php-src/pull/10235

  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • rosin
  • osvaldo12
  • thenastyranch
  • DreamBathrooms
  • khanakhh
  • magazineikmin
  • InstantRegret
  • Youngstown
  • slotface
  • mdbf
  • love
  • kavyap
  • GTA5RPClips
  • everett
  • Leos
  • cubers
  • ethstaker
  • Durango
  • ngwrru68w68
  • tacticalgear
  • modclub
  • cisconetworking
  • provamag3
  • anitta
  • normalnudes
  • tester
  • megavids
  • JUstTest
  • All magazines