Problem with optional function argument

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
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Problem with optional function argument

Post by toasty2 »

Here's line 38 of my code (I'm specifying an optional argument to my function):

Code: Select all

function page($p = $_GET['a'])
{
//stuff
}
PHP tells me:

Code: Select all

Parse error: syntax error, unexpected T_VARIABLE in C:\server\htdocs\index.php on line 38
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

you cannot use $_GET superglobal, or any exisiting variable for that matter as a default value of a function.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Default values cannot be unknown variables; they must be scalar.

is_scalar() may be of interest.
tapas_bahirbag
Forum Newbie
Posts: 14
Joined: Sun Aug 06, 2006 6:54 am
Contact:

Post by tapas_bahirbag »

they must be scalar.
This is not must, but [s]u[/s] you must need a known value.

Thanks,
Tapos Pal
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:11. Please use proper, complete spelling when posting in the forums. AOL Speak, leet speak and other abbreviated wording can confuse those that are trying to help you (or those that you are trying to help). Please keep in mind that there are many people from many countries that use our forums to read, post and learn. They do not always speak English as well as some of us, nor do they know these aberrant abbreviations. Therefore, use as few abbreviations as possible, especially when using such simple words.

Some examples of what not to do are ne1, any1 (anyone); u (you); ur (your or you're); 2 (to too); prolly (probably); afaik (as far as I know); etc.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

tapas_bahirbag is right.
PHP Manual wrote:Also PHP allows you to use arrays and special type NULL as default values
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

It's not scalar if it's not set.
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post by toasty2 »

Alright, well thanks. :(
Post Reply