Google
 
Webnews.only-4-geeks.com
Interesting places
news.only-4-geeks.com Forum Index » PHP

Default array as parameter in function

 
Jump to:  
 
Jeff
PostPosted: Sun Aug 31, 2008 1:24 am    Post subject: 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
 

 
Jerry Stuckle
PostPosted: Sun Aug 31, 2008 1:32 am    Post subject: Re: Default array as parameter in function
       
Jeff wrote:
Quote:
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.
jstucklex@attglobal.net
==================
 

 
Jeff
PostPosted: Sun Aug 31, 2008 1:45 am    Post subject: Re: Default array as parameter in function
       
Jerry Stuckle wrote:
Quote:
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
 

 
Jerry Stuckle
PostPosted: Sun Aug 31, 2008 10:53 am    Post subject: Re: Default array as parameter in function
       
Jeff wrote:
Quote:
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.
jstucklex@attglobal.net
==================
 

 
Jeff
PostPosted: Sun Aug 31, 2008 12:17 pm    Post subject: Re: Default array as parameter in function
       
C. (http://symcbean.blogspot.com/) wrote:
Quote:
On 31 Aug, 04:45, Jeff <jeff@spam_me_not.com> 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

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.
Quote:

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
Quote:

C.
 

 
C. (http://symcbean.blogs
PostPosted: Sun Aug 31, 2008 12:39 pm    Post subject: Re: Default array as parameter in function
       
On 31 Aug, 04:45, Jeff <jeff@spam_me_not.com> wrote:
Quote:
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.
 

 
Michael Fesser
PostPosted: Sun Aug 31, 2008 6:44 pm    Post subject: Re: Default array as parameter in function
       
..oO(Jeff)

Quote:
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
 

Page 1 of 1 .:.

Google
 
Webnews.only-4-geeks.com

Windows Update | C++ | C | PHP | JavaScript | Photoshop | Programming | Windows 2000 | Python | Windows XP | Object | Flash | Flash - ActionScript | Paint Shop Pro | Excel | PowerPoint | Access | Word | Windows 98 | Internet Explorer 6.0 | CorelDraw12 | Java | XML | asm x86 | Linux Mandrake | Linux RedHat | Outlook |  | news from newsgroups |_ | s

Web Templates

Awesome Website Templates ©

literatura noclegi w bieszczadach poezja wiersze klementyna zakłady bukmacherskie smsy urodzinowe