Correct way to use php’s abs function

Asked

Viewed 296 times

2

People what is the correct way to use this function?

first :

$limite = \abs($resultado_cadastro->limite);

2nd:

$limite = abs($resultado_cadastro->limite);

Both work, but netbeans says the correct one is \abs

4 answers

4


According to the official PHP documentation, both forms are valid.

What happens is that if the function you are using is not found in namespace current, there will be a fallback to the namespace global.

Tip: You can first use form to discriminate PHP’s own function if you write your own version of that function. For example:

function strlen($str)
{
    return \strlen($str) - 1;
}
  • You say there will be a fallback?

  • Without the slider, if the function does not exist in the current namespace. If there is the slider, you are forcing the use of the function present in the.

  • got it, good think better leave with the bar then ;) thanks

3

The two forms are correct, however one should use the bar before the name of the function or class when using namespaces this serves for all functions/classes of php core, from contario php will try to find a function called abs() in the current namespace and as there is no error, as in this question.

2

Use what is described in Manual for PHP so it will be guaranteed that regardless of the machine settings all your code will work correctly. Usually the " " is used when you are making use of a namespace or a class.

-2

Browser other questions tagged

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