Page 1 of 1

Puzzled by function parameter passing and nulls...help!

Posted: Wed Feb 24, 2010 10:39 pm
by oldsportbiker
I'm trying to pass 4 parameter values to function next_image as follows:
Calling module

Code: Select all

 
<?php $user_sort="rating";
         $user_sortdirection="DESC";?> (these are the second 2 parameters, the first 2 are set elsewhere)
 
 <?php while (next_image(false, $firstPageImages, $user_sort, $user_sortdirection)): ?>
 
---------------------------------------------------------------------------------------

Called function

Code: Select all

 
function next_image($all=false, $firstPageCount=NULL, $sorttype=null, $sortdirection=NULL, $overridePassword=false) 
{
global $_zp_images, $_zp_current_image, $_zp_current_album, $_zp_page, $_zp_current_image_restore, $_zp_conf_vars, $_zp_current_search, $_zp_gallery;
        
     echo "sorttype=".$sorttype; (This outputs null, not my passed value of "rating")
 
----------------------------------------------------------------------------------------

Does the $sorttype=null in the called function override my passed non-null value? This function gets called from other modules which will not be passing in a paramater value so I'm hesitant to remove the "=null" in the parameter list.

Re: Puzzled by function parameter passing and nulls...help!

Posted: Wed Feb 24, 2010 11:06 pm
by mikosiko
oldsportbiker wrote: <?php while (next_image(false, $firstPageImages, $user_sort, $user_sortdirection)): ?>
.
do you have a : or a ; at the end of that sentence?

Re: Puzzled by function parameter passing and nulls...help!

Posted: Wed Feb 24, 2010 11:34 pm
by oldsportbiker
mikosiko wrote:
oldsportbiker wrote: <?php while (next_image(false, $firstPageImages, $user_sort, $user_sortdirection)): ?>
.
do you have a : or a ; at the end of that sentence?
It's a colon not semicolon. The call is working in the sense that I'm getting images back, (this is ZenPhoto code). The other While /end While loops also have a colon at the end as well.

Re: Puzzled by function parameter passing and nulls...help!

Posted: Thu Feb 25, 2010 12:54 am
by mrcoffee
The code you provided looks fine to me.

I would start troubleshooting by using var_dump($user_sort) just before calling the function, and var_dump($sorttype) at the very beginning of the function to see if it's perhaps being changed somewhere else.

Re: Puzzled by function parameter passing and nulls...help!

Posted: Thu Feb 25, 2010 1:02 am
by Benjamin
Please use

Code: Select all

tags guys.

Re: Puzzled by function parameter passing and nulls...help!

Posted: Thu Feb 25, 2010 1:41 am
by McInfo
oldsportbiker wrote:Does the $sorttype=null in the called function override my passed non-null value?
In the function definition's $sorttype=null, null is a default argument. Any value that you pass to a function takes precedence over the default value. In your case, "rating" overrides null.
oldsportbiker wrote:This function gets called from other modules which will not be passing in a paramater value so I'm hesitant to remove the "=null" in the parameter list.
It's a bad practice to place parameters without defaults after parameters with defaults in the parameters list because that would force you to use placeholder values when calling the function, which negates the point of default arguments.

PHP Manual: Functions: Default Arguments

Edit: This post was recovered from search engine cache.