Jump to content

Simple Error!


Superian

Recommended Posts

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++;
}
?>

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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++;
}
?>

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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++]);
}
<?

Link to comment
Share on other sites

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 ; >_>

Link to comment
Share on other sites

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');
?>

Link to comment
Share on other sites

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;

}
}
?>

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.