On of the things that I’ve enjoyed the most about working through the live workshop on WordPress plugin development is the conversation that’s ensued around writing WordPress code and PHP code readability in our Basecamp discussions.
For those of you who have been around a while, you know that I’m opinionated when it comes to writing WordPress code. Specifically, I believe that just because WordPress is written in PHP does not mean that we should forgo the APIs and use PHP functions just because we’re either more familiar with them or because we find them easier to use.
That said, there are times in which the native PHP functions are the way to go.
For example, string comparison.
PHP Code Readability: Strings
When it comes to detecting if a string is empty, or set, or if it exists, PHP offers a variety of functions that can be used to evaluate the value.
These functions include:
- `empty`
- `isset`
- `strlen`
- …and so on
The thing is, each of these functions actually lends themselves to a specific instance and I tend to lean in the direction that just because it works in a specific instance doesn’t mean that it’s the instance for which the function was intended.
Nonetheless, in the plugin that we’re building in the workshop, I have the following block of code:
if ( 0 < strlen ( $short_url ) ) { // The value exists, so do one thing } else { // The value does not exist, so do something else }
Easy enough, but I received a great question in the forums as to why I set my conditional up this way:
Why do you use this type of if check and not another?
`if ( 0 < strlen( $short_url ) ) { }`
Perhaps something like this?
`if( ! empty( $short_url) ) { }`
Again, I thought this was a good question because it demonstrates using another PHP function which can evaluate a string just as easy as another function.
But the reason that I’m a fan of using `strlen` to evaluate the length of the string is because PHP is not a strongly typed language, and using `strlen` explicitly shows that I’m intending to operate on a string.
For me, it’s an issue of PHP code readability.
Are There More Instances Like This?
I’m sure there are, and as I come across them I’ll likely be discussing them here. The thing is, we all have our own opinions and different styles for how we should be writing code.
This is just one such opinion, so feel free to share your own in the comments, as well.
Besides, I’d much rater learn how the rest of you guys are doing it, too.
That’s no PHP syntax.. If you enable DEBUG mode, you get notice in your checking with 0 < strlen( $arr['key'] ).
I think the better way its empty() method.
Yakir.
I think you’ve missed the point of my post: I wouldn’t use `strlen` on an array – instead, I’d use `empty` or `isset`.
The point that I’m trying to make is that I use `strlen` when I’m working with strings.
nice point tom, i personally use empty for string check or using $var only like this
$var = 'i am here';
if($var) { echo 'string exist'; } else { echo 'string not exist'; }
I think that’s a completely acceptable way to do it as it relies on the feature of a dynamic language – JavaScript works the same way.
The only thing about it, in my opinion, is that it’s not clear if you’re performing the conditional later in the code where the variable isn’t declared immediately above it.
`empty` is a good function to use to check if the variable is even defined, `array_key_exists` is great for associative arrays, `isset` is great to check if the index of an array exists, and `strlen` is good for string operations.
There are also other things we can do like performing `is_numeric` checks to make sure that the variable we’re dealing with are numbers, but this comment is getting longer than the post itself so I digress for now :).
I do appreciate the comment, Richie :).
Well, PHP definitely has its upsides and downsides. And sometimes, it just sucks (-;
https://wiki.theory.org/YourLanguageSucks#PHP_sucks_because:
Oh geez, not one of these sites. #LOL
If we’re gonna go down this route, we might as well say every language sucks :D.
Hey Tom,
completely get your point. On the other hand one could argue “just comment your code” or “an URl is always a string”. The reason why I personally prefer
empty()
over most other options (and that includesstrlen()
as well) is speed. And when you take naming variables seriously, then you should in most cases know with what you deal. The rest will be taken care of by a strict developing environment that screams like a little girl when you got the wrong type.You’re hitting on a point that I’ve yet to discuss here and that’s that I’m actually a fan of hungarian notation in dynamically typed (or weakly typed) languages so that it’s easier to know what each variable is supposed to hold.
Of course, you hit on this in your comment:
I’m also all for code comments – almost going over board with them, so I’ve been told, but I’ve had enough experiences to know that leaving comments generally helps more so than not especially when it comes to maintaining something over a few years even if it’s something in which you’re the only developer.
But now I’m digressing from the original point :). In short, I appreciate the comment and always dig hearing other developers’ perspectives on how they do their stuff.
It’s always food for thought for me.
look like strlen() same as length(); on javascript.. I don’t this function ( strlen(); ) previously.. May be strlen is short word for string length.. ;)
Yep! `strlen` is short for string length and it’s specifically used for getting the length of a string.
Easy enough, huh?