Superian Posted July 31, 2008 Share Posted July 31, 2008 Someone please explain the error that is occurring with in the script below. <?php // Parse error: syntax error, unexpected ',' $level = array('leve 1','level 2','level 3'), $level_index = 0; $i = 0; while ($i < 5) { print $level[$i].'<br>'; $i++; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/117613-simple-error/ Share on other sites More sharing options...
obsidian Posted July 31, 2008 Share Posted July 31, 2008 You have a comma after closing the array assignment: <?php // This: $level = array('leve 1','level 2','level 3'), $level_index = 0; // Needs to be this: $level = array('leve 1','level 2','level 3'); $level_index = 0; ?> Quote Link to comment https://forums.phpfreaks.com/topic/117613-simple-error/#findComment-604958 Share on other sites More sharing options...
ngreenwood6 Posted July 31, 2008 Share Posted July 31, 2008 well one error that I see is that you have left the L off of the end of level 1. can you give more detail on the error? Quote Link to comment https://forums.phpfreaks.com/topic/117613-simple-error/#findComment-604960 Share on other sites More sharing options...
Superian Posted July 31, 2008 Author Share Posted July 31, 2008 You have a comma after closing the array assignment: <?php // This: $level = array('leve 1','level 2','level 3'), $level_index = 0; // Needs to be this: $level = array('leve 1','level 2','level 3'); $level_index = 0; ?> I would like to declare $level_index after the array. Is there a way that this could be done? Thanks for the reply! Quote Link to comment https://forums.phpfreaks.com/topic/117613-simple-error/#findComment-604968 Share on other sites More sharing options...
obsidian Posted July 31, 2008 Share Posted July 31, 2008 I would like to declare $level_index after the array. Is there a way that this could be done? Thanks for the reply! Could you explain further what you mean by declare $level_index? My post shows how to define the variable after your array has been assigned. Quote Link to comment https://forums.phpfreaks.com/topic/117613-simple-error/#findComment-604982 Share on other sites More sharing options...
Trium918 Posted July 31, 2008 Share Posted July 31, 2008 The variable $level must be defined as static in order for your script to work properly. <?php // Parse error: syntax error, unexpected ',' static $level = array('level 1','level 2','level 3'), $level_index = 0; $i = 0; while ($i < 5) { print $level[$i].'<br>'; $i++; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/117613-simple-error/#findComment-604990 Share on other sites More sharing options...
obsidian Posted July 31, 2008 Share Posted July 31, 2008 The variable $level must be defined as static in order for your script to work properly. <?php // Parse error: syntax error, unexpected ',' static $level = array('level 1','level 2','level 3'), $level_index = 0; $i = 0; while ($i < 5) { print $level[$i].'<br>'; $i++; } ?> What? Why on earth would you need to define a variable as static for a simple script to parse? Also, what does that have to do with an unexpected comma? I'm confused. Quote Link to comment https://forums.phpfreaks.com/topic/117613-simple-error/#findComment-604995 Share on other sites More sharing options...
Superian Posted July 31, 2008 Author Share Posted July 31, 2008 It works! Thanks Trium918, but Obsidian has a point! Obsidian why is it a bad idea to to use static to parse my simple script! Quote Link to comment https://forums.phpfreaks.com/topic/117613-simple-error/#findComment-605010 Share on other sites More sharing options...
obsidian Posted July 31, 2008 Share Posted July 31, 2008 It works! Thanks Trium918, but Obsidian has a point! Obsidian why is it a bad idea to to use static to parse my simple script! I'm not saying it's a bad idea to use a static variable when there is a reason to, even in a simple script. I just can't figure out what the reason for one here is. Again, not saying it's wrong, but I just don't see the need for one... Quote Link to comment https://forums.phpfreaks.com/topic/117613-simple-error/#findComment-605020 Share on other sites More sharing options...
Superian Posted July 31, 2008 Author Share Posted July 31, 2008 It works! Thanks Trium918, but Obsidian has a point! Obsidian why is it a bad idea to to use static to parse my simple script! I'm not saying it's a bad idea to use a static variable when there is a reason to, even in a simple script. I just can't figure out what the reason for one here is. Again, not saying it's wrong, but I just don't see the need for one... I am using it to run the script below, so is there a better way of writing the script with out using the variable scope? <?php $level = 10; static $levels = array('level 1','level 2','level 3'), $level_index = 0; while ($level >= $level_index && isset($levels[$level_index])) { $current_level = levels[$level_index]; unset($levels[$level_index++]); } <? Quote Link to comment https://forums.phpfreaks.com/topic/117613-simple-error/#findComment-605083 Share on other sites More sharing options...
obsidian Posted August 1, 2008 Share Posted August 1, 2008 Why not just do this: <?php $levels = array('level 1', 'level 2', 'level 3'); foreach ($levels as $i => $level) { $current_level = $level; unset($levels[$i]); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/117613-simple-error/#findComment-605476 Share on other sites More sharing options...
DarkWater Posted August 1, 2008 Share Posted August 1, 2008 The variable $level must be defined as static in order for your script to work properly. <?php // Parse error: syntax error, unexpected ',' static $level = array('level 1','level 2','level 3'), $level_index = 0; $i = 0; while ($i < 5) { print $level[$i].'<br>'; $i++; } ?> Why in the world would you use static there? I think it's because the comma allows you to separate variables in one line like that with static. But that's not because of the fact that it IS static, it's because of the nature of the keyword. Remove the static and just replace the , with a ; >_> Quote Link to comment https://forums.phpfreaks.com/topic/117613-simple-error/#findComment-605480 Share on other sites More sharing options...
Superian Posted August 1, 2008 Author Share Posted August 1, 2008 Why not just do this: <?php $levels = array('level 1', 'level 2', 'level 3'); foreach ($levels as $i => $level) { $current_level = $level; unset($levels[$i]); } ?> This is what I was trying to do, but it isn't working the way that I want it to! Read the comment in the script below! <?php // A string describing a level to load. Each level adds to the // previous one, so invoking a later level automatically runs the earlier // level too. function load($level) { $levels = array('level 1', 'level 2', 'level 3'); foreach ($levels as $i => $level) { $current_level = $level; unset($levels[$i]); _load($current_level); } } function _load($level) { switch($level) { case 'level 1': print "Test Line 1"; break; case 'level 2': print "Test Line 2"; break; case 'level 3': print "Test Line 3"; break; } } // levels 2 and 3 shouldn't run because I only used level 1 as a param, // but if I used level 3 as the param then all should run! // Get my point? If I call level 3, then all level adds to // previous one, so invoking a later level automatically runs the earlier // level too. If I call level 2, then only level 2 and level one runs and not // level 3 load('level 1'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/117613-simple-error/#findComment-605545 Share on other sites More sharing options...
wildteen88 Posted August 1, 2008 Share Posted August 1, 2008 Chnage your load function to function load($level) { $levels = array('level 1', 'level 2', 'level 3'); $level_key = array_shift(array_keys($levels, $level)); foreach ($levels as $level) { if($i <= $level_key) { _load($level); } } } Quote Link to comment https://forums.phpfreaks.com/topic/117613-simple-error/#findComment-605566 Share on other sites More sharing options...
Trium918 Posted August 1, 2008 Share Posted August 1, 2008 It doesn't take all that! You don't have to change anything. Try this! <?php function load($level) { static $levels = array('level 1','level 2','level 3'), $level_index = 0; while ($level >= $level_index && isset($levels[$level_index])) { $current_level = $levels[$level_index]; unset($levels[$level_index++]); _load($current_level); } } function _load($level) { switch($level) { case 'level 1': print "Test Line 1"; break; case 'level 2': print "Test Line 2"; break; case 'level 3': print "Test Line 3"; break; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/117613-simple-error/#findComment-605569 Share on other sites More sharing options...
Superian Posted August 1, 2008 Author Share Posted August 1, 2008 It doesn't take all that! You don't have to change anything. Try this! <?php function load($level) { static $levels = array('level 1','level 2','level 3'), $level_index = 0; while ($level >= $level_index && isset($levels[$level_index])) { $current_level = $levels[$level_index]; unset($levels[$level_index++]); _load($current_level); } } function _load($level) { switch($level) { case 'level 1': print "Test Line 1"; break; case 'level 2': print "Test Line 2"; break; case 'level 3': print "Test Line 3"; break; } } ?> Trium918 this isn't working. Could someone please help me out? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/117613-simple-error/#findComment-605669 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.