A programmer’s journey to mastering a language and all its features is never considered complete, especially while a language is still in development and obtaining new features. As you read more about a language and discover new tools, you will usually find new and more efficient methods to tackling problems. When you learn something new, you should do the best you can to practice what you just learned by trying to use it on your own self projects or blogging/podcasting about it so you can cement the concepts in your mind by explaining what it is you just learned to other people.
Splat operator
PHP 5.6 gained a new feature called the “splat” operator. If you know what the ellipses looks like, then you know what the Splat operator looks like: “…”. The splat operator allows you to define as many variables in an argument list as you want, and that variable with the splat operator becomes an array.
Here’s an example function:
function variableArguments( $first, …$second ) {
$string_variable = $first;
foreach ( $second as $second_variable ) {
$string_variable .= “ “.$second_variable;
}
return $string_variable;
}
echo variableArguments( “This is”, “one”, “two”, “three”, “four” );
The above code would output: “This is one two three four”. You could also use more or less arguments if you chose to. While this example I provided above is rudimentary and not very useful, you can start to get some ideas of what you can use this for in your own PHP code. The best part of using the splat operator is that you can accept an unlimited amount of parameter arguments in your functions when using the splat operator. A limitation of the splat operator is that you can only use it on the last parameter in a method.
Session Upload Progress
Since PHP 5.4.0, we have had the ability to track the upload progress of files. Usually this isn’t very useful for small files, because they would be uploaded pretty quickly. When you have larger files, it’s very useful to track the progress until you’re finished uploading your file. I remember times from the early 2000s where in order to see if your file was uploaded, I would have to poll the server to see if the file was done uploading. When you set the PHP INI variable “session.upload_progress.enabled”, you can track individual files being uploaded. The progress is stored in the $_SESSION variable. You can read the official documentation here https://www.php.net/manual/en/session.upload-progress.php
0 Comments