HomeWho We AreContact

PHP Coding Style (More Than One Way to Skin a Cat)

By Neil Ludlow
Published in Development
November 29, 2020
2 min read
PHP Coding Style (More Than One Way to Skin a Cat)

I look up everything before I use it. An IDE such as PHP Storm has the PHP docs built in so can tell you a lot of the information you need, or there is the old-school looking on php.net or Stackoverflow.

I had to check whether a string ended in a certain needle and I stumbled upon this Stackoverflow post.

The accepted answerer, ircmaxell, lays out the various options you would have to check for the last character being a comma. He seems to favor string functions such as this because presumably they make more sense to him. All this is saying is that one character in from the end of the string should be a comma. Simple and clear…

if (substr($string, -1) == ',') {

The above is technically shorthand for something like this, below. Here you are manually checking the length of the string and saying that the character in last place should be a comma…

if ($string[strlen($string) - 1] == ',') {

The next version is a bit different. Moving from the end of the string, backwards to the start, he looks for a comma, if the comma is found he looks to see if it’s position is at the end of the string…

if (strrpos($string, ',') == strlen($string) - 1) {

The answerer does not seem to like the following version but I love it! preg_match is a way you can use amazing powerful regex in PHP. The regex is saying if the comma is at the end of the string the expression will be true. When you are just looking for a comma, one of the earlier ways might be simpler, but doing the calculation this way can prepare you better for more complex problems…

if (preg_match('/,$/', $string)) {

The method the answerer hates the most is the last example he gives. He converts the string into an array saying to split the string up wherever there is a comma. Then, the last part of the array will be ” if the string ends in a comma. This may not be very readable and it may not be the best way to solve this particular problem but the mechanics of solving a string problem this way, with an array, can be very powerful too…

if (end(explode(',', $string)) == '') {

Stackoverflow does not (or did not, at least) tolerate opinion very much, but we see some opinions creeping into the comments…

I know it’s subjective, but I find the preg_match one to be the most readable. – mastazi Sep 21 ‘15 at 0:23

Then, also in the comments…

From PHP 7.1 you can use if ($string[-1] == ’,’). It’s clear and faster than using substr(). – Nick Rice Jan 21 ‘18 at 11:35

So, in a later version of PHP they have created a shorthand version of the answerer’s preferred method!

For my specific problem, I was not looking for a comma, I was looking for a string of unknown length. This forfeits most of the answers and I went with the preg_match method.

Conclusion

If I had the problem of looking for a single comma at the end of a string I might use one of the other methods listed here, or, if I was trying to find it so that I could delete it I might use a rtrim to remove the offending comma. Or, are we looking for rogue commas that should be full stops? If that’s the case it might be better just to make sure the last character is a full stop. Rather than 1) search for it then 2) to fix it, maybe we can do both steps in one.

Sometimes the question isn’t “how do I do x?” the question should sometimes be “why am I wanting to do x?”


Tags

#php

Share

Previous Article
Regex Examples
Neil Ludlow

Neil Ludlow

Freelance Software Developer

Related Posts

Choosing a Tech Stack in 2024
September 16, 2024
5 min
© 2024, All Rights Reserved.
Powered By

Quick Links

About ShortdarkContact

Legal Stuff

Social Media