# Q. zend_parse_parameters() - allow grouping of optional parameters.

- Thread: https://externals.io/message/32718
- Original archive: https://news-web.php.net/php.internals/32718
- Started by Richard Quadling on 2007-10-10T07:38:01+00:00
- Messages: 3
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #32718 — Richard Quadling — 2007-10-10T07:38:01+00:00

URL: https://externals.io/message/32718#32718

Hi.

From what I understand about the argument list for
zend_parse_parameters(), you separate optional parameters from
mandatory ones using pipe (|).

e.g.

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lll|llll", &m,
&d, &y, &h, &n, &s, &ms) == FAILURE) {

In the above example, this says that there are 3 mandatory parameters
and upto 4 optional parameters.

The prototype for this looks like ...

proto bool myExampleDateFunc(int month, int day, int year, [int hour
[, int min [, int sec [,int msec]]]])

Is there a way to make the prototype ...

proto bool myExampleDateFunc(int month, int day, int year, [int hour,
int min, int sec[, int msec]])

So, making this function accept 3 or 6 or 7 parameters only.

I'm guessing not, but I'm guessing. I think this has to be checked using ...

switch (ZEND_NUM_ARGS()) {

after parsing the parameters and generate an error for ZEND_NUM_ARGS = 4 or 5.

Regards,

Richard.

-- 
-----
Richard Quadling
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
"Standing on the shoulders of some very clever giants!"

---

## #32719 — Scott MacVicar — 2007-10-10T09:55:20+00:00

URL: https://externals.io/message/32718#32719 · In reply to: #32718

if (ZEND_NUM_ARGS() != 3 && ZEND_NUM_ARGS() != 6 && ZEND_NUM_ARGS() != 7) {
	WRONG_PARAM_COUNT;
}

Will let you check that there are 3, 6 and 7 parameters.

Scott
Richard Quadling wrote:
> Hi.
> 
> From what I understand about the argument list for
> zend_parse_parameters(), you separate optional parameters from
> mandatory ones using pipe (|).
> 
> e.g.
> 
> if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lll|llll", &m,
> &d, &y, &h, &n, &s, &ms) == FAILURE) {
> 
> In the above example, this says that there are 3 mandatory parameters
> and upto 4 optional parameters.
> 
> The prototype for this looks like ...
> 
> proto bool myExampleDateFunc(int month, int day, int year, [int hour
> [, int min [, int sec [,int msec]]]])
> 
> Is there a way to make the prototype ...
> 
> proto bool myExampleDateFunc(int month, int day, int year, [int hour,
> int min, int sec[, int msec]])
> 
> So, making this function accept 3 or 6 or 7 parameters only.
> 
> I'm guessing not, but I'm guessing. I think this has to be checked using ...
> 
> switch (ZEND_NUM_ARGS()) {
> 
> after parsing the parameters and generate an error for ZEND_NUM_ARGS = 4 or 5.
> 
> Regards,
> 
> Richard.

---

## #32731 — Andrei Zmievski — 2007-10-10T18:17:35+00:00

URL: https://externals.io/message/32718#32731 · In reply to: #32719

Use quite parameter parsing:

if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS()  
TSRMLS_CC, "lll", &m, &d, &y) != SUCCESS &&
    zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS()  
TSRMLS_CC, "llllll", &m, &d, &y, &h, &min, &s) != SUCCESS &&
   zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS()  
TSRMLS_CC, "llllll", &m, &d, &y, &h, &min, &s, &ms) != SUCCESS) {
    php_error(E_WARNING, "Could not parse parameters");
}

Disadvantage is that you will not get the nicely formatted error  
message and have to do it yourself.

-Andrei
http://10fathoms.org/vu - daily photoblog



> if (ZEND_NUM_ARGS() != 3 && ZEND_NUM_ARGS() != 6 && ZEND_NUM_ARGS 
> () != 7) {
> 	WRONG_PARAM_COUNT;
> }
>
> Will let you check that there are 3, 6 and 7 parameters.
>
> Scott
> Richard Quadling wrote:
> > […]
