I learned a few things this week.
1. I learned that php 7 allows type hinting for basic variable types, and for return type hinting.
“So, what’s type hinting?” you ask. “And why is this a big deal?”
Type Hinting is being able to tell what kind of variable you expect when you activate (call) a bit of code. Variables come in lots of types: there are numbers, strings of letters and other characters, yes/no bits (called boolians), arrays, and on it goes.
A common kind of error happens when your code is expecting one kind of data (maybe you need a number, because you want to know how many units you’ve sold), but the wrong kind of data gets passed in (maybe the word “units”). Everything breaks down, because you can’t multiply the word “units” by a price to get a total. Type hinting allows you to say right off the bat if you’ve got the kind of information you need to do what you need to do. If you specify you need a number and you don’t get a number, you know right away, rather than when something unexpected happens down the line.
Up to now (well, a year ago when php7 was released) you could only specify complex data types, but you couldn’t say for numbers and strings. And, type hinting the returned output allows you to know right away if you didn’t get back the kind of data you expected, too.
So , yeah, this is a big geek deal.
2. I learned a few new things about php strings. String padding.
I guess I just never had any reason to go there before. Maybe I did, I don’t know.
String padding is handy when you need to have a string of characters that is exactly so many characters long. Say you need an invoice number and it always has to have 12 characters in it. So your first few invoices are a few characters short of 12. Say it’s invoice ARZ–13579. That’s 9 characters. So you can pad that out with spaces in the front by just specifying you need it to be 12 characters and add spaces to the left. All fixed!
I probably learned a few other things this week, too. But those are the ones that stand out. Now you know a little bit about them too. Congratulations!