phily245 Posted July 21, 2011 Share Posted July 21, 2011 Hi, I'm making a script that generates an array by using a while loop and a start and end date. The while loop will echo the array out perfectly, but I need it stored, not echoed. In an attempt to create the array, I made this script: <?php $dated = date("dmY", $date); $eventarray[]='array(' "month" => '" . dated["2"] . =$dated["3"] . "', "day" => '" . $dated["0"] . $dated["1"] . "', "year" => '" . =$dated["4"] . $dated["5"] . $dated["6"] . $dated["7"] . "' ); '; ?> However, when I run the script, I get the error message "unexpected T_CONSTANT_ENCAPSED_STRING". This is the array which it needs to put out when the start date 14-07-2011 and the enddate is 21-07-2011: array( "month" => 07, "day" => 14, "year" => 2011 ); array( "month" => 07, "day" => 15, "year" => 2011 ); array( "month" => 07, "day" => 16, "year" => 2011 ); array( "month" => 07, "day" => 17, "year" => 2011 ); array( "month" => 07, "day" => 18, "year" => 2011 ); array( "month" => 07, "day" => 19, "year" => 2011 ); array( "month" => 07, "day" => 20, "year" => 2011 ); array( "month" => 07, "day" => 21, "year" => 2011 ); Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/242534-unexpected-t_constant_encapsed_string/ Share on other sites More sharing options...
trq Posted July 21, 2011 Share Posted July 21, 2011 Look at the string your are creating. Single quotes within a single quotes string need to be ascaped. Quote Link to comment https://forums.phpfreaks.com/topic/242534-unexpected-t_constant_encapsed_string/#findComment-1245575 Share on other sites More sharing options...
phily245 Posted July 21, 2011 Author Share Posted July 21, 2011 Thanks for that. Now I have: unexpected '[' in /home/phil/public_html/admin/calendar_events.php on line 26 which refers to the same line. is it talking about: dated["2"] Quote Link to comment https://forums.phpfreaks.com/topic/242534-unexpected-t_constant_encapsed_string/#findComment-1245629 Share on other sites More sharing options...
Muddy_Funster Posted July 21, 2011 Share Posted July 21, 2011 could you post your revised code - it looks as though you have put the array() that comes after the = inside the single quotes Quote Link to comment https://forums.phpfreaks.com/topic/242534-unexpected-t_constant_encapsed_string/#findComment-1245652 Share on other sites More sharing options...
phily245 Posted July 22, 2011 Author Share Posted July 22, 2011 Here is the revised code: <?php $dated = date("dmY", $date); $eventarray[]='array(\' "month" => "' . dated["2"] . $dated["3"] . '", "day" => "' . $dated["0"] . $dated["1"] . '", "year" => "' . $dated["4"] . $dated["5"] . $dated["6"] . $dated["7"] . '" \'); '; ?> Quote Link to comment https://forums.phpfreaks.com/topic/242534-unexpected-t_constant_encapsed_string/#findComment-1246127 Share on other sites More sharing options...
phily245 Posted July 22, 2011 Author Share Posted July 22, 2011 I found what was wrong, I didn't need to put the entire array section in single quotes. Here is the new code: <?php $eventarray[]= array(\' "month" => "' . dated["2"] . $dated["3"] . '", "day" => "' . $dated["0"] . $dated["1"] . '", "year" => "' . $dated["4"] . $dated["5"] . $dated["6"] . $dated["7"] . '" \'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/242534-unexpected-t_constant_encapsed_string/#findComment-1246156 Share on other sites More sharing options...
trq Posted July 22, 2011 Share Posted July 22, 2011 If your trying to add an actual array to the $eventarray array your still going about it wrong. For example. $eventarray[]= array("month" => dated["2"] . $dated["3"], "day" => $dated["0"] . $dated["1"]); Sorry for the confusion but your code was so far off track I assumed you where trying to insert a string. Quote Link to comment https://forums.phpfreaks.com/topic/242534-unexpected-t_constant_encapsed_string/#findComment-1246160 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.