An operator simply put is something that takes one or multiple expressions and results in a value. PHP offers quite a bit of operators and we're going to cover most of them in this video. If an operator takes only a single value it's called a unary operator and an example of that is this operator right here and we're going to talk about it in a minute.
If an operator takes two values it's called a binary operator and most of PHP's operators are binary operators because they expect two operands. If an operator requires three values it's called a ternary operator and an example of that is this operator right here and we're going to talk about that in a minute as well. So let's review these operators one by one.
We're going to start with the arithmetic operators. Arithmetic operators allow you to perform common arithmetic operations such as addition, subtraction, multiplication, division, modules and exponentiation. So for example if we had two variables x equals to 10 and y equals to 2 we could perform these arithmetic operations on them.
So for example we could do our dump x plus y and this will return 12. We could do subtraction and this will return 8. We can do multiplication and this will return 20. We could do division and this will return 5. We could do modules and it will return 0 and then we could do exponentiation which just takes x to the power of y and we'll get 100. Also as a note these operations would work with negative numbers as well. So if you had negative 10 here and you multiplied x by y you would get negative 20. If you did subtraction you would get negative 12. If you had negative 2 instead of negative 10 then in this case you would just get 12. Let's go over some of the tips and notes about arithmetic operators. So the first tip is about plus and minus operators.
You could use them to prefix a value or a variable and it will essentially just convert that variable to either an integer or float. So for example if you had 10 as a string and you did var dump x, the data type is string. But if you prefix this with a plus then this number will be cast to an integer. The same applies for negative numbers so if you put minus in here you will get converted to an integer data type. Same would work if you did it right by the value so this would also get converted to an integer.
The second tip is about division. So let's change this back to 10 and let's do x divided by y and we get 5. The type of the result will always be float unless both of the operands are integers and they're evenly divisible. However, if they were not evenly divisible, something like 10 divided by 3, the result will be float data type.
And also, if they were evenly divisible, but one of them was a float, the result will also be float. Another note about division is that if you divide by 0, your application will stop running and you will get an error. However, if you're working prior to PHP 8, instead of fatal error, you will probably get a warning. and it will still return a value but the value will be infinity.
To avoid this and if you still want to get that infinity as a value when you're dividing by zero you could use a function that was introduced in PHP 8 and that's called fdiv. So you could do fdiv past both arguments x and y and now this will return in infinity and it's not throwing an error. All right the next tip is about the modulus operator.
So as you probably know the modulus is just the remainder of x divided by y. So in this case when 10 is divided by 2 it's evenly divisible and there is no remainder so we're getting 0. However if we were to divide 10 by 3, 10 modules 3 will return 1. If we change this to 6 the remainder of 10 divided by 6 will be 4 and the value is 4. Now the note here is that both operands are cast into integers. So for example 10 mod 2 returns 0 and also 10.5 mod 2.9 will also return 0 because both of these are cast into integers and when you're casting flows to integers the decimal parts are just removed.
So it's essentially doing same thing as 10 mod 2 and you're getting 0. If you want to perform mod operation on floating numbers then you could use a function called fmod and pass both arguments like this and this will give you the proper result. Another thing to know about the mod operator is that when you're working with the negative numbers the sign of the result is taken from the number that you're dividing. So for example, if we had 10 mod 3, the result is 1. If we did negative 3 here, the result is still 1. And that's because the sign is taken from the number you're dividing. And in this case, we're dividing x by y and x is 10, which is positive number.
That's why we're getting positive as the result. If we, however, did negative 10 mod 3, then the result would be negative 1. And with that said, let's move on to the assignment operators. We've already covered the basic assignment operator which is just the equal sign so if we had x equals to 5 this is the assignment operator here which basically just assigns the value from the right to the left and in this case it assigns the value of 5 to the variable x.
Now don't confuse this with the comparison operators like double equal or the triple equal because these things do the comparison while this does the assignment and we'll talk about the comparisons in a minute but just keep in mind that these are different and I have seen sometimes developers make that mistake when they do if condition something like if x equals 5 and they think that this is comparing if x is 5 while it's actually just assigning x to 5 and then checking if the expression is true or not and in this case because x is 5 the expression evaluates to true and this will pass even if x is not 5. You could also assign multiple variables at the same time so you could do x equals y equals 10 and this would work and both x and y would equal equal to 10. So if we did var dump x and y we get 10 for both of them. You could also do more complex assignments something like x equals y equals 10 and then plus 5. And what this will do is that it will create a variable y with the value of 10 and this evaluates to 10 and you're basically just saying that x equals to 10 plus 5 and x will be 15. So if we var dump this we're getting 15 and 10. While this is possible I do not recommend you do this kind of stuff because it just makes code less readable. I'm just showing it to you because in case you come across it in someone else's code, you will know what it is.
A slightly more advanced assignment operator is called combined operators, which essentially is just a shorthand for the arithmetic or the string operator. So for example if you had something like x equals to 5 and then later in the code you wanted to add 3 to it or multiply by 3 for example, you could do something like x equals x times 3 and then echo x and this will work perfectly fine you will get 15 but there is a shorthand version of this and that is by just prefixing equal sign with your arithmetic operator so now we can just get rid of the x and multiplication from here and it will still work this will apply to other arithmetic operators as well you can do division and that will work you could do exponentiation it will also work subtraction and addition and so on one thing to note here is that if you never define x and you try to do the shorthand version right now it's trying to do x equals x plus 3 but x does not exist yet so you will get undefined variable warning and the value will still be 3 but you will get this warning. Another note about the assignments is that assignment by default is done by value and not by reference which means that it copies the value of the original variable to the new one.
Let's move on to string operators and you have already seen these operators from the previous videos. This is the concatenation operator and the concatenation operator combined with the assignment. So you could have x equals to hello and then you could have x equals to x concatenated with space and then world and then if you echo x this prints hello world. Now you could simplify this and remove this from here entirely and just prefix the assignment with the concatenation operator and this will still work.
Comparison operators allow you to compare two values to each other. For example you could have x equals to 5 and y equals to 3 and then you could do bar dump x equals y. and this will return true if x equals to y otherwise it will return false and in this case it will return false.
Now there is another way to compare and that is called strict comparison by using triple equal sign and this will also return false in this case and the difference between two is that this does something called loose comparison where it does the type conversion for you while the triple equal comparison is called a strict comparison where it also checks the data type of value. So for example if you had 5 and 5, both of them would return true because 5 is equal to 5 and they also have the same data type. However, if I changed one of them to string, now the first one will return true and the second one will return false.
And if we refresh, we see that that's what's printed. And the reason for that is because even though 5 is equal to 5, the data types of the two variables are different. This is an integer and this is a string.
To check for inequality, you would combine the equal sign with exclamation point. So you would have exclamation point and then equals and the reason it looks different on mine is because of the fonts on my IDE so when I combine the exclamation point and equal sign it looks like this so this will return true if x does not equal to y while php handles the type conversion for you if you wanted to do the strict comparison then it would remove one equal sign and replace it with the exclamation point and it does the same thing using strict comparison so doing this on the above values the first one will return false while the second one will return true and if we refresh we get false and true Also there is another way to check for loose inequality and that is by using this operator here. and it's pretty much same thing as this. Then we have less than, greater than, less than or equal to, and greater than or equal to operators.
And these work as you would expect them to work. And if we refresh we get the correct results. We also have a spaceship operator which basically combines less than, equal and greater than sign.
So you could do x spaceship y and this will return 0 if x is equal to y, it will return negative 1 if x is less than y and it will return 1 if x is greater than y. So in this case, 5 is less than 10, so we're going to get negative 1. If we change this to 50, then we're going to get 1. And if we change this to 10, we're going to get 0. Before I move on to the other two operators here, I want to point out a few important things. For example, before PHP 8, when string was compared to a number, the string was converted to number before the comparison, and then it was compared.
So for example, if we had something like var dump 0 equal to hello, before PHP 8, in PHP 7.4 for example, this would be converted to number and when this was converted to number it would result in zero and therefore zero equals zero would return true. But in PHP 8 this is no longer converted to number. Instead what happens is that when the string is not numeric the other side will be converted to string and then a string comparison will happen. So for example in this case in PHP 8 zero gets converted to string and then just a regular string comparison happens and zero does not equal to hello.
and therefore it returns false. So if we refresh the page we get false. Now I want to show you the exact same thing in PHP 7.4 so I'm going to open a php sandbox and we are at 7.4.
Let's copy this out and put it here. So as you can see in PHP 7.4 we're getting true while in PHP 8 we're getting false. I'm going to leave a link to the RFC that actually fixed this issue in PHP 8 if you want to know more about it.
Again if you did 0 as an integer equal to 0 as a string this would return true even in PHP 8 because if string is numeric then the string gets converted to a number and then the number comparison happens and in this case 0 equals 0 will return true. So in general I'm a big fan of strict types and strict comparisons so I highly recommend using strict comparison when possible to avoid potential issues. Let me show you an example where strict comparison would be very important. So for example if we have X equal to hello world and Y equals to str pose and this basically will just look for the letter H and return its position or index in the string and then if we did something like if Y equals false H not found let's echo that out otherwise we can echo out H found at index Y.
If we refresh, we're getting H not found, even though H is the first letter in the sentence. The reason for that is because this is returning the index 0, and this is essentially doing 0 equals false, and 0 gets converted to Boolean, and 0 equals false in a loose comparison actually returns true, and therefore H not found is printed. If we did strict comparison here, then H found that index 0 would be printed, and if we refresh, we get the correct result. All right, so let's move on to these two operators. And these two operators are what's called conditional operators.
This right here is a ternary operator. It's just a shorthand of this. For example, you could do the same thing using the ternary operator. The reason it's called ternary operator because it expects three values.
So you have the first expression which needs to evaluate either true or false. And based on its value, if it's true, the second expression gets evaluated. And if it's false, the third expression gets evaluated. And we can get rid of this.
and we get the exact same result. So this expression gets evaluated only if this expression is true otherwise this expression gets evaluated. You could also stack the ternary operators but just to point it out when you're stacking ternary operators always use parentheses for two reasons.
One not using parentheses has been deprecated in PHP 7.4 and it would result in errors in PHP 8. And second it is more readable when you have parentheses. The other conditional operator is called the null coalescing operator and it is mainly used when working with nulls. So for example if we had x equals null and then if we did var dump y, y will actually equal to hello.
So to simply explain this the variable y will equal to hello only if variable x is null. Otherwise if variable x is not null then y will equal to whatever the value of x is. Otherwise if we change x to something other than null, even if we change this to false, when we refresh the page the value will be false because that's the value of variable x. Null coalescing operator can also be useful when working with undefined variables or undefined array keys. For example if we never defined variable x the value of x is actually null and we would otherwise get undefined variable x warning.
We're not going to get the error, instead it will evaluate to hello. So if you refresh the page the variable Y is equal to hello. I don't want to make this video too long so I'm going to split this video into two videos and in the next video we're going to cover the rest of the operators and I'm also going to leave some useful links in the description so please check them out if you're interested. Thank you so much for watching, please hit like and subscribe and I will see you in the next video.