Programming Questions

  • Newest
  • Popular Tags
  • Ask Question
  • Simple Symbols in PHP
    <?php function SimpleSymbols($str) { // code goes here $str = str_split($str); for ($i = 0; $i < sizeof($str); $i++) { if (ctype_alpha($str[$i])) { if ($str[$i + 1] == "+" && $str[$i - 1] == "+") { return "true"; } else { return "false"; } } } } // keep this function call here // to see how to enter arguments in PHP scroll down echo SimpleSymbols(fgets(fopen('php://stdin', 'r'))); ?> Just wondering if anyone can give me some pointers on how to maybe clean up this code. I was shooting in the dark on this one, but I managed to come up with a correct solution.
    aluzor posted this question on 11/8/14 | symbol, php
    Answers
  • +
  • 3
  • -
  • What if your first or the last character in the string is a character. Won't it throw an exception?
  • +
  • 0
  • -
  • If I've read this correctly, the function returns true on the first instance of an alphabetic character with a plus immediately on either side. Seems efficiant enough, but as the commenter above said, what happens if the first or last character are alphabetic? Maybe nest another "if": if ([$i > 0 && $i < sizeof($str) { }
    Log in to write an answer.