tags:

views:

33

answers:

2

Hi,

i have this code:

<?php var_dump(strpos($url, "cashgold.")+9) ?>

<?php var_dump(strpos($url, '/', 8)) ?>

<?php $resta = strpos($url, '/', 8) - strpos($url, "cashgold.")+9 ?>

<?php var_dump($resta) ?>

this prints:

20 22 20

I expected it prints :

20 22 2

Any idea?

Regards

Javi

A: 

Sorry i should write:

(strpos($url, "cashgold.")+9)
+2  A: 

I think you're having issues with maths - specifically with operator precedence.

When you're doing

<?php $resta = strpos($url, '/', 8) - strpos($url, "cashgold.")+9 ?>

you're doing (22 - 11) + 9. This is because + and - are the same precedence, so it's being evaluated left to right.

Try (note the brackets)

<?php $resta = strpos($url, '/', 8) - (strpos($url, "cashgold.")+9) ?>

to do the calculation you're after.

Deebster
+1 Well thought through
Mark Baker