0

I'm storing an array of comma separated values in my database in a column called line_services. The data in the column looks like 1,1,1,1,0,0,0 and I have a prepared statement where I bind the results of this column to the variable $line_services. In this prepared statement I have the following code after my fetch.

foreach(explode(',', $line_services) as $lineservices) {
    if ($lineservices == '1') {
        strtolower(str_replace('','_',$service_name.'_total')) = ((($service_tax / 100) * $service_cost) + 80.00) * $line_artwork_hours;  //1 hour
    }
    if ($lineservices == '2') {
        strtolower(str_replace('','_',$service_name.'_total')) = ((($service_tax / 100) * $material_cost) + $material_cost) * $line_product_sqft; //2 sq ft
    }    
    if ($lineservices == '3') {
        strtolower(str_replace('','_',$service_name.'_total')) = (($service_tax / 100) * ($line_L + $line_W)) + ($line_L + $line_W); //3 linear
    } 
    if ($lineservices == '4') {
        strtolower(str_replace('','_',$service_name.'_total')) = (($service_tax / 100) * (($line_L + $line_W) * 2) * .75) + (($line_L + $line_W) * 2) * .75; //4 linear
    } 
}

I'm getting the following error:

Can't use function return value in write context

When I do a var_dump on $line_services it is coming up NULL even though there is data in the column. The line_services column in the database is a varchar if that makes any difference.

3
  • please add the rest of your code for "When I do a var_dump on $line_services it is coming up NULL" Commented Oct 26, 2016 at 15:47
  • Did you give up or what??? Commented Oct 26, 2016 at 18:19
  • I didn't give up. Removed the strtolower and it worked. I didn't end up using variable variables just straight up arrays. Commented Oct 26, 2016 at 18:27

3 Answers 3

2

You are using an assignment to strtolower, and you can't do this. Check your code:

strtolower(str_replace(...)) = ...;

That doesn't make any sense. What are you trying to do? You are not assigning the result of (($service_tax / 100) * ($line_L + $line_W)) + ($line_L + $line_W) (for example) to any variable.

Sign up to request clarification or add additional context in comments.

4 Comments

All variables are defined in my prepared statement
The goal is to have the results of the math forumula get stored in the variable $service_name_total
this is a good point, I did not know that you can't use strtolower
I haven't said that you can't use strtolower, I have said that the way you were using it was wrong.
1

I assume you are trying for a variable variables? Rarely if ever a good idea, but you can use the curly syntax:

${strtolower(str_replace('','_',$service_name.'_total'))} = 'whatever'; 

Consider using an array instead:

$total[$service_name] = 'whatever';

1 Comment

didn't end up using the variable variable but the strtolower was causing the big issue. I used a straight up array. Thanks!
0

For starters, you are overwriting your variable in you foreach.

foreach(explode(',', $line_services) as $lineservices)

Switch this to:

foreach(explode(',', $line_services) as $lineservice)

Second of all, what are you expecting to happen here?

strtolower(str_replace('','_',$service_name.'_total')) = ((($service_tax / 100) * $service_cost) + 80.00) * $line_artwork_hours;

You are trying to assign a value to the strtolower function?

3 Comments

$line_services != $lineservices. There's no overwriting.
I have a service called Graphic Design for example so the purpose of strtolower and str_replace is to ensure that all variables come in as variable_name rather than Variable Name.
The goal is to have the results of the math formula get stored in the variable $service_name_total

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.