# How can I change this function so that I can pass an additional argument. It should multiply each value in the array

**URL:** <https://www.phphelp.com/t/how-can-i-change-this-function-so-that-i-can-pass-an-additional-argument-it-should-multiply-each-value-in-the-array/33319>\
**Category:** Beginners - Learning PHP\
**Created:** [April 9, 2021, 1:24pm UTC](https://www.phphelp.com/t/how-can-i-change-this-function-so-that-i-can-pass-an-additional-argument-it-should-multiply-each-value-in-the-array/33319 "2021-04-09T13:24:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![idontknowphp](https://www.phphelp.com/letter_avatar_proxy/v4/letter/i/c68b51/32.png) [@idontknowphp](https://www.phphelp.com/u/idontknowphp)\
**Post date:** [April 9, 2021, 1:24pm UTC](https://www.phphelp.com/t/how-can-i-change-this-function-so-that-i-can-pass-an-additional-argument-it-should-multiply-each-value-in-the-array/33319/1 "2021-04-09T13:24:33Z")

</div>

How can I change this function so that I can pass an additional argument. It should multiply each value in the array by this additional argument (‘factor’ inside the function).

For ex: $first = array(5,23,10,8,7). When you say…

$second = multiply($first, 3);  
var\_dump($second);  
this should dump $second which contains [15, 69, 30, 24, 21].

This is my code:

```auto
<?php

$first = array(5,23,10,8,7);

$second = array();

function show_array($first){

    echo '<b>The values inside the $first array are: </b><br>';

    foreach($first as $value){

       echo $value."<br>";

    }

}

function multiply($first){

    $second = array();

    

    echo "<b>The products multiplied by 5 are: </b><br>";

    $multiplier = 5;

    

    foreach($first as $value){

        $product = $value*$multiplier;

        echo $product."<br>";

        array_push($second,$product);       

    }

    echo '<br><b>The values inside the $second array are:<b>';

    var_dump ($second);      

}

echo show_array($first)."<br>";

$second = multiply($first);

?>
```

---

<div class="post-metadata">

**Author:** ![idontknowphp](https://www.phphelp.com/letter_avatar_proxy/v4/letter/i/c68b51/32.png) [@idontknowphp](https://www.phphelp.com/u/idontknowphp)\
**Post date:** [April 9, 2021, 1:27pm UTC](https://www.phphelp.com/t/how-can-i-change-this-function-so-that-i-can-pass-an-additional-argument-it-should-multiply-each-value-in-the-array/33319/2 "2021-04-09T13:27:40Z")

</div>

how can I make this post’s code appear readable?

---

<div class="post-metadata">

**Author:** ![phdr](https://www.phphelp.com/letter_avatar_proxy/v4/letter/p/91b2a8/32.png) [@phdr](https://www.phphelp.com/u/phdr)\
**Post date:** [April 9, 2021, 4:39pm UTC](https://www.phphelp.com/t/how-can-i-change-this-function-so-that-i-can-pass-an-additional-argument-it-should-multiply-each-value-in-the-array/33319/3 "2021-04-09T16:39:22Z")

</div>

Either use bbcode `[code][/code]` tags or three markdown back-ticks ``` before/after the lines of code. I have edited your post above to add bbcode tags.

---

<div class="post-metadata">

**Author:** ![whispers](https://www.phphelp.com/letter_avatar_proxy/v4/letter/w/858c86/32.png) [@whispers](https://www.phphelp.com/u/whispers)\
**Post date:** [April 10, 2021, 3:39am UTC](https://www.phphelp.com/t/how-can-i-change-this-function-so-that-i-can-pass-an-additional-argument-it-should-multiply-each-value-in-the-array/33319/4 "2021-04-10T03:39:36Z")

</div>

```auto
$first = array(5,23,10,8,7);
$second = array();

function show_array($first){
	echo '<b>The values inside the $first array are: </b><br>';
	foreach($first as $value){
	   echo $value."<br>";
	}
}

function multiply($first, $multiplier){
	$second = array();			

	echo "<b>The products multiplied by $multiplier are: </b><br>";
	foreach($first as $value){
		$product = $value*$multiplier;
		echo $product."<br>";
		array_push($second,$product); 
	}

	echo '<br><b>The values inside the $second array are:<b>';
	var_dump ($second);      
}

echo show_array($first)."<br>";
$second = multiply($first, 3);

```
