Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

3.1 Nullable Types

Some types of data cannot be null (like ints, floats, etc.), but you might want to represent them as null. Now you can with PHP 7.1!

3.1 Nullable Types

7.1 one brings the concept of nullable types to PHP. Now nullable types are useful when ever you want to say that a particular type can be null especially if that type of value normally cannot be null. Like for example, write a function called isNullOrWhitespace? So if we pass in a string value that is null or it contains nothing but whitespace, then it's going to return true. Otherwise, it's going to return false. So let's write this as just pure dynamic code first. Let's have a null check for our first things so we will pass an R value if it is null then we will of course return true. And if it's not then we need to check to see if it is whitespace. And we can do that by getting the string length of a trimmed string. If that is equal to zero then we have a string of nothing but whitespace so we will return TRUE. Otherwise we return false. So let's do this we will var dump. We will call it is null or whitespace as pass in a string that is neither no whitspace and when ever we run this we will see the value of false. Just like we would expect. Okay, so let's make this all whitespace. We should see true now, we do. We can pass in an empty string which is going to be fine and we will have true there as well and then lets also pass in null. This will too return true and that's exactly what we expected. Okay, so one of the things that we aren't doing here is ensuring that we have a string value. So we could call is null a whitespace we could pass in an integer value and that would work. We see false there but we don't really want that. We want to be able to only pass strings to this function. Well we could do our type checking or we could use type-ins because that is what type-ins are for. So now that we are expecting a string, we can pass only strings to this function. Once again if we pass a normal string, it's false. If we pass in whitespace that's going to be true but if we pass in null, we are going to get an error because null is not a string. We can't do that. So this is where having a nullable type system would be beneficial because we could say that okay, we have a string or we are accepting a string. But this can also be a null string and we specify that with a question mark followed by our type. And now we can pass a string value or we can pass null and our code is going to work just fine. Now Visual Studio code thinks that this is a syntax error but it's not. If we run this code we see true so we were able to pass null to this function. It executes just fine and we get the results that we would expect. So we can use a nullable type for type and we can also use it with a return type declaration. So this is of course returning a Boolean value. But if we wanted to return a nullable Boolean value, we would use the same syntax, a question mark followed by the data type. Now in this particular case that doesn't make a whole lot of sense. Either the string is null or whitespace or it's not. There's no room there for returning null. But if you have a function where it's possible that you could return null, you could do so now using a return type declaration and nullable types.

Back to the top