Connecting Tech Pros Worldwide Forums | Help | Site Map

evaluate expression inside array

amygdala
Guest
 
Posts: n/a
#1: Aug 13 '06
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.



Andy Hassall
Guest
 
Posts: n/a
#2: Aug 13 '06

re: evaluate expression inside array


On Sun, 13 Aug 2006 22:23:47 +0200, "amygdala" <noreply@noreply.comwrote:
Quote:
>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 :: andy@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
amygdala
Guest
 
Posts: n/a
#3: Aug 13 '06

re: evaluate expression inside array



"Andy Hassall" <andy@andyh.co.ukschreef in bericht
news:vs2vd2puvg64d1dcnuhekra2ci9l3e3g59@4ax.com...
Quote:
On Sun, 13 Aug 2006 22:23:47 +0200, "amygdala" <noreply@noreply.com>
wrote:
>
Quote:
>>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 :: andy@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


Mateusz Markowski
Guest
 
Posts: n/a
#4: Aug 13 '06

re: evaluate expression inside array


I'm trying to assign values to an array by evaluating expressions like so:
Quote:
>
$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 );

amygdala
Guest
 
Posts: n/a
#5: Aug 13 '06

re: evaluate expression inside array


There is no chance to work for it. Simply, there is a syntax error. It
Quote:
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' ];
?>


Umberto Salsi
Guest
 
Posts: n/a
#6: Aug 13 '06

re: evaluate expression inside array


"amygdala" <noreply@noreply.comwrote:
Quote:
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

amygdala
Guest
 
Posts: n/a
#7: Aug 13 '06

re: evaluate expression inside array



"Umberto Salsi" <salsi@icosaedro.italiaschreef in bericht
news:ebo5tm$tb6$1@nnrp.ngi.it...
Quote:
"amygdala" <noreply@noreply.comwrote:
>
Quote:
>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.

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


Mladen Gogala
Guest
 
Posts: n/a
#8: Aug 13 '06

re: evaluate expression inside array


On Sun, 13 Aug 2006 22:23:47 +0200, amygdala wrote:
Quote:
f so, what
am I missing?
Keyword "array".

--
http://www.mgogala.com

ImOk
Guest
 
Posts: n/a
#9: Aug 14 '06

re: evaluate expression inside array


Hi,
Quote:
>
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;
}

amygdala
Guest
 
Posts: n/a
#10: Aug 14 '06

re: evaluate expression inside array


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


Closed Thread