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

Colin O'Dell:
Automatically Switch PHP Version on cd
Aug 09, 2018 @ 15:48:31

Colin O'Dell has shared an interesting setup he's created to make it easier to switch between PHP versions when testing your code. In his latest post he shows how to switch the version "on cd" (directory change) with the help of some command line shell tooling.

After using phpbrew to manage my local PHP versions for a while, I got tired of re-compiling PHP after every release and decided to install multiple PHP versions side-by-side with Ond?ej Surý's PPA. One of the features I missed from phpbrew was the ability to run a command like phpbrew use php-7.2.8 to automatically change the php command to that version, so I ended up implementing this feature myself using symlinks and shell aliases.

He then walks through his end result, showing the modification of the default php symlink and aliases that can be used to dynamically switch its target. He then includes some examples of how to automate this using your application's PHP version requirements in the composer.json and a simple ZSH script that's triggered on directory change.

tagged: version development composer zsh commandline shell switch tutorial

Link: https://www.colinodell.com/blog/201808/automatically-switch-php-version-cd

Derick Rethans:
PHP 7.2's "switch" optimisations
Nov 01, 2017 @ 14:34:29

Derick Rethans has a post to his site covering some optimizations around PHP's "switch" handling and how it has changed in the upcoming PHP 7.2 release.

PHP 7.2 is around the corner soon, and comes with many optimisations. Many new optimisations are implemented in opcache, but some others are implemented in PHP itself. One optimisation that falls in the latter category is an optimisation of the switch/case construct.

He then gets into the differences between the previous functionality and how it has changed, mostly in how the language handles the evaluation of the "case" statements. He include a flow diagram of how the pre-7.2 flow happened and how, with PHP 7.2, a "jump table" is used to optimize the process. This table allows PHP to perform a lookup on the value rather than evaluating equality (like in an if) on string values. If it's not a string the same evaluation happens as before, however.

tagged: switch php72 optimization if evaluation jumptable

Link: https://derickrethans.nl/php7.2-switch.html

Jeff Madsen:
PhpStorm: Tasks & Contexts with Your Git Branches
Sep 26, 2017 @ 14:37:29

On his Medium.com site Jeff Madsen shows you how to use contexts in the PhpStorm IDE to switch between environments or current work using a more streamlined process.

Switching context is a pain.

Not just because you need to mentally switch the complex web of ideas in your head. Think about all the physical files on different git branches you have to remember in order to answer a “quick question about task #123”. [...] PhpStorm has a lot of great context links and shortcuts to help you navigate among all these, but it is still a royal pain whenever you need to put one set of files aside and work in a different area of the codebase.

[...] When I finished something and pushed it up for review if there was even a small request to change a default or label I had to reopen the branch and track down the correct files where the work was done. How could I turn that all into a single, easy step?

Enter Contexts and Tasks!

He starts off by defining what a "context" is in the world of PhpStorm - a group of open files with a name attached - and how they can be created/saved inside the IDE. Next is the idea of "tasks" that help with performing operations and relating them to contexts and groupings of files. He then shows how to switch between tasks related to a certain feature and how to close it out when you're done.

tagged: phpstorm custom task context group file switch tutorial

Link: https://medium.com/@codebyjeff/phpstorm-tasks-contexts-with-your-git-branches-92d9d1c5a34b

Michael Dyrynda:
Switching PHP versions with Laravel Valet
Jan 24, 2017 @ 15:57:40

On his site Michael Dyrynda shows you how to switch PHP versions in Laravel Valet away from the current default of PHP 7.1 (should the need arise).

At the time of writing, Laravel Valet ships with PHP 7.1 but if you're like me, you have some legacy projects around the place that haven't quite lifted their dependencies to PHP 7 just yet.

A lot of folks might have previously used a VirtualBox Virtual Machine, or more recently considered Docker but a lot of the time and especially when dealing with simpler situations, Valet may be all that you need.

Luckily, the combination of Valet and brew on macOS makes switching PHP versions really easy.

With the help of the Homebrew tool it's as easy as telling it to install the PHP version you require and create the link to this needed version. He's included two screencasts in the post to show the process and commands you'll need to accomplish it.

tagged: laravel valet language version switch homebrew link tutorial

Link: https://dyrynda.com.au/blog/switching-php-versions-with-laravel-valet

INANI El Houssain:
Build your OWN switch statment using Laravel’s custom blade directives
Nov 03, 2016 @ 15:26:01

In this post on his Medium blog INANI El Houssain shows you how to create a custom directive for use with Laravel's Blade templating language. In this example he shows how to make a custom switch statement, something commonly used on the PHP side to select an action based on a value.

One of the good points of Laravel’s framework is that it allows you to make your own components, macros and directives. so today we will make use of Laravel’s Custom Blade directives and make something good.

He starts with a simple "hello world" example to show where the pieces all live, outputting a simple "Hello $name" string. He then moves into the creation of the "@switch" directive having it write out the PHP code required for the switch to start and end. He adds in two more tags to start and end the different cases: @case and @endcase. The post wraps up with an example of all of these tags in use and how to catch when the value under evaluation might be empty.

