Day 16: Exceptions - String to Integer

  • + 5 comments

    Can someone show me example how to do this without conditional statements in PHP/JS/CoffeeScript? And if this is impossible (as I think) why are these languages in the list of possible languages for solutions?

    --- UPDATE ---

    Ok. PHP solution is next

    <?php
    
    $handle = fopen ("php://stdin","r");
    fscanf(`$handle,"%s",$`S);
    
    try {
        new ReflectionClass('ReflectionClass' . ((int)`$S . "" !== $`S));
        echo $S;
    } catch (Exception $e) {
        echo "Bad String";
    }
    
    ?>
    

    Thanks to http://stackoverflow.com/questions/36483800/php-string-to-integer-exception/36488040#36488040

    I'm sure this is not for novices, comparable to other languages solutions and clear (it still contains comparison) but it is how exception should be handled in PHP.