473,583 Members | 3,567 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Function definition with optional parameters?

Some of the functions that are part of PHP have optional parameters. Is
it possible to define your own function so that it has optional
parameters? If so, how is that done?

Thanks,

Phester

vilefesteringsc um
at
yahoo

Jul 17 '05 #1
4 102436
On Sun, 28 Sep 2003 20:20:33 GMT, Phester <ph*****@nonexi stenthost.com> wrote:
Some of the functions that are part of PHP have optional parameters. Is
it possible to define your own function so that it has optional
parameters? If so, how is that done?


Certainly; you can give the optional parameters default values.

http://www.php.net/manual/en/functions.arguments.php

--
Andy Hassall (an**@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 17 '05 #2
Phester wrote:
Is
it possible to define your own function so that it has optional
parameters?
Yes.

If so, how is that done?


Define a function, giving the last parameters a default value

<?php
function xyz($a, $b, $c=0, $d='localhost') {
// do nothing :-)
}

### and then call it
xyz(0, 0, 3, 'yahoo.com');
xyz(0, 0, 4); // same as xyz(0, 0, 4, 'localhost');
xyz(0, 0); // same as xyz(0, 0, 0, 'localhost');
?>

--
I have a spam filter working.
To mail me include "urkxvq" (with or without the quotes)
in the subject line, or your mail will be ruthlessly discarded.
Jul 17 '05 #3
Andy Hassall wrote:
On Sun, 28 Sep 2003 20:20:33 GMT, Phester <ph*****@nonexi stenthost.com> wrote:
Some of the functions that are part of PHP have optional parameters. Is
it possible to define your own function so that it has optional
parameters? If so, how is that done?


Certainly; you can give the optional parameters default values.

http://www.php.net/manual/en/functions.arguments.php

--
Andy Hassall (an**@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)

Thanks!

Phester

Jul 17 '05 #4
Pedro wrote:
Phester wrote:
Is
it possible to define your own function so that it has optional
parameters?

....
Define a function, giving the last parameters a default value

<?php
function xyz($a, $b, $c=0, $d='localhost') {
// do nothing :-)
}

### and then call it
xyz(0, 0, 3, 'yahoo.com');
xyz(0, 0, 4); // same as xyz(0, 0, 4, 'localhost');
xyz(0, 0); // same as xyz(0, 0, 0, 'localhost');
?>

Thanks, that's exactly what's required.

Phester

Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
9496
by: Programmer | last post by:
This may sound easy and it probably is, I just started learning C++. I have a class that writes to a file and I would like to add a second optional parameter that will be used to tell the class if a newline character should be written so the next output will start on a new line. My class looks like this: class Debug { public: Debug();
2
20191
by: Frank Rizzo | last post by:
Does c# support optional parameters in function declarations? Does it support assignment of default values to arguments, in particular optional parameters? If so, can someone point me to or provide an example. For instance in vb6, you could do the following: function DoStuff(optional ID as integer = 10, optional Name as string = "test")...
0
1633
by: lakshmi | last post by:
include the following lines of code: using System.Runtime.InteropServices; prefix the optional argument with This takes care if an optional parameter is not supplied by a VB script client. >-----Original Message----- >I found that optional parameters are not supported in C#. >We are currently rewriting a C++ dll in C#. The C++ dll
16
4051
by: ad | last post by:
Does C#2.0 support optional parameters like VB.NET: Function MyFunction(Optional ByVal isCenter As Boolean = False)
3
4089
by: guy | last post by:
found this oddity- copying a vb6 function with optional parameters and pasting it into a vb2003 class, for conversion purposes, it all works fine except that the collapsing "-" on the LHS goes away, only to return if "Optional" is removed. Strange. anyone else had this? guy
14
3256
by: cody | last post by:
I got a similar idea a couple of months ago, but now this one will require no change to the clr, is relatively easy to implement and would be a great addition to C# 3.0 :) so here we go.. To make things simpler and better readable I'd make all default parameters named parameters so that you can decide for yourself which one to pass and...
12
2657
by: pamelafluente | last post by:
Hi guys, In the past I have used several time optional parameters in my function. But Now I am more inclined to think that they are more dangerous than useful, and probably better to be avoided. I'd like to hear your various opinions on this matter.
1
8407
by: peridian | last post by:
This is more of a general question, but I didn't know where to post it. Since Java is an example of a language which does this, I thought here would work. Coming from a C++ background, having used VBA/VB6 quite a bit, and a number of scripting languages with similar traits, like php, I had grown accustomed to optional parameters in my...
1
20797
by: Glenn | last post by:
I am writing a Web Service in C# ASP.NET 2.0 that takes two optional parameters. One is an int and the other is a string. Below is the definition of the Web Method: public List<ModelFamilyModelFamilySearch(Nullable<intid, string name) { MyBusinessObject mbo = new MyBusinessObject(); return mbo.Search(id, name);
0
7893
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7821
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8317
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7928
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5695
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5369
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2326
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1422
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1151
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.