string manipulation

string manipulation

am 04.09.2007 01:43:11 von ross.oneill

Hi,

Basically I want to do a subtract operation then append the result to
a string. When I execute the script below my output it "-1". I want
my output to be "this is a test 4" Any ideas what I am doing wrong?
Thanks a bunch
-R


$str = "this is a test" . (int)5-1;

echo $str;

?>

Re: string manipulation

am 04.09.2007 02:00:12 von ivansanchez-alg

ross.oneill@gmail.com wrote:

> [...] Any ideas what I am doing wrong?

You're forgetting that both . and - have the same precedence, and are
left-associative operators.

> $str = "this is a test" . (int)5-1;

That means that you're running this:

$str = ( "this is a test" . (int)5 ) -1;

Put in type juggling, and the result is that the string is eval'ed as (int
0, so you output 0 - 1 = -1.

But you do need to use parenthesis to overcome the precendence:


$str = "this is a test" . ( 5 - 1 );

or even

$str = "this is a test" . (int) ( 5 - 1 );


--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

La sabiduría me persigue, pero yo soy más rápido