2021-01-20, 07:29 PM
(This post was last modified: 2021-01-20, 09:54 PM by RhaegarX.
Edit Reason: Forum PHP Code Bug
)
"Sorry for the bad English, I'm using the translator to do this tutorial."
- What is a ternary operator?
In various situations in programming we need to deal with various scenarios, through conditional structures, such as if, else and switch. However, in some situations we deal with wave situations there are only 2 possible returns.
For example, let's imagine a hypothetical situation, in which if the player is an administrator he wins $ 1500, otherwise he will win $ 500.
We can do this logic using the if and else operator:
Now notice that we are making a relatively long structure for a simple action, with only two possibilities. Is there no other way to write this code in a simpler way? Yes, there is, and that is what the ternary operator is for:
Now we did the same action with just one line of code, however let's understand a little bit about how to build a ternary operator.
- How to build a ternary operator?
Building a ternary operator is simple, its own structure is self-explanatory.
To better understand, we can see that the '?' acts as an if and the ':' acts as the else.
- In what situations will I use the ternary operator?
This is a relative question, it depends a lot on the type of logic that you will build for your system, but as stated in the first question we will use the ternary operator for situations in which there are only 2 possibilities of return. Remembering that the ternary operator is not limited to returning only number values, but can also return strings and other options.
Example of a string with a ternary operator:
- Problems with ternary operator
The main problem with the ternary operator is its structure, which because it is very compact, its structure can be difficult to read by programmers who are not used to it.
- End
This is my first personal authoring tutorial, i hope you like it and criticism will always be welcome!
- What is a ternary operator?
In various situations in programming we need to deal with various scenarios, through conditional structures, such as if, else and switch. However, in some situations we deal with wave situations there are only 2 possible returns.
For example, let's imagine a hypothetical situation, in which if the player is an administrator he wins $ 1500, otherwise he will win $ 500.
We can do this logic using the if and else operator:
PHP Code:
if (PlayerData[playerid][pAdmin] > 0)
{? ? ? ? ?
? ? GivePlayerMoney(playerid, 1500);
}
else
{
? ? GivePlayerMoney(playerid, 500);
}
Now notice that we are making a relatively long structure for a simple action, with only two possibilities. Is there no other way to write this code in a simpler way? Yes, there is, and that is what the ternary operator is for:
PHP Code:
GivePlayerMoney(playerid, (PlayerData[playerid][pAdmin] > 0 ? 1500 : 500));
Now we did the same action with just one line of code, however let's understand a little bit about how to build a ternary operator.
- How to build a ternary operator?
Building a ternary operator is simple, its own structure is self-explanatory.
PHP Code:
(condition ? value if true : value if false)
To better understand, we can see that the '?' acts as an if and the ':' acts as the else.
- In what situations will I use the ternary operator?
This is a relative question, it depends a lot on the type of logic that you will build for your system, but as stated in the first question we will use the ternary operator for situations in which there are only 2 possibilities of return. Remembering that the ternary operator is not limited to returning only number values, but can also return strings and other options.
Example of a string with a ternary operator:
PHP Code:
SendClientMessage(playerid, -1, (playerData[playerid][pAdmin] > 0 ? "True" : "false"));
- Problems with ternary operator
The main problem with the ternary operator is its structure, which because it is very compact, its structure can be difficult to read by programmers who are not used to it.
- End
This is my first personal authoring tutorial, i hope you like it and criticism will always be welcome!