tagged: laravel blade directive custom output switch tutorial case

Link: https://medium.com/@InaniT0/build-your-own-switch-statment-using-laravels-custom-blade-directives-218244e41a7c#.dtkbzif3j

Evert Pot:
Save memory by switching to generators
Aug 11, 2015 @ 14:45:51

Evert Pot has a post to his site showing you to conserve memory with generators in your PHP scripts. Generators are a language feature that allows you to generate/manipulate data like an iterator without needing to pre-generate the array beforehand.

Since the release of PHP 5.5, we now have access to generators. Generators are a pretty cool language feature, and can allow you to save quite a bit of memory if they are used in the right places. [...] It's not uncommon in complex applications for the result of a function like our [example] to be passed to multiple functions that mangle or modify the data further. Each of these functions tend to have a (foreach) loop and will grow in memory usage as the amount of data goes up.

He uses a common example of fetching a set of articles from a database to show how memory consumption could get huge when a large number of articles are involved. He rewrites the example using generators instead, making use of the yield functionality to only fetch one record at a time and map it to the object structure. He also includes a few things to watch out for when using generators including the different return value of the method (iterator, not an array). He also points out an issue where the array_* functions will no work on iterators so you'd need to convert it back to an array before use.

tagged: memory generator switch example records yield

Link: http://evertpot.com/switching-to-generators/

HHVM Blog:
Wikipedia on HHVM
Jan 07, 2015 @ 17:47:20

In a new post to the HHVM blog, Brett Simmers looks at the recent announcement from Wikipedia and how they made the switch to HHVM and the impact it made.

If you’ve been watching our GitHub wiki, following us on Twitter, or reading the wikitech-l mailing list, you’ve probably known for a while that Wikipedia has been transitioning to HHVM. This has been a long process involving lots of work from many different people, and as of a few weeks ago, all non-cached API and web traffic is being served by HHVM. This blog post from the Wikimedia Foundation contains some details about the switch, as does their page about HHVM.

Brett spends the rest of the post talking about his time working with the Wikimedia foundation and some of the hurdles they had to tackle along the way. This included things outside of PHP too like an issue with their Lua extension and compile changes in the installed PCRE version (no JIT). He also shares some of the statistics (in graph form) of the results of the move to HHVM from normal PHP5 - an impressive drop of around 7 seconds, median save time. He also includes a graph showing the server loads and the resulting (very impressive) drop from the move.

tagged: hhvm wikipedia statistics wikimedia switch php5

Link: http://hhvm.com/blog/7205/wikipedia-on-hhvm

Derick Rethans' Blog:
Multiple PHP versions set-up
Nov 07, 2011 @ 15:14:00

Derick Rethans has a new post today looking at a method for setting up multiple PHP versions on the same machine and switch between them with a little help from some simple bash scripts.

For many of my projects (both hobby and commercial) I need to support many different PHP configurations. Not only just different PHP versions, but also debug builds, ZTS builds and 32-bit builds. In order to be able to test and build extensions against all those different PHP configurations I have adopted a simple method that I'm sharing with you here.

Using this script he can tell his system to pull the latest version of any release from SVN and try to compile it. The trick is putting each of them into a different directory under /usr/local. He uses another small shell function (with a function called "pe()") that sets up the pathing to the right release, complete with its own locations for PEAR/PECL and a php.ini.

tagged: multiple version setup bash script switch tutorial

Link:

Joris de Wit's Blog:
Extending different layouts for Ajax requests in Twig, Symfony2
Aug 29, 2011 @ 16:39:34

Joris de Wit has a (very) quick post about a handy tip he found about switching layouts easily with Twig in his Symfony2-based application - a handy ternary sort of switch that can detect when something's an Ajax request.

I just learned about the 'app' global variable in twig. It's very handy for loading a special layout for ajax requests.

The "app" variable allows you get get back at some of the settings of your application and check on special things like the isXMLHttpRequest in his example. For more information about Twig and how you can add it to your application, check out Twig-Project.org. Using it's as simple as adding a phar.

tagged: twig symfony2 layout switch template ajax request

Link:

Martin Psinas' Blog:
Switching to PDO
Aug 04, 2011 @ 15:17:59

In a new post to his blog Martin Psinas talks about some of his pains experienced with upgrading his code to use PDO instead of the mysql extension for database interaction.

I read not too long ago that the mysql library in PHP is being deprecated as of v6.0 in favor of mysqli or PDO, so of course I had to update all of my database code keep on top of things. I spent about 5 or 6 hours over the course of 2 days familiarizing myself with the new syntax and updating my code offline. Without any testing, I decided I could go ahead and push the code "live" because I'm just that over-confident sometimes, although I did make a backup in case anything went wrong (or so I thought).

Two problems jumped out immediately - a SQL error caused by this bug and the other being a problem with preparing his statement inside of a session handling method.

tagged: pdo mysql switch problem prepare session limit bug

Link:


Trending Topics: