Jesdisciple Posted November 3, 2007 Share Posted November 3, 2007 First, I have a question about the scopes of variables. In the below code, I tried to use var as I would in JavaScript, including inside functions, but got "Parse error: syntax error, unexpected T_VAR" for each instance of that. Now, some variables are originally declared in the form $name;, because I want their scopes to reach the levels of those statements. Do all variables declared in a function have the whole function as their scope, or do I need to use this declaration form?Problem:I'm trying to write a function that will recursively interpret the text in a cookie as several key-value pairs, but the script always outputs "Fatal error: Maximum execution time of 30 seconds exceeded in C:\wamp\www\cookies\cookies.php [approx.] on line 75". (BTW, I am accessing the page via localhost.) Am I supposed to make it more efficient, is there an infinite loop, or what? EDIT: My do...while is looping infinitely; I'm using echo to debug, as I didn't know to do before.Implementation:Each key-value pair should be stored in the output array as both an Association object (see the bottom of cookies.php) at the default index and an association (i.e., $array['key'] == 'value') if the key is defined. Also, "[key1=value1;value2;(...)]" should be interpreted as a sub-array (and a value), with unlimited depth.test.php <?php function COOKIE($name){ if(isset($name) && !isnull($name)){ return parseArray($_COOKIE[$name]); }else{ return NULL; } } /** * array parseArray(string $string) * * Returns an interpretation of $string as a numerical, associative, and multidimensional array, ignoring newline * characters (but not other whitespace). For example, * parseArray("sampleValue; laughs=[hah; lol;rofl ;];ages=[Charles=30;Nina=24;Debbie=3;];") will return * (to be determined upon proper execution) */ function parseArray($string){ $string = preg_replace('/\n/', '', $string); $results; $index = 0; $semicolon; $open; $close; $assignment; for($i0 = 0; $i0 < strlen($string){ $semicolon = strpos($string, ';', $i0); $open = strpos($string, '[', $i0); $close = strpos($string, ']', $i0); $assignment = strpos($string, '=', $i0); $key = NULL; $value; if($close < $open){ throw new Exception("']' before '[' at string index $close"); } if($semicolon < $open || $open == -1){ $start; $end = $semicolon == -1 ? strlen($string) : $semicolon; if($assignment != -1 && $assignment < $semicolon){ $key = substr($string, $index + 1, $assignment); $start = $assignment + 1; }else{ $start = $index + 1; } $value = substr($string, $start, $end); $i0 = $semicolon + 1; }else{ $start = $openArray + 1; $end = getEndOfArray(substr($string, $start)); $value = parseArray(substr($string, $start, $end)); if($assignment != -1 && ($assignment < $open || $open == -1)){ $key = substr($string, $index, $assignment); } new Association($key, $value, $results); $i0 = strpos($string, ';', $end) + 1; } $index++; } return results; } function getEndOfArray($string){ $level = 0; $index = 0; $open; $close; do{ $open = strpos($string, '[', $index); $close = strpos($string, ']', $index); if($open < $close){ $level++; $index = $open; }else{ $level--; $index = $close; } }while($level > 0); if($level < 0){ throw new Exception("']' before '[' at string index $close"); } return $index; } class Association { var $key; var $value; public function Association($key, $value, $array){ $this->key = $key; $this->value = $value; if(isset($array)){ $array[] = $this; if(!isnull($key)){ $array[$key] = $value; } } } }?> (Also, how do you debug in PHP? Should I use echo like JavaScript's alert?) Link to comment Share on other sites More sharing options...
Jesdisciple Posted November 3, 2007 Author Share Posted November 3, 2007 EDIT: Never mind; I didn't understand how to debug PHP, will post back here if I get hopelessly stuck. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.