684425 0 Posted January 5 Share Posted January 5 (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 by 684425 Quote Link to post Share on other sites
gw1500se 62 Posted January 5 Share Posted January 5 Not directly but writing your own is pretty simple in both PHP and Javascript. Quote Link to post Share on other sites
Barand 1,632 Posted January 5 Share Posted January 5 (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 by Barand extend reply Quote Link to post Share on other sites
gizmola 218 Posted January 5 Share Posted January 5 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 post Share on other sites
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.