Re: Re: HELP!! how can arithmetic variables(string) ??

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 10/18/07, Jochem Maas <jochem@xxxxxxxxxxxxx> wrote:
>
> Colin Guthrie wrote:
> > LKSunny wrote:
> >> <?
> >> $a="1+1"; //variables(string)
> >>
> >> //how can arithmetic variables(string) ??
> >> //N ROW................
> >>
> >> //output
> >> echo $a //i need output "2" not "1+1"
> >> //Please Help, Thank You Very Much !!
> >> ?>
> >>
> >
> > You can do:
> >
> > eval("echo $a;");
>
> you realise Sunny stopped reading at this point.
> some people should not be given guns. ;-)


a much safer technique is to restrict the options on the input and define a
custom function that
has expectations based upon those restrictions:

function addStringPieces($stringWAddition) {
    $addComponents = explode('+', $stringWAddition);
    return $addComponents[0] + $addComponents[1];
}
php > echo addStringPieces('1+1');
2

-nathan

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux