PHP Syntax with ? Symbol: 3-in-1 Example from Laravel

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys today I want to show you a piece of beautiful code in PHP that I want to emphasize with a lot of question marks here in the function definition and in here and those question marks come from kind of newer PHP syntax which is not that new anymore but still I see a lot of developers don't use that so I want to emphasize that to make your code shorter instead of doing if statements or other checks and this comes from a free repository lateral repository by Pascal who released Eddie management as split demo app he's the author of laravel's Blade and he created a real application to manage servers open sourced it and I will link that in the description below and if you don't know what split is I have a full video course quite short 45 minutes of showcasing another way to create Spas with just blade syntax I will link that as well in the description below but topic of today is these question marks so kind of two less here the first part is nullable types so when defining the type for the variable parameter or return type you may add question mark which means that this parameter is optional and may have null as value this feature comes from 7.1 PHP 7.1 so this is the documentation and you can do this string and this string here for example to kind of tell other developers and PHP compiler that it may be no now what happens if you don't pass that question mark I have an experiment function in my Tinker well so if you have that as null and default value and if you just call that function without any parameters it returns no as it should so no error if you don't pass that question mark then actually nothing happens we refresh and still null because it has default value so with this syntax what it actually does is inform other developers in the future which may be yourself by the way that this variable can be null and it may be important for someone who would call that function in the future they would know that they may skip that parameter or assign null to it so this is not a syntax error but it would be a syntax error if you have something like this so if you don't pass the default value and you don't pass that as nullable and you call that function like this we refresh and we have an error of two few arguments and now if we do have that without the default value we're refresh and we still have an error so actually Pascal the author has the best solution here this is how I would do that with both default value and the nullable type but this is more to avoid syntax error and this is more to inform other developers who would work with that code in the future and then the second part the second topic of this video is these question marks these are called null say wave operators and they appeared quite recently in PHP 8.0 and they allow you to call some object and call the method of that object without checking that the object actually exists in that function in that job the task is an object but it may be null so what happens if you do pass null but you want to call the method from that object which may be no so this is where you add a question mark here or here two cases and then if that object is null PHP would not fire an error similarly here server created by user that user maybe no may be deleted or something we try to notify the user if they exist this null safe operator is well explained by brand who is a superstar of explaining new things in PHP So to avoid doing something like this you could do just something like this so you can even change those things with php8 and by the way if you haven't upgraded to php8 the earlier versions of PHP are not even officially supported so you should upgrade to php8 anyway and I will link that article in the description below as well and actually while shooting this video I have noticed the third question mark that I haven't even thought about so this one did you know this syntax it's a ternary operator So to avoid if statements so if that tail output is present then it would be used same thing if error message is present that would be used otherwise if it's empty it would default to empty string and this comes from a very old syntax I also found it on surprise surprise Brands blog it's called shorthand ternary operator so this is a shorter version of this so this is for true and this is for false but if you want to handle only false you can have a shorthand like this so are you using those newer syntax things in PHP and with those small syntax things it's a matter of habit so you use it once twice and then it becomes a habit in my opinion this code is really well written readable and it could be an example what do you think do you agree with me share your opinion in the comments below as usual that's it for this time and see you guys in other videos
Info
Channel: Laravel Daily
Views: 7,100
Rating: undefined out of 5
Keywords:
Id: Q4tCXwZakOc
Channel Id: undefined
Length: 5min 18sec (318 seconds)
Published: Thu Jun 22 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.