What are the best features of PHP 8 version? Learn why should you upgrade!

There are a lot of people who have some very strong opinions about php uh but you know what i actually really like php i’m a web developer i really do like php uh the fact of the matter is php has a bad reputation because it gives you a lot of slack that you can very easily get yourself into trouble with um but by that same token i think that’s why it’s kind of fun you have to kind of know what you’re doing with php uh or else you’ll get yourself into some very sticky situations really quickly yeah php is uh is a good language though and i really like it uh and that’s why i’m excited that there’s now uh php version 8 available and i wanted to talk about some of the really cool new features that are coming with version 8 that i’m excited for so let’s talk about it now a lot of people will say oh it’s not strongly typed so therefore php is a bad uh choice for anything well you know what the web is not typed at all like it’s the web is literally just strings of text personally i don’t think that it makes sense to have you know a server side language that is strongly typed when you’re basically exclusively dealing with strings now you can parse the strings into types of data and you can do other things like that which is what php is for but this is actually a really interesting uh new feature same string and number comparisons uh in previous versions of php if you were to perform a weak comparison which is two equal signs between the integer zero and the string fubar for example it would return true i can’t tell you how many times that actually has thrown me off it didn’t make sense thankfully this has been solved now php’s default behavior is to check if the string is numeric uh it have if it has numbers in it and if it does it does a number comparison otherwise it converts the number to a string and then compares the strings that’s a much more sane way of doing it so if you were to do uh the integer versus weak compared against fubar there are no numeric characters in the string foo bar therefore the string the number zero would actually be converted to a string zero and then compared and they would not match now this would be higher up much higher up on my list of awesome if php8 features but the problem is i can see this having like in insane potential to break a lot of existing uh scripts that are written out there uh tons of weak comparisons are now going to uh potentially change the result uh which means legacy code written in php might break that is kind of a thing though with php like everybody kind of expects that to happen uh so yeah that’s why i’m not putting it higher up on the list consistent errors for internal functions this is actually really interesting because it used to be that if you supplied an invalid type of as an argument to a php internal function you’d get a warning instead of a type error this has changed in php 8. for example if you were to provide an empty array to sterlin which is like a way of checking how long a string is it would throw a type error now but uh previously it would just throw a warning and potentially the script would just keep executing and you’d have like a null result and things could get wonky and similarly if you did array chunk with a second parameter of negative one it would now throw a value error where before it would just throw a warning this is going to be really handy for development and debugging and i can also see this causing a lot of issues with existing scripts out there especially since type error will abort execution where the former behavior would just let the script keep going so interesting this is going to be interesting to see how how legacy scripts handle these kind of errors so the next thing on my list is union types because uh php doesn’t have strong typing you can have a an array that is an integer or a float and then you can convert that float to a string or you could uh change the the string to an array or even like change it to an object there’s there’s no reason you can’t do any of that but you can actually do strong typing in php but now there are union types where you can actually assign a variable to say that i only want this to be a integer or a float i don’t want it to be anything other than those two things so being able to declare your variables as a certain type or as two or more different types is really handy it’s going to be handy for people who like to code in that way especially next up is null safe operators basically the way this works is by using the null safe operator here you can actually access parts of an object like either a method or a property that might not exist and if it doesn’t it won’t throw an error it will just return null um that’s super useful uh because doing a lot of error checking and error handling and stuff like that it can get really confusing especially when you have to nest checks to make sure that a part of a an object exists it’s it’s very confusing it’s not super elegant this is a much more clean way of checking if a part of an object exists or not this is a really cool feature but i wish that it was a little bit more flexible all the documentation i’ve seen uh is about accessing uh properties or methods inside of an object and i would kind of like this to just work with arrays or any kind of nested hierarchy constructor property promotion this is going to be really really awesome there’s a ton of examples in my own code and in code across the the web when you’re creating an object in php uh basically the way it works is you you can declare your variables that are part of the properties that are part of the object and then you have to accept uh arguments when you’re creating the class in the constructor method and then you have to actually assign the value of the arguments to the actual properties in the in the class um it’s super annoying and you do it all the time so now php php8 allows you to actually just declare uh the values of the thing right in the arguments uh in the constructor class and it just automatically assigns those values as they are uh as the constructor is being executed i think that’s super awesome i’m really excited about that besides the fact that it’s gonna save me a ton of boilerplate code and a ton of time it’s just gonna be easier to create new classes in php and i love that next up is attributes attributes is something that has never been natively supported in php but it’s basically uh metadata that exists alongside your anything classes functions methods parameters properties classes variables it it’s a way of like storing metadata that the rest of the program can read uh at runtime using the reflection api this is something that i’m super stoked about i’m gonna try uh a bunch of cool ideas that i have on how to use attributes i’ve never actually used like phpdoc which is a way to um to do this in a in a non-native way the problem with phpdoc is that you you end up writing your attributes in like code comments it doesn’t get checked uh before the program is actually executed so you can like end up with a lot of like errors happening because the you know you might have made a typo in your comment and then when it goes to parse that it it has a typo in it it’s not going to work the way you expect um this is a native way to actually handle attributes in php uh and i think that this is super cool i i have this idea of how i think i’m going to rewrite or like rewrite a part of the router system that i’m using uh i think this is really sweet all right next up is trailing commas in parameter lists uh i’ve got to say i’ve got in the habit of writing um arrays with like a you know you open the open the array and then you have the property and a comma and then a second property and a comma and then close the array that’s i mean that’s a good practice especially if you’re putting uh the elements of an array in you know on new lines um because then if you go to add a new uh element and you wanna like commit your changes you’re only changing one line instead of two in your git commit but the problem is when you try you know when you’re used to writing your elements in your array with trailing commas and then you go to actually you know call a function and you add a trailing comma uh well then you have an issue because it’ll throw an error i can’t tell you how many times i’ve absentmindedly done this myself and the fact that the language is actually becoming tolerant to this makes me super excited uh stir contains stir starts with and stir ends with these are functions that i’ve actually written for myself but the fact that uh they’re becoming integrated functions into php itself means that uh i don’t have to maintain this code myself that that makes me really happy named arguments now named arguments is something that i’m like really really stoked about we’re getting to like the highest tier of awesome features in php eight here so let’s say you have a function like this right you have name color equals red and number equals three now if you’re going to call this function you could just say do something and then name provide just a name and then that’s it that’s all you have to do you don’t have to provide the color or the number because the arguments uh are linear here so if you were to say gardner green eight uh then it would those would associate uh respectively to the order that they’re written in the function um but what if you want to just use uh say the name and the number and not the color uh just have the colors remain the same well there really hasn’t been a good way to do that outside of php8 i mean if you wanted to do that you would have to actually like look up the documentation or the code that you had written and find what the default value is and then just provide that as an argument that’s not elegant and it’s not fun you could also write your function in a way that accepts uh associative arrays and then destructure the array into the you know constituent values i don’t like that method either so the way that this works now is you’re actually going to be able to provide named values for these uh for function calls so so saying something like number colon three means that the php engine is intelligent enough to know ah number three provide the default color of red that’s really awesome to me i think named arguments might be the coolest feature of php if it weren’t for the next feature that i think is just a little bit cooler the match expression the match expression is similar to a switch block except that the value of the match expression can actually be stored as a variable or returned from a function call it does strict comparisons rather than the weak comparisons we’ve talked about branches only support a single expression and the syntax looks very much like an associative array i’ve actually written a helper function for myself that actually basically works just like this but this is a much more elegant way of doing it this new expression will allow me to refactor so much of my code to make it simpler and easier to read in the documentation for these changes the switch code that the match expression was meant to replace is all too familiar i write so much php i read it i write it every day i actually really love php i know a lot of people out there hate it but i think it’s a great and fun language i i’m probably gonna end up using most of these new features and that means that i’m going to have to upgrade some of the stuff i’ve written so that it works with php 8. match expressions named arguments attributes these all should be game changers for the language.