开发者

PHP: trying to substract the returning values from strpos()

开发者 https://www.devze.com 2023-01-18 23:49 出处:网络
i have开发者_开发知识库 this code: <?php var_dump(strpos($url, \"cashgold.\")+9) ?> <?php var_dump(strpos($url, \'/\', 8)) ?>

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


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.


Sorry i should write:

(strpos($url, "cashgold.")+9)
0

精彩评论

暂无评论...
验证码 换一张
取 消