By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,614 Members | 1,653 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,614 IT Pros & Developers. It's quick & easy.

Default array as parameter in function

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
..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 discussion thread is closed

Replies have been disabled for this discussion.