Optional function variable[Solved]

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
AGISB
Forum Contributor
Posts: 422
Joined: Fri Jul 09, 2004 1:23 am

Optional function variable[Solved]

Post by AGISB »

I want something like

Code: Select all

function test ($var) {
}
but var should be optional. if I call text() it gives a required argument error.

Is there a way to archive this?
Last edited by AGISB on Sat Mar 05, 2005 2:07 am, edited 1 time in total.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

You need to set a default value such as...

Code: Select all

function test ($var="") {
  //function body
}
Optional vars should be placed after required arguments, not mixed.

You could also use
:arrow: http://www.php.net/manual/en/function.func-num-args.php and
:arrow: http://www.php.net/manual/en/function.func-get-args.php


feyd | ;)
Post Reply