684425 Posted January 5, 2021 Share Posted January 5, 2021 (edited) 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 January 5, 2021 by 684425 Quote Link to comment https://forums.phpfreaks.com/topic/311977-php-fuction-for-multiplying-numbers/ Share on other sites More sharing options...
gw1500se Posted January 5, 2021 Share Posted January 5, 2021 Not directly but writing your own is pretty simple in both PHP and Javascript. Quote Link to comment https://forums.phpfreaks.com/topic/311977-php-fuction-for-multiplying-numbers/#findComment-1583662 Share on other sites More sharing options...
Barand Posted January 5, 2021 Share Posted January 5, 2021 (edited) 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 January 5, 2021 by Barand extend reply Quote Link to comment https://forums.phpfreaks.com/topic/311977-php-fuction-for-multiplying-numbers/#findComment-1583663 Share on other sites More sharing options...
gizmola Posted January 5, 2021 Share Posted January 5, 2021 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 Quote Link to comment https://forums.phpfreaks.com/topic/311977-php-fuction-for-multiplying-numbers/#findComment-1583666 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.