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

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/

Adam Wathan:
Optimizing Your PHPUnit Workflow in Sublime Text
Jan 17, 2017 @ 18:55:10

Adam Wathan has a post over on his site showing you how you can optimize your PHPUnit workflow in Sublime Text with the help of a few handy snippets.

I've been a big fan of Sublime Text's snippets feature for years.

They make it really easy to generate repetitive boilerplate code, and if you know how to use placeholders and substitutions properly, you can do some pretty magical stuff.

He shares some of his own snippets for automagically:

  • creating a new test matching the filename currently selected
  • making new test methods with some placeholder content
  • making the tests easier to run

For this last point he shows the use of the Sublime PHPUnit package to run the tests through the Sublime menu. Finally he ties it all together in a much simpler workflow including a final "all tests" run before he commits.

tagged: sublimetext editor phpunit workflow testing plugin snippet

Link: https://adamwathan.me/2017/01/16/optimizing-your-phpunit-workflow-in-sublime-text/

Laracasts Snippet:
Episode 14 - Russian-Doll Caching in Laravel
Feb 15, 2016 @ 16:22:05

The Laracasts Snippet podcast has posted their latest episode with a brief look at Russian-doll caching in Laravel.

One of the things I've been tinkering with these last few days is a mechanism for performing Russian-Doll caching in Laravel. In addition to determining if I can even make it work, I've been pondering whether this truly has a place in your future projects, or if there simply isn't enough value to warrant its usage. Who knows - let's talk about it.

The podcast also links to this series of posts on the Laracasts site on the subject. If you'd like to listen to this podcast, though, you can use either the in-page audio player or you can download the mp3 of the show. If you enjoy it, be sure to subscribe to their feed too.

tagged: laracasts snippet podcast ep14 russiandoll caching

Link: https://laracasts.simplecast.fm/14

Knp University:
Our favorite PhpStorm Live Templates: Share yours
Aug 21, 2015 @ 15:49:39

The Knp University site has a posted an article where they've shared their favorite PHPStorm Live templates, a set of handy shortcuts that can make for less typing on your part and, as a result, less development time spent on common keystrokes.

Most editors have a feature like this, and if you’re not leveraging them, you’re slowing yourself down. Seriously: taking a few minutes to get into these now could add up to a lot of hours saved in the future. The vim users at KnpLabs love this kind of stuff, and have published their own snippets (docteurklein, PedroTroller, Einenlum).

In our tutorial about live templates, we turn formhandle into a snippet that types about 10 lines of form-handling boilerplate code from 10 characters of text. So I started wondering: what are some other awesome live templates we should all be using?

To this end, they've created a repository to store some of these helpful shortcuts. Their list so far includes shortcuts for a 404 if statement, several Twig tags, several for use in a controller context and YAML formatting. The repository includes instructions on how to install the shortcuts and how to include only the ones you want.

tagged: phpstorm live template share repository starter snippet shortcut

Link: http://knpuniversity.com/blog/phpstorm-live-templates

Sameer Borate's Blog:
PHP 5.4.0 in a nutshell
Apr 09, 2012 @ 14:16:11

If you've been looking for the "quick and dirty" definition of all of the changes that come with the latest version of PHP 5.4, you should check out this new post from Sameer Borate. It has a quick rundown, code included, of these new features and changes.

Although purists have always sneered upon PHP for being a “patched” language; the evolution of PHP over the years, with new features added in every version, has only increased its popularity. The latest 5.4 release has followed the trend with some major feature additions. The following post describes some important changes in PHP 5.4.

His list includes: the updated array syntax, the bulit-in web server, array dereferencing, traits, upload progress tracking and various core changes to things like the short syntax for calling static methods in classes and the complete remval of magic quotes.

tagged: new feature summary version code snippet

Link:

Lorna Mitchell's Blog:
Using iterator_to_array() in PHP
Feb 29, 2012 @ 14:55:52

Lorna Mitchell has a new post to her blog today showing off a lesser-known but very useful function included in PHP - the iterator_to_array function, used to translate things that implement Traversable into arrays.

Someone watching over my shoulder recently had never seen the ubiquitously-useful iterator_to_array() before. [...] Mostly I find this useful when I'm working with collections of data as these often present themselves as an object that you can foreach() over, but you can't dump it directly. If the object in question implements the Traversable interface, you can instead pass it into iterator_to_array to get the data as an array.

She includes a brief snippet of code showing it in use - transforming the results from a MongoDB cursor object back into an array.

tagged: iterator translate array snippet traversable

Link:

Rob Allen's Blog:
Sublime Text 2 Snippet for PHP getter and setter generation
Jan 03, 2012 @ 15:54:23

In a quick new post to his blog, Rob Allen has shared a snippet for the Sublime Text 2 editor to make creating getters and setters for your class simpler (dynamically too).

As with a lot of editors, Sublime Text supports snippets which are essentially text expansions of a short phrase into more text. I needed to create a few getXxx() and setXxx() methods for some properties of a class and decided that the easiest way to do this would be with a snippet.

Included in the post is the code you'll need to put into the snippet - a simple find (regular expression based) looks at the currently selected variable and expands out the getter and setter for it. For more information on the Sublime Text 2 editor, see the product's website.

tagged: sublimetext2 editor snippet getter setter code

Link:

Sameer Borate's Blog:
Grabbing the referrer search engine keywords for a site
Oct 18, 2011 @ 18:25:27

On his blog today Sameer Borate has a new post with a handy bit of code you can use to find the keywords from a search engine referral to help with tracking how visitors have come to your site.

A couple of weeks back I had to write a solution for a client to track the referrer search engine from where the user came to his sites contact page, without using Google Analytics. If a user was to fill the contact form on the website, the referring search engine name and the keyword for which it was refereed was to be emailed along with the contact information. The following is a solution for the same.

The code itself is pretty simple - it checks the $_SERVER['HTTP_REFERER'] and, based on an array of search engine types, looks for a certain "query" keyname in the URL and matches what follows (with a regular expression). This can be useful for not only determining what sort of audience is visiting your site, but could also be used to present a custom message to visitors from certain search engines (or, more complicated, to show different content based on search terms).

tagged: search engine keyword referrer url snippet

Link:

WebInOne.net:
Date validation for CodeIgniter 2
Oct 07, 2011 @ 16:57:21

On the WebInOne blog there's a post about doing some date validation in CodeIgniter 2 forms using the "set_validation" method.

I wrote a tutorial here about the CI date. At that post I used the HTML <select> for the input of date. Sometime our client can want to use text box for the date input. We need the validation for this date. I have found this code from here. It is for CI 1.7.* and PHP 5.2.*. So I change some code for the CI 2.0.* and PHP 5.3.*.

There's some code snippets included in the post showing how to use it in the form (HTML), setting the validation on the form object (for US and UK validation) and the actual code of the updated custom Validation class.

tagged: date validation tutorial framework snippet form

Link:

CatsWhoCode.com:
10 super useful PHP snippets
Apr 05, 2011 @ 13:38:10

The CatsWhoCode.com blog has a new post today with what they call super useful PHP snippets that could help you out in a pinch.

Having the right code snippet at the right time can definitely be a life saver for web developers. Today, I’ve compiled 10 really awesome PHP code snippets that will, I hope, be very helpful in your forthcomming developments.

Their list of ten includes bits of code for:

  • Super simple page caching (file-based)
  • Convert seconds to time (years, months, days, hours...)
  • Get current weather using Google API
  • Get latitude and longitude from an address
  • Save url to PDF
tagged: snippet useful helpful

Link:


Trending Topics: