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

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/

Sergey Zhuk:
Building ReactPHP Memached Client: Making Requests And Handling Responses
Oct 26, 2017 @ 16:37:03

Sergey Zhuk has kicked off a series of posts to his site showing how to create a ReactPHP memcache client that can work as a streaming client for your PHP application rather than single get/set requests.

Before writing any code we should think about our future client’s API: how we are going to use it [and] what methods it is going to have.

The client is going to be used in ReactPHP asynchronous ecosystem, so I’m going to provide a promise-based interface for it (when methods return promises). Also, we are building a streaming client. Under the hood, we will open a socket connection and use it as a stream. The client itself will be a wrapper on this binary stream communication. That means that it is our job to manually parse Memcached protocol to write and read data with sockets. So, having all of this in mind, let’s start.

He then starts in on the development of the base for the client including the factor class that will create the client (connector) with the provided Loop instance. He includes an example of this in use to create the client and point it to a local memcache server. Next he creates the client class that will use the stream to send requests and a parser to work with the responses and resolve actions that need to be taken based on their contents.

tagged: reactphp tutorial memcache client stream loop request response

Link: http://seregazhuk.github.io/2017/10/09/memcached-reactphp-p1/

Freek Van der Herten:
Making overloaded functions readable
Jan 10, 2017 @ 18:18:19

Freek Van der Herten has a new post to his site sharing some of his ideas around making overloaded functions more readable, functions that can take in variable types of parameters (ex: both a string or an array) and can handle them correctly.

Sometimes you might allow a function to accept multiple data types. I don’t know for certain if it’s the correct term but for the remainder of this post I’m going to call such a function overloaded. In this post I’d like to show you a little trick to make overloaded functions more readable.

He starts off with an illustration from the Laravel framework of a "session" helper method that, in turn, calls "put" and checks for an array versus string input with some interesting logic behind it. He gives another example from a recent pull request where the code could be simplified using the same method as the "put" example, making it much more readable in the end. The post ends with one more example from this package and how the "respond" method was refactored with the same process, simplifying it down to a more readable and less-nested version.

tagged: overloaded method readability refactor loop variable argument

Link: https://murze.be/2017/01/making-overloaded-functions-readable/

/Dev/Hell Podcast:
Episode 84: Whiny Feedback Loops
Oct 24, 2016 @ 17:33:23

The /Dev/Hell podcast, hosted by PHP community members Chris Hartjes and Ed Finkler, has posted their latest episode - Episode 84: Whiny Feedback Loops.

Chris and Ed record a very quick show to talk about Ed’s latest conference adventure, the launch of some new stuff from OSMI, and Chris talks about feedback loops, hoodies that don’t fit, and the end of TrueNorthPHP.

You can listen to this latest episode either using the in-page audio player or by downloading the mp3 directly. If you enjoy the show, be sure to follow their feed and follow them on Twitter for updates when new shows are released.

tagged: devhell podcast 84 feedback loop chrishartjes edfinkler

Link: http://devhell.info/post/2016-10-22/whiny-feedback-loops/

SitePoint PHP Blog:
PHP, Arduino, And… Minecraft? Connecting an Arduino to PHP!
Jul 12, 2016 @ 15:39:38

The SitePoint PHP blog has continued their series looking at connecting the real world with the online world via Minecraft and an Arduino. In this new post author Christopher Pitt picks up where he left off in part one and brings the Arduino in to the picture.

In the first part of this series, we learned a bit of Minecraft and the circuitry we can make inside it. We also made a circuit to alert us when the door to our mansion was opened. We then hooked this virtual alarm to a listening PHP script, so we can know when the door is opened in the context of a PHP script.

In this part, we’ll build a small Arduino-based alarm circuit. We’ll learn how to trigger the alarm, using the the official IDE and programming language, and then using something called Firmata. We’ll round the series out by connecting the alarm circuit to the Minecraft circuit, so we hear a real alarm for Minecraft mansion.

He takes some time at the beginning of the post introducing the Arduino hardware and what they have to offer. He lists the parts you'll need for this setup to work and how they need to be set up. He then gets into the code for the Ardunio side and how to get it over to the board. He presents another option to the potentially painful change-reupload cycle of debugging Arduino code: using the PHP "carica/firmata" library to connect to and add listeners to hook into the board. He uses this to then set up a PHP script to watch for changes in the Minecraft log files and fire an event to the waiting Arduino board.

The final item in this part of the tutorial series talks about bringing in the "Gorilla" extension for Carica Firmata to help prevent issues with too fast connections to the board interrupting the boot sequence.

tagged: tutorial series part2 minecraft arduino alarm connecting event loop

Link: https://www.sitepoint.com/php-arduino-and-minecraft-connecting-an-arduino-to-php/

Michelangelo van Dam:
PHP Arrays - Associative Arrays or Hash Maps
Jan 14, 2016 @ 16:50:54

Michelangelo van Dam has continued his introductory series looking at arrays in PHP with this latest post covering associative arrays (otherwise known as hash maps).

Associative array or hash maps are listings of key and value pairs with a possibility to nest additional keys and values. An associative array is a very powerful construct within PHP.

He mentions the previous article and its examples of numerically indexed arrays. He then shows how to use strings as the keys instead, pointing out that these are widely used in things like framework configurations. He shows how to use a foreach to work with the associative array and loop through each of the values, yielding the index and value for each. He also includes examples of for and do-while loops using the array_keys method to get the indexes before hand. He ends the post with a look at using the array_filter function to iterate over and find a certain record.

tagged: assoicative array hashmap tutorial introduction loop filter

Link: http://www.dragonbe.com/2016/01/php-arrays-associative-arrays-or-hash.html

SitePoint PHP Blog:
An Introduction into Event Loops in PHP
Sep 10, 2015 @ 15:06:55

The SitePoint PHP blog has posted a tutorial from Christopher Pitt introducing you to using event loops in PHP, a feature that allows for asynchronous processing, executing code while waiting for other code to finish.

PHP developers are always waiting for something. Sometimes we’re waiting for requests to remote services. Sometimes we’re waiting for databases to return rows from a complex query. Wouldn’t it be great if we could do other things during all that waiting?

If you’ve written some JS, you’re probably familiar with callbacks and DOM events. And though we have callbacks in PHP, they don’t work in quite the same way. That’s thanks to a feature called the event loop.

He starts by explaining event loops with an example from a language that naturally supports it - Javascript. He includes another example using the setTimeout function in Javascript to show a simple loop but points out that PHP just doesn't support this same kind of handling (code included). So, how can you simulate the loop like in JS? He links to and includes a examples of two libraries that could be dropped in and used to do the hard work behind the scenes of the looping:

He suggests that PHP developer "get out of the single threaded mindset" and work in ways to handle asynchronous processing into their code to improve performance and flexibility.

tagged: event loop javascript library icicle reactphp asynchronous processing

Link: http://www.sitepoint.com/an-introduction-into-event-loops-in-php/

Cees-Jan Kiewiet:
AWS PHP SDK Asynchronously
Jun 30, 2015 @ 16:31:15

Cees-Jan Kiewiet has a new post today talking about some interesting trickery he was able to do with the AWS (Amazon Web Services) PHP SDK to allow requests to be made asynchronously.

Just got off the AWS SDK for PHP Office Hour hangout and it was great talking with both team members Jeremy and Michael. And one of the things we talked about was async access to the AWS services using the PHP SDK. The goal of this post is to get the AWS PHP SDK client working asynchronously.

He starts with brief instructions on getting the SDK installed (via Composer) along with a library of his own that brings in a few other dependencies. The ReactPHP event loop is what makes the asynchronous connections possible. He includes the code to create the new handler stack and how to use it to make the asynchronous calls. A demo screencast is also included in the post to illustrate the output from a simple set of requests.

tagged: aws amazon sdk asynchronous connection reactphp event loop tutorial

Link: http://blog.wyrihaximus.net/2015/06/aws-php-sdk-asynchronously/

Julien Pauli:
On PHP function calls
Jan 22, 2015 @ 15:58:39

Julien Pauli has a new post today sharing an interesting function optimization he found using the Blackfire execution profiler.

This blog post is a technical explanation of a PHP optimization found with BlackFire profiler into a PHP script. The related post is located here : http://blog.blackfire.io/owncloud.html

He found that a replacement of a call to strlen with an isset optimized the script by about 20%. It's not typical though, he explains. He points out that the optimization worked so well because the call was part of a loop. He gets into some of the "under the covers" details of why this speed boost happens and even includes the op code output showing the difference. He then starts getting deep into the internal code for PHP and walks through each step made in the evaluation of a string's length. He finishes the post looking at isset (not technically a function) and how it handles its data checking. He also includes information about opcode caching and how to best maximize its impact.

tagged: function call strlen loop isset internals opcode cache performance

Link: http://jpauli.github.io/2015/01/22/on-php-function-calls.html

PHPMaster.com:
Say Hello to Boris: A Better REPL for PHP
Apr 02, 2013 @ 15:34:00

On PHPMaster.com today Shameer C has a new tutorial introducing you to Boris, a REPL (read-eval-print loop tool) that's a bit more enhanced than the basic PHP interactive shell.

As web developers, we know the importance of the JavaScript console provided by the browser in testing out code snippets. We don’t need to write an entire HTML page and JavaScript code just to verify the functioning or logic of a small routine we wrote. Instead, we simply run the expressions in the console and immediately see the results. Similarly, a REPL (Read-Eval-Print Loop) is the console of a programming language in which we can write code line-by-line and see what it does. [...] PHP’s REPL is very good in what it does, although it does have some limitations. [...] And so, Boris tries to solve these problems and other concerns as well.

He walks you through the installation (via a git clone and, later, through Composer) and shows how to run it as well as some sample output. He also shows how to make a custom command-line Boris runner and how to embed it into your application. His example of a tool that would benefit from this is a command-line web service client using Boris and Guzzle.

tagged: boris repl read eval print loop tool commandline github

Link:


Trending Topics: