Page 1 of 1
Problem with optional function argument
Posted: Sun Jul 01, 2007 1:02 am
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
Posted: Sun Jul 01, 2007 1:13 am
by John Cartwright
you cannot use $_GET superglobal, or any exisiting variable for that matter as a default value of a function.
Posted: Sun Jul 01, 2007 1:14 am
by feyd
Default values cannot be unknown variables; they must be scalar.
is_scalar() may be of interest.
Posted: Sun Jul 01, 2007 5:03 am
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.
Posted: Sun Jul 01, 2007 5:27 am
by Oren
tapas_bahirbag is right.
PHP Manual wrote:Also PHP allows you to use arrays and special type NULL as default values
Posted: Sun Jul 01, 2007 5:36 am
by Benjamin
It's not scalar if it's not set.
Posted: Sun Jul 01, 2007 9:58 am
by toasty2
Alright, well thanks.
