Skip to content

Mastering PHP and Laravel

Never stop learning!

Menu

  • Home
  • Blog Posts
  • Humor

PHP

The Pipeline Pattern

The pipeline pattern is one way to refactor your code to separate concerns. Here is an example of un-refactored code that uses individual if statements to process some data. I’ll keep the details out for clarity. This code seems simple

Kenny July 3, 2023September 27, 2023 Learning, PHP Read more

The current state of things

It’s amazing how life gets going in a rhythm, and you get your head down and working so hard, that you sometimes forget to look around (or in this case, write in your blog). Working at a small travel advertising

Kenny March 26, 2020September 27, 2023 Blog Posts, Javascript, Laravel, Learning, PHP, Programming Read more

Sorting Associative Arrays in PHP

$arr = [ ‘foo’ => ‘bar’, ‘faz’ => ‘baz’ ]; array_multisort(array_column($arr, ‘value’), SORT_ASC, $arr); You can sort an associative array with one line of code as I did above. As a result, the array becomes sorted by the keys: [

Kenny January 6, 2020September 27, 2023 Blog Posts, Learning, PHP, Programming Read more

PHPStorm – Stop those accidental tab closures!

If you’re like me, you have probably had this happen. You go to click on one of the many tabs you have open in PHPStorm, only to accidentally click the X, and close it. ARGH! Today I found a way

Kenny October 9, 2019September 27, 2023 Blog Posts, PHP, PHP Storm, Programming, Tools Read more

Defensive Programming | Laravel find()

Defensive Programming One of the things I’ve learned in my career is to code defensively. Laravel makes it very easy to ask for an object, but it also makes it very easy to get it wrong. If you request an

Kenny February 5, 2019September 27, 2023 Blog Posts, Eloquent, Laravel, PHP Read more

Laravel – the difference between all() and get()

When dealing with a model, all()  is a static method. It creates a new query object, and runs get()  on that object get()  is not static, but can be called statically because of a magic method in the Model class.

Kenny October 12, 2018September 27, 2023 Blog Posts, Eloquent, Laravel, Learning, Programming Read more

Laravel Eloquent only()

$fullModel = Model::find(1) // Get model with id 1 $idArray = $fullModel->only(‘id’) // array containing id // this does not work. You’ll get back an empty collection // It is trying to pull the id column off the collection object,

Kenny October 12, 2018September 27, 2023 Blog Posts, Eloquent, Laravel, Learning, Programming Read more

Laravel Eloquent Attributes

When dealing with an Eloquent model, you can add your own attributes. These can be useful for computed values. For example: class Person extends Model { // By default, all the database fields will be available. // Let’s assume for

Kenny October 9, 2018September 27, 2023 Blog Posts, Laravel, Learning, PHP, Programming Read more

Laravel Eloquent sortBy function

Here is how you can sort a collection by an ordered array of ids. The use case here was that I had a number of objects in a Redis cache, which were retrieved in unsorted order. The objects were large,

Kenny October 9, 2018September 27, 2023 Blog Posts, Laravel, PHP, Programming Read more

Compact and Extract

Compact takes individual variables, and packs them into an associative array. Extract does the opposite. <?php // example code // Set 2 variables $var1 = “one”; $var2 = “two”; // Create associative array $arr = compact(‘var1′,’var2’); // Show results var_dump($arr);

Kenny August 15, 2018September 27, 2023 Blog Posts, Learning, PHP Read more

Splat!

While looking through the Query Builder class in the Laravel source code today, I came across something I hadn’t seen before in PHP: $query->{$method}(…array_values($value)); I had no idea what the three dots meant, so I went looking for it. Turns

Kenny August 15, 2018September 27, 2023 Blog Posts, Learning, PHP Read more

Understanding Octal

Why would we need to understand Octal? The most relevant reason I can come up with right now is that looking back at some of the interview question challenges I’ve posted, there is one that deals with an Octal number.

Kenny July 26, 2018September 27, 2023 Blog Posts, Learning, PHP, Programming Read more

foreach vs array_filter

The problem: Given an array of numbers, create a new array containing only the odd numbers from the original. $original_array = [1,2,3,4,5,6,7,8,9,10]; There are two ways you can approach this problem. Let’s look at a foreach loop first: $original_array =

Kenny July 23, 2018September 27, 2023 Blog Posts, Learning, PHP Read more

Factor Finder

// Number to factor $num = 60; // Loop from the number to factor down to 1 for($i=$num; $i>=1; $i–){ // If it divides evenly if($num % $i === 0 ){ // Output result of division, which is a factor

Kenny July 18, 2018September 27, 2023 Blog Posts, Learning, PHP, Programming Read more

Changing the color of the first word

The question/problem is: Write a PHP script, which changes the color of the first character of a word. This challenge tests your knowledge of preg_replace, and regular expressions. $text = ‘PHP Tutorial’; $text = preg_replace(‘/(\b[a-z])/i’,'<span style=”color: red;”>\1′,$text); echo $text; ?>

Kenny July 16, 2018September 27, 2023 Blog Posts, Learning, PHP, Programming Read more

Reference challenge

This one tests your knowledge of how reference variables work: $a = ‘P’; $b = &$a; $b = “PH$b”; What will $a and $b be after these three lines of code run? First, $a is set to ‘P’. Then, $b

Kenny July 12, 2018September 27, 2023 Blog Posts, Learning, PHP, Programming Read more

preg_match problem

Given: $a = “4|6|3|5|x|2|”; Write code that will output: “2|3|4|5|6” It could be done the long way round, by turning the string into an array, looping over each element, testing each item to see if it’s a number, and placing

Kenny July 10, 2018September 27, 2023 Blog Posts, Learning, PHP, Programming Read more

MySql Limit

SELECT c.first_name,c.last_name, o.order_total FROM customers c LEFT OUTER JOIN orders o ON c.id = o.cust_id WHERE o.order_total > 100.0 I was once asked to write a query similar to this. The goal was to select all customers who had placed

Kenny July 10, 2018September 27, 2023 Blog Posts, Learning, PHP, Programming Read more

Array Sorting

I came across this problem recently: How would you sort an array of strings to their natural case-insensitive order, while maintaining their original index association? array( ‘0’ => ‘z1’, ‘1’ => ‘Z10’, ‘2’ => ‘z12’, ‘3’ => ‘Z2’, ‘4’ =>

Kenny July 10, 2018September 27, 2023 Blog Posts, Learning, PHP, Programming Read more

How to enable PHP error reporting

ini_set(‘display_errors, 1); error_reporting(E_ALL);   The ini_set function sets a configuration option for the duration of the script execution. When the script is done, the value returns to whatever it was set to originally in the ini file. The error_reporting function

Kenny July 10, 2018September 27, 2023 Blog Posts, Learning, PHP, Programming Read more
  • « Previous

Archives

Categories

Copyright © 2025 Mastering PHP and Laravel. All rights reserved. Theme Spacious by ThemeGrill. Powered by: WordPress.