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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
oldsportbiker
Forum Newbie
Posts: 7
Joined: Wed Feb 24, 2010 10:21 pm

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

Post 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.
Last edited by Benjamin on Thu Feb 25, 2010 1:02 am, edited 1 time in total.
Reason: Added [code=php] tags.
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

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

Post 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?
oldsportbiker
Forum Newbie
Posts: 7
Joined: Wed Feb 24, 2010 10:21 pm

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

Post 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.
mrcoffee
Forum Commoner
Posts: 31
Joined: Tue Nov 10, 2009 3:03 pm
Location: Wyoming, USA

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

Post 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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

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

Post by Benjamin »

Please use

Code: Select all

tags guys.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

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

Post 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.
Post Reply