2

Here is my initial code:

    $camp_price=array(
   'option 1' => array(
                    'id' => 'June 30 to July 20',
                    'weeks' => 3,
                    'week_price' => 995),
   'option 2' => array(
                    'id' => 'June 30 to July 13',
                    'weeks'=> 2,
                    'week_price' => 995)
  ); 

 foreach ($camp_price as $name=>$values) {
$total_cost[$name]=$values['weeks'] * $values['week_price'];
}

Here is the HTML code. It echo perfectly the $total_cost until the form gets submitted. It then show the error:

Warning: Illegal string offset 'option 1'

Which I don't understand:

  <select name="preferred_session">
  <option value="option 1" <?php if($preferred_session =='option 1') echo'selected="selected"'; ?>>
 <?php echo $camp_price['option 1']['id'],': ', $total_cost['option 1']; ?>euros
 </option>
 <option value="2" <?php if($preferred_session =='option 2') echo 'selected="selected"'; ?>>
 <?php echo $camp_price['option 2']['id'],': ', $total_cost['option 2']; ?>euros
 </option>
</select>
hakre
  • 178,314
  • 47
  • 389
  • 754
user1794825
  • 113
  • 1
  • 8
  • 2
    Please `var_dump($total_cost);` and add it to your question. And the error message tells you a line number. The code of that line is most interesting, the rest is (very) not helpful. – hakre Dec 29 '12 at 22:09
  • Thanks hakre, I posted below the line code that changes the $total_cost to a string... hope you can help me figure out why. Thanks again – user1794825 Dec 30 '12 at 01:43

2 Answers2

3

Looks like, when the code triggering the error runs, either $camp_price or $total_cost is not an array but a string.

You may want to print the values of those variables just before the point where the error occurs, and look for any places where you might have unintentionally changed them.

Ilmari Karonen
  • 44,762
  • 9
  • 83
  • 142
  • 3
    Or, more likely, `$total_cost`. – Lightness Races in Orbit Dec 29 '12 at 22:12
  • Thanks for your response...by doing what you told me I identified the code that changes the variable $total_cost into a string: – user1794825 Dec 30 '12 at 01:32
  • `else if (empty ($errors)===false){ print_r($total_cost); foreach ($_POST as $key=>$value) { $$key = isset($value) ? $value : ''; } print_r($total_cost); echo output_errors($errors); }` – user1794825 Dec 30 '12 at 01:38
  • it prints returns the arry for the first print_r and a string for the second... but I can't understand why the foreach changes the $total_cost variable to a string! Thanks again and I hope you can help me figure that out. – user1794825 Dec 30 '12 at 01:40
  • Does `$_POST` contain a key named `total_cost`? If yes, the line `$$key = ...` will set `$total_cost` to the corresponding value. (BTW, doing that is a bad idea in general; there's a [reason](http://php.net/manual/en/security.globals.php) why [register_globals](http://php.net/manual/en/ini.core.php#ini.register-globals) and [import_request_variables()](http://php.net/import_request_variables) have been deprecated.) – Ilmari Karonen Dec 30 '12 at 01:45
  • Yes there is one... I feel very stupid now. Thanks a lot. Do you have any suggestion to replace such bad practice? The only purpose of this foreach loop is to create a variable that can be echoed in the field to prevent the user to retype it in case there are errors ex: Thanks again for your help! – user1794825 Dec 30 '12 at 02:27
  • You can just do `` instead. (Actually, what you _should_ do is ``.) – Ilmari Karonen Dec 30 '12 at 02:39
  • That makes sense; thanks again for your help! – user1794825 Dec 30 '12 at 04:05
1

Warning: Illegal string offset 'option 1'

This error means that you're using a string offset (here: 'option 1') which is illegal. String offsets must be a positive number, not a string.

This most often happens when you access a string variable but you think it is an array: (Demo)

<?php

$total_cost = 'string value';

echo $total_cost['option 1'];

Gives the error (and output):

Warning: Illegal string offset 'option 1' in /demo.php on line 5
s

The s at the end is $total_cost[0] because the illegal offset 'option 1' is used as 0 then. Following the rules of PHP's string substring accessDocs, this is the first character:

string value
s
^---- character 0: substr($total_cost, 0, 1);
                                       ^
hakre
  • 178,314
  • 47
  • 389
  • 754