Hello everyone,

I'm new to PHP, so please go easy on me! :eek:

I am writing a web page for people to choose some items. One of the functions I have is to remove the "$" signs from prices, then multiply them times the number of items the person is asking for. Sounds simple, right? So I thought. I'm getting a "Fatal error: Can't use function return value in write context in..." error and I can't figure out why. Any assistance would be greatly appreciated.

Here is my function code:

//function that clears the "$" sign and gets the cost for the item
function item_cost($cost, $amount)
{
	$cost2 = preg_replace("/[^a-zA-Z0-9s.]/", "", $cost);  //removes the "$" sign
	$cost1 = (float)$cost2;                                //makes sure the variable is a number
	$amount1 = (float)$amount;                             //makes sure the variable is a number
	$x= $cost1 * $amount1;                                 //multiplies the amount ordered by the number ordered
	return $x;
}

Here is the line that calls the function:

$item_price($i)= item_cost($price[$i], $amount_ordered);

The line number the error gives me is the above function call.

Thanks everyone!!

    OK, I found it. All it took was me posting it for someone else to look at. I put () instead of [] for the array.

    Sorry to take up your time!

      just as a heads up... you really shouldnt use preg_replace() for removing the $ sign since the preg functions are for patterns and regular expressions... you really should use [man]str_replace()[/man]

        scrupul0us;10909211 wrote:

        just as a heads up... you really shouldnt use preg_replace() for removing the $ sign since the preg functions are for patterns and regular expressions... you really should use [man]str_replace()[/man]

        Thanks! I changed my code to use str_replace. I didn't even know that function was there!

        (Did I mention I'm a PHP newbie?? :rolleyes: )

        Thanks again

          no sweat! we welcome t3h n00bs and their desires to learn... I've been at it for almost 6 years and I still consider myself a noob to some degree

            Write a Reply...