"if" elegant in PHP

Asked

Viewed 159 times

2

(!file_exists($pathFize) ? $response = false : $response = true);

if ($response == true) {
//executa o código ok
}

I would like suggestions on how to do this more elegantly, remembering that all this is within a larger function that needs to return a $response = true or false according to the conditional response (if the file exists) that is nothing more than a security check.

For example, in the first $response = false I’d like to close the whole loop repeat and work on the error File not exist.

1 answer

8


The first attempt to make it more elegant:

if (file_exists($pathFize)) {

But if you are going to use this data many times then it is better (or the most correct way possible by doing so):

$response = file_exists($pathFize)

if ($response) {

If you want to do it right, and then it’s not a question of elegance, you should not check if the file exists, unless the logic is to actually determine whether the file exists or not, just this, then you can’t do it like this. If the intention is to access the file, access it and check if the access worked. If you could not access it for any reason treat the error. If you do what you’re trying then you might have a running condition (is not the focus explain here even why it may be just wanting to verify the existence even).

I did not analyze if the logic is correct because by the excerpt and the posted can not know.

One of the points that is more elegant is not comparing a variable that has boolean value with a boolean literal, because the result will always be equal to the variable, so it is using a redundant code, you can use the variable directly. The same goes for an expression that generates a boolean value.

But note that this only works if the value is guaranteed to be boolean. If there is the possibility of the value being another (PHP has dynamic and weak typing and may happen in some situations. In this case there is no problem, is boolean guaranteed, at least according to the documentation.

It makes no sense to use a negation operator in the expression (!file_exists($pathFize)) to get the inverted value of what you want, so when I used I just eliminated the operator, its code is doing a double inversion, therefore taking the original value.

And reinforcement that if you take a boobleano value (the return of file_exists() is a boolean according to the documentation) has no reason to check the value if what you want is a boolean, you already have this value, use it directly. If your example wanted it reversed, that is, if you want false when he returns true and vice versa there would make sense to use the operator ! to reverse, but it’s not even what you want in the example.

One last point is that it is quite odd to use a conditional operator (which some mistakenly call ternary) to execute a complex command, in general you should only generate values directly. Let’s think I’d like to generate a number for some reason, the elegant thing would be to do this:

$response = funcaoQueRetornaBool() ? 1 : 0;

I put in the Github for future reference.

Realized that the assignment is outside the operator and not inside as it did?

It works because a PHP assignment is a expression and not a statement pure (a statement accepts only declarations then gets confused) then the operator accepts the assignment, since it expects expressions. If the assignment was only statement would not accept.

I have explained this Boolean question in more detail in some places:

  • Perfect friend! I liked this last example, it can be a better way to get the expected result... Overall I only check if the file exists to know if I can delete it, it doesn’t make sense that I try to delete a file that doesn’t exist (until it would return me an error) I’ll even try to apply the way you described it with the "conditional operator" (and not ternary rs) I come back here to tell you the result. Thanks in advance for your complete reply

  • You are right... I just read your post about "racing conditions" I understood better about and today I can edit my code to avoid such a situation. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.