Objective

Create a factorial() function to compute the factorial of a given integer.

Difficulty

Easy

Description

The factorial of an integer ::n:: is the product of all integers starting from that integer all the way down to ::1::.

In terms of a function, it could be expressed as follows:

::f(n) = n \times (n - 1) \times (n - 2) \times \dots \times 2 \times 1::

For instance:

::1! = 1::
::2! = 2 \times 1 = 2::
::3! = 3 \times 2 \times 1 = 6::
::4! = 4 \times 3 \times 2 \times 1 = 24::
::5! = 5 \times 4 \times 3 \times 2 \times 1 = 120::

::0!:: is a special case — it evaluates to ::1::.

In this exercise, you have to create a function factorial() that takes in an integer and returns its factorial.

The function MUST compute the factorial iteratively i.e. using a for (or maybe while) loop. In addition to this, if the given argument to factorial() is not an integer, the function should return the string 'Undefined'.

Shown below are a couple of examples of the function's usage:

<?php

echo factorial(10), "\n";
echo factorial(5), "\n";
echo factorial(2), "\n";
echo factorial(0), "\n";
echo factorial('0'), "\n";
3628800 120 2 1 Undefined