473,387 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Default array as parameter in function

I'd like to have a default array as a function parameter.

I can do this:

function my_function($MY_ARRAY = array('1'=>'one')){...

But not this:

function my_function($MY_ARRAY = $_POST){...

I get:
Parse error: syntax error, unexpected T_VARIABLE

Why is that, and what is the solution? I can think of a hack...

Jeff
Aug 31 '08 #1
6 2078
Jeff wrote:
I'd like to have a default array as a function parameter.

I can do this:

function my_function($MY_ARRAY = array('1'=>'one')){...

But not this:

function my_function($MY_ARRAY = $_POST){...

I get:
Parse error: syntax error, unexpected T_VARIABLE

Why is that, and what is the solution? I can think of a hack...

Jeff
The default value must be a constant. So, just

function my_function ($my_array = null) {
if ($my_array == null)
$my_array = $_POST;

BTW - conventionally, names in all upper case are indicate constants. I
know PHP itself doesn't follow this practice - but it's a good convention.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Aug 31 '08 #2
Jerry Stuckle wrote:
Jeff wrote:
> I'd like to have a default array as a function parameter.

I can do this:

function my_function($MY_ARRAY = array('1'=>'one')){...

But not this:

function my_function($MY_ARRAY = $_POST){...

I get:
Parse error: syntax error, unexpected T_VARIABLE

Why is that, and what is the solution? I can think of a hack...

Jeff

The default value must be a constant. So, just

function my_function ($my_array = null) {
if ($my_array == null)
$my_array = $_POST;

BTW - conventionally, names in all upper case are indicate constants. I
know PHP itself doesn't follow this practice - but it's a good convention.
Thanks.

I've been naming my arrays in uppercase for readability. I asked about
this before and there doesn't seem to be a naming convention for arrays.
In perl, it would be @this_is_an_array. I suppose I'll have to append
"_array" after all of them...

I'd like to stick to the accepted php naming conventions, but I can't
really figure them out!

Jeff
Aug 31 '08 #3
On 31 Aug, 04:45, Jeff <jeff@spam_me_not.comwrote:
Jerry Stuckle wrote:
Jeff wrote:
I'd like to have a default array as a function parameter.
I can do this:
function my_function($MY_ARRAY = array('1'=>'one')){...
But not this:
function my_function($MY_ARRAY = $_POST){...
I get:
Parse error: syntax error, unexpected T_VARIABLE
Why is that, and what is the solution? I can think of a hack...
Jeff
The default value must be a constant. So, just
function my_function ($my_array = null) {
if ($my_array == null)
$my_array = $_POST;
BTW - conventionally, names in all upper case are indicate constants. I
know PHP itself doesn't follow this practice - but it's a good convention.

Thanks.

I've been naming my arrays in uppercase for readability. I asked about
this before and there doesn't seem to be a naming convention for arrays.
In perl, it would be @this_is_an_array. I suppose I'll have to append
"_array" after all of them...

I'd like to stick to the accepted php naming conventions, but I can't
really figure them out!

Jeff
The conventions are just that: conventions. Using Hungarian notation
is something Microsoft programmers do.

It's always a good idea to work with established standards though -
PEAR is probably the most prevalent (http://pear.php.net/manual/en/
standards.php)

C.
Aug 31 '08 #4
Jeff wrote:
Jerry Stuckle wrote:
>Jeff wrote:
>> I'd like to have a default array as a function parameter.

I can do this:

function my_function($MY_ARRAY = array('1'=>'one')){...

But not this:

function my_function($MY_ARRAY = $_POST){...

I get:
Parse error: syntax error, unexpected T_VARIABLE

Why is that, and what is the solution? I can think of a hack...

Jeff

The default value must be a constant. So, just

function my_function ($my_array = null) {
if ($my_array == null)
$my_array = $_POST;

BTW - conventionally, names in all upper case are indicate constants.
I know PHP itself doesn't follow this practice - but it's a good
convention.
Thanks.

I've been naming my arrays in uppercase for readability. I asked about
this before and there doesn't seem to be a naming convention for arrays.
In perl, it would be @this_is_an_array. I suppose I'll have to append
"_array" after all of them...

I'd like to stick to the accepted php naming conventions, but I can't
really figure them out!

Jeff
It's pretty loose, but the most common I've seen: Constants in all upper
case (i.e. MYCONSTANT). Variables in lower or mixed case with the first
letter uncapitalized, i.e. myvariable or myVariable. Class names in
mixed case with the first letter capitalized (i.e. MyClass).

As Jeff indicated, hungarian notation is not generally used in PHP
(although it is NOT a Microsoft creation - it was around long before
ever Microsoft existed). Type names are generally not appended to the
variable name.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Aug 31 '08 #5
C. (http://symcbean.blogspot.com/) wrote:
On 31 Aug, 04:45, Jeff <jeff@spam_me_not.comwrote:
>Jerry Stuckle wrote:
>>Jeff wrote:
I'd like to have a default array as a function parameter.
I can do this:
function my_function($MY_ARRAY = array('1'=>'one')){...
But not this:
function my_function($MY_ARRAY = $_POST){...
I get:
Parse error: syntax error, unexpected T_VARIABLE
Why is that, and what is the solution? I can think of a hack...
Jeff
The default value must be a constant. So, just
function my_function ($my_array = null) {
if ($my_array == null)
$my_array = $_POST;
BTW - conventionally, names in all upper case are indicate constants. I
know PHP itself doesn't follow this practice - but it's a good convention.
Thanks.

I've been naming my arrays in uppercase for readability. I asked about
this before and there doesn't seem to be a naming convention for arrays.
In perl, it would be @this_is_an_array. I suppose I'll have to append
"_array" after all of them...

I'd like to stick to the accepted php naming conventions, but I can't
really figure them out!

Jeff

The conventions are just that: conventions. Using Hungarian notation
is something Microsoft programmers do.
Thanks. My formal programming instruction dates to Fortran, which I
despised at the time. I'm at a disadvantage in formal training.

I actually had never heard of hungarian notation and have never
programmed in any of the MS languages. I have a perl background which
I'm still shaking off, php is wordy by comparison.
>
It's always a good idea to work with established standards though -
PEAR is probably the most prevalent (http://pear.php.net/manual/en/
standards.php)
I'll adapt to that. I hadn't seen it. It is a bit skimpy though.

I'm still a bit unsure what to do with arrays.

Jeff
>
C.
Aug 31 '08 #6
..oO(Jeff)
I'd like to stick to the accepted php naming conventions, but I can't
really figure them out!
This is how I do it in all my projects:

$someCoolVariable
someCoolFunction()
TSomeCoolClass
ISomeCoolInterface
SOME_COOL_CONSTANT

Micha
Aug 31 '08 #7

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

Similar topics

16
by: Steven T. Hatton | last post by:
As far as I know, there is no way to provide a default value for the argument to the constructor A::A(char (&array)) in this example. Correct? struct A{ A(char (&array) ){...
5
by: wilson | last post by:
Dear all, In this time, I want to pass array to function. What should I declare the parameter in the function?i int array or int array? Which one is correct? ...
11
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
8
by: Michel Rouzic | last post by:
I had a program that worked perfectly, and that read .wav files. I changed something so the tags of the wave file, instead of being each in a different variable, are all in an array, called tag. i...
2
by: Tobias Olbort | last post by:
Hello, i've a outer function, which takes a params-array as a parameter. I want to pass this array to inner function with a params-array (e. g. string.format). When i've passed an integer to...
8
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to...
2
by: Urs van Binsbergen | last post by:
Hi! This sounds probably quite silly, but I just can't figure out this one. Consider the following which lets "a" be an array with 0 elements: Dim a() As Integer a = New Integer() {} Now...
26
by: aruna.mysore | last post by:
Hi all, I have a specific problem passing a function pointer array as a parameter to a function. I am trying to use a function which takes a function pointer array as an argument. I am too sure...
2
by: Clyde | last post by:
Hi, what i'm trying to do is: /////////////// Code Start template <class TType, int* p = 0> class Template { public:
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.