LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-25-2007, 07:42 AM   #1
jkobrien
Member
 
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507

Rep: Reputation: 30
PHP functions - default arguments


Hi,

If I have a PHP function with two arguments, both with defaults, how can I just pass the second argument and rely on the default value for the first?

Code:
function test($a=1,$b=2) {
  echo "$a $b  <br />";
}

test(12);      // prints 12 2 - as you'd expect

test(,5);      // causes a blank page

test(Null,10); // prints 10
test("",10);   // prints 10
Or is this just, for some reason, a silly thing to try to do? It isn't for any particular application. I'm just learning PHP and want to understand it properly.

Many thanks in advance,

John

Last edited by jkobrien; 01-25-2007 at 07:58 AM.
 
Old 01-25-2007, 05:04 PM   #2
AdaHacker
Member
 
Registered: Oct 2001
Location: Brockport, NY
Distribution: Kubuntu
Posts: 384

Rep: Reputation: 32
Can't do it. Parameter passing in PHP is purely positional and you can only omit parameters at the end of the list. If you want $a to have the default value of 1 in your example, you need to use test(1, 12).

There's nothing inherently silly about wanting to do this. Some languages do allow you to omit parameters at the beginning or middle of the list. Other allow you to pass parameters by name rather than by position, e.g. test(b=12). PHP, however, is not one of those languages.
 
Old 01-26-2007, 04:36 AM   #3
jkobrien
Member
 
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507

Original Poster
Rep: Reputation: 30
Thanks, AdaHacker! I like to understand these things properly.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Looking for a more effecient way to pass arguments to functions. RHLinuxGUY Programming 10 05-01-2006 10:52 PM
Pointers/Passing arguments to functions question (C) smoothdogg00 Programming 7 03-17-2006 01:46 PM
[bash-scripting]functions + arguments hylke Programming 14 10-05-2004 01:48 AM
PHP -- How to execute a shell script from PHP using FTP functions?? zoonalex Programming 3 07-29-2004 11:51 AM
Linked Lists: Pointers sent to functions as arguments do not change their valur Jose Muņiz Programming 3 01-12-2004 07:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration