简体   繁体   中英

How to convert a string in a number PHP

I have a Foreach where I have two variables ($num1 and $num2) as string. I'd like to covert them as number (integer should be ok).

This is my code:

    ...
    foreach ($titles as $match) {
    list($num1, $num2) = explode(':', $results[$c++]->innertext); // <- explode

    echo "<tr><td class='rtitle'>".
    "<td class='last-cell'>".$match_dates[$c]->innertext . "</td> " .
            //"<td class='first-cell tl'>".$match->innertext."</td> "  .   
            " - ".$match->innertext." ".$num1.':'.$num2 .  "  "   .     
            "<td class='odds'>".$best_bets[$b++]->attr['data-odd'] . ";" .
            "".$odds[$b++]->attr['data-odd'] . ";" .
            "".$odds[$b++]->attr['data-odd'] . "</td>" .

            "</td></tr><br/>";
}
...

Thanks!

EDIT
Thanks for your answer. I am going to explain and maybe you could help me: I have aa problem with best_bets value, because it's not alway before odds variable, so I was thinking to use "if statement", where I can fix some rules: when $num1 is > $num2 , I will show an echo order; when $num1 is = $num2 another echos order and. at least, $num1 is < $num2 another echos order. But to do this, I need to convert $num1 and $num2 to do it. I hope to be clear. Thank you for your helping

You could cast them to an int:

$num1 = (int) $num1;

Or use the intval function:

$num1 = intval($num1);

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM