I'm trying to code a function that will do some mathematical operations. Here's an example:
function test($number){
$result = 5 * $number[2];
echo $result;
}
So if I enter a number "12345", I want to multiply the number at index 2 with 5. So in this case, I should be multiplying 5*3 and get 15.
But I always get 0. I found the error why I'm always getting 0. It's because $number[2] isn't working. So how do I get a number at a certain index and then calculate with it?
The problem is that you can get the number by index only of string not of int.
test("124"); // working
test(124); // not working
Possible solution is this:
function test($number){
$number = "$number";
$result = 5 * $number[2];
echo $result;
}
test(124);