php

How can I separate a number and get the first two digits in PHP?


How can I separate a number and get the first two digits in PHP?

For example: 1345 -> I want this output=> 13 or 1542 I want 15.


Solution

  • one possibility would be to use substr:

    echo substr($mynumber, 0, 2);
    

    EDIT:
    please not that, like hakre said, this will break for negative numbers or small numbers with decimal places. his solution is the better one, as he's doing some checks to avoid this.