Jump to content

php fuction for multiplying numbers

Recommended Posts

Is there any function in php lib that does the following

$a = 1238;
$n = 48; // 1*2*3*8

or I will have to create one for my script?

(I want to do it for both client and server sides based on request sent by user. I actually want it in php that is why i asked it here, but if the solution (as function) is available in javascript (not jQuery) please share or guide me.)

Edited by 684425
Link to post
Share on other sites

PHP has array_product(). EG

echo array_product([1, 2, 3, 8]);       // 48

If you want to be able to supply separate arguments to a function instead of an array EG

echo mul(1, 2, 3, 2, 4);

then you can define the function as

function mul(...$a)
    return array_product($a);

echo mul(3,8,2);      //48
echo mul(1,2,3,2,2,2) //48


Edited by Barand
extend reply
Link to post
Share on other sites

Adding onto Barand's reply, here's the technique to ingest the original integer and turn it into an array of digits.


function mul(int $int) {
    return array_product(str_split($int));

echo mul(12345);
// 120


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.