loyx Posted March 26, 2009 Share Posted March 26, 2009 Hey, I'm having trouble when trying to use a variable in a variable name. I get the error Parse error: syntax error, unexpected T_VARIABLE, expecting ']' when trying to do this: if ($_POST[item_{$x}_num_of_supplies] != '') ... The $x is a variable for a loop counter. Every time the loop runs I want it to access a different variable, like this: $_POST[item_1_num_of_supplies] $_POST[item_2_num_of_supplies] $_POST[item_3_num_of_supplies] $_POST[item_4_num_of_supplies] etc etc I tried putting the variables in single quotes and double quotes, and using the braces in different places but nothing seems to work, producing various errors. Does anyone know a solution? Link to comment https://forums.phpfreaks.com/topic/151299-variable-within-variable-name-unexpected-t_variable-expecting/ Share on other sites More sharing options...
mentalist Posted March 26, 2009 Share Posted March 26, 2009 Might need to see more code, but have you tried: if ($_POST['Item_{$x}_num_of_supplies'] != '') or if ($_POST['Item_'.$x.'_num_of_supplies'] != '') Link to comment https://forums.phpfreaks.com/topic/151299-variable-within-variable-name-unexpected-t_variable-expecting/#findComment-794764 Share on other sites More sharing options...
chmpdog Posted March 27, 2009 Share Posted March 27, 2009 it might work if you removed the "item_x_num_suplies" try having just the variable: "x" so like this $_POST[$X] that will work Link to comment https://forums.phpfreaks.com/topic/151299-variable-within-variable-name-unexpected-t_variable-expecting/#findComment-794829 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.