If you’re a PHP developer, you’ve probably heard about Laravel. But did you know that you can use some of the coolest parts of Laravel in your plain PHP projects?
One of those parts is the illuminate/support package, which makes working with arrays, collections, and strings much easier.
In this post, I’ll show you how to use the illuminate/support package in a simple PHP script.
What is illuminate/support?
The illuminate/support package is a set of helper functions and tools that come with Laravel, but you can use them in any PHP project. It gives you things like:
- Helper functions to work with arrays.
- Collections: A powerful way to handle arrays with lots of built-in functions.
- Other useful helpers for things like strings, validation, and more.
You don’t need to install Laravel to use this package, so it’s a great way to get some of Laravel’s magic in your project without all the complexity.
Step 1: Install the Package
First, you need to install the illuminate/support package using Composer. If you don’t have Composer installed, you can download it from here.
Once you have Composer, follow these steps:
- Create a folder for your project (e.g.,
my-php-project). - Open the terminal (Command Prompt or Terminal) and go to that folder.
mkdir my-php-project
cd my-php-project
- Run this command to install the package:
composer require illuminate/support
Composer will download and install the package for you.
Step 2: Use illuminate/support in a PHP Script
Now, let’s write a simple PHP script that uses some of the features of illuminate/support. Create a file called index.php in your project folder.
Here’s an example script that uses arrays and collections:
<?php
// Include Composer's autoloader to load the installed packages
require 'vendor/autoload.php';
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
// Example 1: Using Illuminate\Support\Arr (Array Helper Functions)
// Create an array
$myArray = [1, 2, 3, 4, 5];
// Get a random item from the array
$randomItem = Arr::random($myArray);
echo "Random item: " . $randomItem . "\n";
// Get a subset of the array
$subset = Arr::only($myArray, [1, 3]);
echo "Subset: ";
print_r($subset);
// Example 2: Using Illuminate\Support\Collection (Collections)
// Create a Collection (just like an array but with extra features)
$myCollection = new Collection([1, 2, 3, 4, 5]);
// Double each number in the collection
$doubled = $myCollection->map(function ($item) {
return $item * 2;
});
echo "Doubled collection: ";
print_r($doubled->all());
// Filter to get only even numbers
$evenNumbers = $myCollection->filter(function ($item) {
return $item % 2 == 0;
});
echo "Even numbers: ";
print_r($evenNumbers->all());
?>
Step 3: Run the Script
To run the script, go to your terminal and use this command:
php index.php
What Will Happen?
- Array Functions:
Arr::random($myArray)picks a random item from the array.Arr::only($myArray, [1, 3])picks only specific items from the array (at index 1 and 3).
- Collections:
- Collections are just like arrays, but with more features. Collections provide a lot of helper methods to manipulate and work with data in a more structured and readable way like
first(),last(),get(),find(), and many others. map()lets you apply a function to each item in the collection. In this case, we double each number.filter()allows you to keep only certain items. Here, we filter out the even numbers.
- Collections are just like arrays, but with more features. Collections provide a lot of helper methods to manipulate and work with data in a more structured and readable way like
Sample Output:
When you run the script, you will see something like this:
Random item: 3
Subset: Array
(
[1] => 2
[3] => 4
)
Doubled collection: Array
(
[0] => 2
[1] => 4
[2] => 6
[3] => 8
[4] => 10
)
Even numbers: Array
(
[1] => 2
[3] => 4
)
Why Should You Use illuminate/support?
Here are some awesome reasons to use this package in your projects:
- It makes things easier: Working with arrays and collections becomes much smoother.
- It’s lightweight: You don’t need Laravel to use it. Just install the package and start using the helpers.
- It saves time: You get to use powerful functions without having to write them yourself.
Final Thoughts
The illuminate/support package is a simple way to get some of Laravel’s magic in your plain PHP projects. Whether you’re working with arrays, collections, or just need a few helpful functions, this package can make your life much easier.
If you want to get started with Laravel but don’t want to dive in completely, this package is a great first step. Plus, it works perfectly fine in small PHP scripts or larger applications.
Give it a try and see how much easier your code can become!
