Q:

Write a PHP program to check a positive integer and return true if it contains a number 2

0

Write a PHP program to check a positive integer and return true if it contains a number 2.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

<?php
function test($n)
    { 
      while ($n > 0)
       {
          if ($n % 10 == 2) return true;
           $n /= 10;
        }
    return false;
    }

var_dump(test(123));
var_dump(test(13));
var_dump(test(222));

Sample Output:

bool(true)
bool(false)
bool(true)

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now