string manipulation

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

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


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

echo $str;


Re: string manipulation wrote:

Quoted text here. Click to load it

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

Quoted text here. Click to load it

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

Site Timeline