PHP Basics

Handling Null Values & Safe Function Calls

Maximiliano Firtman

Maximiliano Firtman

Independent Consultant
PHP Basics

Check out a free preview of the full PHP Basics course

The "Handling Null Values & Safe Function Calls" Lesson is part of the full, PHP Basics course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano begins building an API endpoint for the Crypto Converter project. This endpoint can be used by an existing application or a client-side JavaScript application. Two new operators for handling null values and safe function calls are also introduced.

Preview
Close

Transcript from the "Handling Null Values & Safe Function Calls" Lesson

[00:00:00]
>> Maximiliano Firtman: What if we wanna create an API now? So now, we are also mobile developers. So we have a native mobile app that wanna talk to our API, or we are a React developer. I wanna do a client-side version of the app. We are not going to do the client-side version, but we can maybe want to offer the API.

[00:00:21]
Okay, and this case is an API that is consuming other API, but anyway, what's an API from a PHP point of view? It's just another PHP file. So we can call this api.php, okay, that is going to be php, that it's going to use the same CryptoConverter class.

[00:00:45]
Maybe we wanna get the value, let's say the code from GET now instead of from post, just to do something different. Let's call it code, and then, we can say maybe we receive the amount or maybe not we use 1, doesn't matter, and then we need to call CryptoConverter.

[00:01:09]
How do you call CryptoConverter from here? Should I create a converter like that? New CryptoConverter, will it work? What do I need? Why is it complaining?
>> Speaker 1: You need to include it.
>> Maximiliano Firtman: I need to include it. So I can include the file directly. Remember, we already created a classes.php that will include all the rest that we want.

[00:01:33]
Now it's complaining because it needs a code as an argument, okay? So, and then, it's an API, so I just wanna return a JSON. Can I do just this? I will return a JSON that has the rate, the current rate, and the current rate will be, actually, we need to get the value, the rate of Bitcoin or let's say rate in USD to understand better what I'm talking about.

[00:02:07]
It's a converter and we will say I wanna convert, and I won't pass any argument, will give me an error, but maybe we can do is create a 1 as a default value. So if I don't parse a value it's 1, 1 Bitcoin. Does it make sense? So that's the default value.

[00:02:24]
You can add spaces or not, it's up to you. So now I can just say I wanna adjust the rate and the rate will be rate in USD, I closed this, like that, I think it's fine. I'm writing a JSON manually, actually, I don't need to do that.

[00:02:43]
I can also create an object and then parse it with JSON decode, okay, but the chat will show you something different. It's just that. So, pretty simple. Okay, a couple of lines. I'm not done yet, so if you're writing, give me a sec. So, if I try this, I'm getting error here.

[00:03:05]
Line 4, it says, you don't have a code. Yeah, you're right. I didn't parse the code in the GET. What if we want a default 1? So, if you don't parse a code, I want bitcoin.
>> Maximiliano Firtman: Okay, I will show you two ways to do that. Do you understand what I want you to do?

[00:03:27]
First, I can check if,
>> Maximiliano Firtman: The code is not set, so I will check if it's set or if it's not set, wherever you prefer, there. I will say that code = BTC. By the way, if you pay attention, I'm using a lot of double quotes. I don't need them there, I can use single quotes.

[00:03:52]
Okay, so remember that, if not, I will use the other thing. Okay, it's complaining. Why it's complaining? Let's see if someone realized quickly what's going on. It's complaining here.
>> Maximiliano Firtman: What? It's a simple answer but yeah I went-
>> Speaker 2: Semicolon?
>> Maximiliano Firtman: Semicolon, yeah, I'm missing the semicolon. Now remember, okay, so this is one way, okay?

[00:04:23]
It's okay, it's fine, but I will show you a better one. So now I will refresh, and now it's giving me the right, BTC. If I add another code, ETH, it's giving me a different rate. Now, it works, okay, pretty cool. I mean, it's fine, it's working but this is kind of old code.

[00:04:49]
We have better operators these days to work with nulls and optionals. There is a new operator, this is new in PHP, by the way, it's also available in JavaScript, also these days. You can say, you know what? I wanna get the code from here, but if there is no code there, I wanna default one.

[00:05:13]
And for that, you use this operator, double question mark. So you don't need the, if.
>> Maximiliano Firtman: I mean, it's the same, but it's maybe cleaner. So this will actually check. It will use this value, unless it's null, and if it's null, is gonna use this one.
>> Maximiliano Firtman: That's the idea, okay?

[00:05:40]
Okay, I think it's cool.
>> Maximiliano Firtman: Also, what if you,
>> Maximiliano Firtman: Let's say this converter is not there and you're getting this converter. I mean, I'm showing you this quickly. We're not going to try it, just for you to understand something. We are getting the converter for somewhere else, okay?

[00:06:02]
Someone else, another file, I don't know where it's giving me a converter, but what if it's null? If it's null, I cannot execute a method on a null, it's going to give me an error, an exception. Well, now there is a way to do this.
>> Maximiliano Firtman: It's ?->, that means if we have an object here call this method, if not just continue or it will return no, okay, but it will not generate an exception.

[00:06:37]
This is called the safe call operator.
>> Maximiliano Firtman: Okay, so how mine is there. So for those of you complaining about PHP because you need to write a lot of code, check about these new operators that we have because probably it has to do with that. So a few years ago we didn't have this operator, so yeah you have to make a lot of if all the time, okay?

[00:06:59]
Well now you have these operators that are better.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now