468,273 Members | 2,155 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,273 developers. It's quick & easy.

evaluate expression inside array

Hi,

I'm trying to assign values to an array by evaluating expressions like so:

$myArray = (
'sessionMaxInactive' =60*5,
'persistentMaxTime' =60*60*24*30
);

This doesn't seem to work. I've tried surrounding them with the brackets and
double quotes too, to no avail. Sould this be possible in php? If so, what
am I missing?

Thanks.
Aug 13 '06 #1
9 1683
On Sun, 13 Aug 2006 22:23:47 +0200, "amygdala" <no*****@noreply.comwrote:
>I'm trying to assign values to an array by evaluating expressions like so:

$myArray = (
'sessionMaxInactive' =60*5,
'persistentMaxTime' =60*60*24*30
);

This doesn't seem to work.
In what way does it "not seem to work"?

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Aug 13 '06 #2

"Andy Hassall" <an**@andyh.co.ukschreef in bericht
news:vs********************************@4ax.com...
On Sun, 13 Aug 2006 22:23:47 +0200, "amygdala" <no*****@noreply.com>
wrote:
>>I'm trying to assign values to an array by evaluating expressions like so:

$myArray = (
'sessionMaxInactive' =60*5,
'persistentMaxTime' =60*60*24*30
);

This doesn't seem to work.

In what way does it "not seem to work"?

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Hi Andy,

It gives me:

Parse error: parse error, unexpected '*', expecting ')'

Any ideas?

Cheers
Aug 13 '06 #3
I'm trying to assign values to an array by evaluating expressions like so:
>
$myArray = (
'sessionMaxInactive' =60*5,
'persistentMaxTime' =60*60*24*30
);

This doesn't seem to work. I've tried surrounding them with the brackets and
double quotes too, to no avail. Sould this be possible in php? If so, what
am I missing?
There is no chance to work for it. Simply, there is a syntax error. It
should be:
$myArray = array( 'sessionMaxInactive' =60*5, 'persistentMaxTime' =>
60*60*24*30 );

Aug 13 '06 #4
There is no chance to work for it. Simply, there is a syntax error. It
should be:
$myArray = array( 'sessionMaxInactive' =60*5, 'persistentMaxTime' =>
60*60*24*30 );
Yes you are right of course. Typo on my part in my message. But in my
original script I have that. Now I am confused though:

The full array is this (part of a session class):

private $config = array(
'sessionTable' =DB_TABLE_SESSIONS,
'userTable' =DB_TABLE_USERS,
'persistentMaxTime' =60*60*24*30,
'sessionMaxInactive' =60*30,
'regenerateId' =true,
'idRegenerationInterval' =10,
'cookieName' ='PHPSESSION',
'cookiePath' ='/',
'checkIp' =true,
'checkAgent' =true
);

which yields the error:

Parse error, parse error, unexpected '*', expecting ')' on line 49 (which is
the line of 'persistentMaxTime')

But this script seems to work fine indeed:

<?php
error_reporting( E_ALL );
$myArray = array( 'sessionMaxInactive' =60*5, 'persistentMaxTime' =>
60*60*24*30 );

echo $myArray[ 'sessionMaxInactive' ];
?>
Aug 13 '06 #5
"amygdala" <no*****@noreply.comwrote:
The full array is this (part of a session class):

private $config = array(
'sessionTable' =DB_TABLE_SESSIONS,
'userTable' =DB_TABLE_USERS,
'persistentMaxTime' =60*60*24*30,
'sessionMaxInactive' =60*30,
'regenerateId' =true,
'idRegenerationInterval' =10,
'cookieName' ='PHPSESSION',
'cookiePath' ='/',
'checkIp' =true,
'checkAgent' =true
);

which yields the error:

Parse error, parse error, unexpected '*', expecting ')' on line 49 (which is
the line of 'persistentMaxTime')
That's an invalid syntax: the default value of a class property must be
a static_scalar, that is a simple number, a string or an array of these
items. Other expressions are not allowed.
Here is a chunk from the EBNF syntax of PHP 5:

static_scalar = common_scalar
| T_STRING
| "+" static_scalar
| "-" static_scalar
| "array" "(" [static_array_pair_list] ")"
| static_class_constant ;

static_array_pair_list = static_array_pair { "," static_array_pair } [","] ;

static_array_pair = static_scalar ["=>" static_scalar] ;

Check the official BNF syntax Zend/zend_language_parser.y
(http://www.icosaedro.it/articoli/php-syntax-yacc.txt)
or the equivalent EBNF syntax translated by me from that file
(http://www.icosaedro.it/articoli/php-syntax-ebnf.txt)

So, expressions like "60*30" aren't allowed. Use "1800" instead.

Regards,
___
/_|_\ Umberto Salsi
\/_\/ www.icosaedro.it

Aug 13 '06 #6

"Umberto Salsi" <sa***@icosaedro.italiaschreef in bericht
news:eb**********@nnrp.ngi.it...
"amygdala" <no*****@noreply.comwrote:
>The full array is this (part of a session class):

private $config = array(
'sessionTable' =DB_TABLE_SESSIONS,
'userTable' =DB_TABLE_USERS,
'persistentMaxTime' =60*60*24*30,
'sessionMaxInactive' =60*30,
'regenerateId' =true,
'idRegenerationInterval' =10,
'cookieName' ='PHPSESSION',
'cookiePath' ='/',
'checkIp' =true,
'checkAgent' =true
);

which yields the error:

Parse error, parse error, unexpected '*', expecting ')' on line 49 (which
is
the line of 'persistentMaxTime')

That's an invalid syntax: the default value of a class property must be
a static_scalar, that is a simple number, a string or an array of these
items. Other expressions are not allowed.

Clear! Thanks.

Here is a chunk from the EBNF syntax of PHP 5:

static_scalar = common_scalar
| T_STRING
| "+" static_scalar
| "-" static_scalar
| "array" "(" [static_array_pair_list] ")"
| static_class_constant ;

static_array_pair_list = static_array_pair { "," static_array_pair } [","]
;

static_array_pair = static_scalar ["=>" static_scalar] ;

Check the official BNF syntax Zend/zend_language_parser.y
(http://www.icosaedro.it/articoli/php-syntax-yacc.txt)
or the equivalent EBNF syntax translated by me from that file
(http://www.icosaedro.it/articoli/php-syntax-ebnf.txt)
Eehh, thanks for the suggestions, but I think I'll pass trying to grasp
what's going on in those files ;)
Aug 13 '06 #7
On Sun, 13 Aug 2006 22:23:47 +0200, amygdala wrote:
f so, what
am I missing?
Keyword "array".

--
http://www.mgogala.com

Aug 13 '06 #8
Hi,
>
I'm trying to assign values to an array by evaluating expressions like so:

$myArray = (
'sessionMaxInactive' =60*5,
'persistentMaxTime' =60*60*24*30
);

This doesn't seem to work. I've tried surrounding them with the brackets and
double quotes too, to no avail. Sould this be possible in php? If so, what
am I missing?

this wont work either inside a class. Seems like a serious problem to
me.

class foo {
private $xyz = 2 * 3;
}

Aug 14 '06 #9
this wont work either inside a class. Seems like a serious problem to
me.

class foo {
private $xyz = 2 * 3;
}
Yeah seems pretty odd to me too. I wonder what the reason for this approach
is.
Aug 14 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by John Spiegel | last post: by
reply views Thread by SubasreeG | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.