2

From the php documentation i came across the following function:

string number_format ( float $number , int $decimals = 0 , 
                       string $dec_point = '.' , string $thousands_sep = ',' )

This function accepts either one, two, or four parameters (not three). When passed three arguments, the function generates a warning Warning: Wrong parameter count for number_format() on the line of function call.

From what I understand, any optional parameter should be totally optional. Also, php does not support function overloading so that we could have made two different functions to achieve this.

My questions are:

  1. Is it possible to somehow restrict the number of arguments as above in the function declaration itself (not within the function code)

  2. If not, and that the above function uses trigger_error() to generate the warning, how does the generated warning refer to the file and line from where this function is being called from. trigger_error() function seems to generate a warning / error on the line it is called.

1

1 Answer 1

0

The function just adds an additional check of whether you have 3 parameters or not.

If 3 parameters are sent, it generates a warning.

Similar to :

if(func_num_args() == 3) {
  trigger_error('Wrong parameter count for number_format()', E_USER_WARNING);
}

Reference: func_num_args() , trigger_error()

Sign up to request clarification or add additional context in comments.

4 Comments

But will it not generate the warning on a line within the library where this function is defined. In this case, the warning is shown on the line where the function is being called.
similar logic is defined in PHP parser. that's why PHP parser will raise Warning in function definition level.
I have updated the question. I want to understand that logic and whether it is possible to implement it in some of our own functions. The trigger_error raises the warning on the file / line it is used.
you can control the output buffer & show only the errors on the page , i.e. make use of ob_*() functions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.