pinkbits Posted November 16, 2008 Share Posted November 16, 2008 My php file is crapping itself whenever I've got this uncommented. I've played with commenting various pieces, and it's specifically the switch statement. I also tried removing all commands from the switch comparisons, and I think it's the switch statement itself, not the resulting commands on a positive comparison. Help as to why would be lovely thanks. function findColumnsAndRows() { $fh = file($systemFile); foreach ($fh as $line) { $temp = explode(" ", $line); if ($temp[0] == "columns") echo "FOUND COLUMNS <br />\n"; switch ($temp[0]) { case "columns" $columns = (int)$temp[2]; break case "rows" $rows = (int)$temp[2]; break case default break } } } Quote Link to comment https://forums.phpfreaks.com/topic/132931-comparing-results-of-an-explode-in-a-switch/ Share on other sites More sharing options...
trq Posted November 16, 2008 Share Posted November 16, 2008 Sorry, but It would seem you forgot to actually describe the problem. Quote Link to comment https://forums.phpfreaks.com/topic/132931-comparing-results-of-an-explode-in-a-switch/#findComment-691246 Share on other sites More sharing options...
pinkbits Posted November 16, 2008 Author Share Posted November 16, 2008 I'm not sure where I've not described the issue... I don't have that switch statement commented, and the page doesn't load anything, completely white. I've also already given any debugging information I have. The switch statement itself is causing the php to fail. The problem is: There seems to be an issue with the switch statement. Quote Link to comment https://forums.phpfreaks.com/topic/132931-comparing-results-of-an-explode-in-a-switch/#findComment-691250 Share on other sites More sharing options...
JasonLewis Posted November 16, 2008 Share Posted November 16, 2008 Um, well, it should technically be throwing an error at you. You're missing the colons from the end of the case's, and your missing the semi-colons from the end of the break's. Quote Link to comment https://forums.phpfreaks.com/topic/132931-comparing-results-of-an-explode-in-a-switch/#findComment-691252 Share on other sites More sharing options...
pinkbits Posted November 16, 2008 Author Share Posted November 16, 2008 Thanks. I'm a noob with php, so stupid stuff, while embarassing, doesn't surprise me that it's happening. I've made those changes, and tested, and still nothing. Any ideas? I can post the full file if you like? (It's only 50-odd lines atm) Quote Link to comment https://forums.phpfreaks.com/topic/132931-comparing-results-of-an-explode-in-a-switch/#findComment-691256 Share on other sites More sharing options...
JasonLewis Posted November 16, 2008 Share Posted November 16, 2008 From the looks of it you could use variable variables here (although it's probably a crappy way). That way you don't need the switch. ${$temp[0]} = $temp[2]; That will make a variable (either $columns or $rows, depending on what $temp[0] equals) and store $temp[2] in it. You could do it with arrays as well. Perhaps posting the full code will give us a better understanding of what it is you're trying to achieve. Quote Link to comment https://forums.phpfreaks.com/topic/132931-comparing-results-of-an-explode-in-a-switch/#findComment-691258 Share on other sites More sharing options...
pinkbits Posted November 16, 2008 Author Share Posted November 16, 2008 Ok, here's the full code. Basically what I'm trying to do is: [*]Open systemFile [*]Loop through the whole file, one line at a time [*]Explode each line using space as the delimiter [*]test the first item on each line ($temp[0]) to see if it's the columns or rows setting [*]If it's rows or columns, set the appropriate variable to the value contained in $temp[2] See below the main body of code for an example of the text found in $systemFile <?php #-------------------------------------------------------------------------------VARIABLES---------------------------------------------------------------------------- #Path to the Mugen installation (NO SLASH AT THE END) $mugenPath = 'E:\GAMES\MUGEN'; #Path to the various data files inside Mugen $selectFile = $mugenPath . '\DATA\select.def'; $systemFile = $mugenPath . '\DATA\system.def'; #find the columns and rows $columns = 0; $rows = 0; #-------------------------------------------------------------------------------END VARIABLES------------------------------------------------------------------------ #-------------------------------------------------------------------------------FUNCTIONS---------------------------------------------------------------------------- function findColumnsAndRows() { $fh = file($systemFile); foreach ($fh as $line) { $temp = explode(" ", $line); if ($temp[0] == "columns") echo "FOUND COLUMNS <br />\n"; switch ($temp[0]) { case "columns": $columns = (int)$temp[2]; break; case "rows": $rows = (int)$temp[2]; break; } } } #---------------------------------------------------------------------------END FUNCTIONS---------------------------------------------------------------------------- if ($columns == 0 || $rows == 0) { findColumnsAndRows(); } echo "columns" . $columns . "\n"; echo "rows" . $rows . "\n"; $lines = file($selectFile); foreach ($lines as $line_num => $line) { print "<font color=red>Line #{$line_num}</font> : " . $line . "<br />\n"; } ?> ;------------------------------------------------------------------- ;Character select definition [select Info] rows = 400 columns = 22 wrapping = 0 pos = 6,1 showemptyboxes = 0 moveoveremptyboxes = 0 cell.size = 14,14 cell.spacing = 0 Does that help a bit more? Quote Link to comment https://forums.phpfreaks.com/topic/132931-comparing-results-of-an-explode-in-a-switch/#findComment-691262 Share on other sites More sharing options...
JasonLewis Posted November 16, 2008 Share Posted November 16, 2008 Some problems: 1) Your function has no idea what any of the variables outside of its scope are. To it, $systemFile is an undefined variable. So that means that your file() call shouldn't be doing anything. To fix this, you can pass $systemFile as a parameter to the function. 2) Your function doesn't return anything. Again your getting confused with the scope. Setting $rows and $columns inside the function is making those variables only available to the function. They are not the same as the variables outside. So you need to return both $rows and $columns inside the function as an array so that you can access the values If you need help fixing them, then let us no. Quote Link to comment https://forums.phpfreaks.com/topic/132931-comparing-results-of-an-explode-in-a-switch/#findComment-691265 Share on other sites More sharing options...
pinkbits Posted November 16, 2008 Author Share Posted November 16, 2008 Thanks for the info, makes sense, for some reason I thought I'd seen that php didn't care about scope... <forehead slap> Anyway, I've had a stab at making those changes, and it's now actually processing the rest of the file, but not finding the columns or rows. As you've seen in the sample I gave you, they are actually in there. I've got that "FOUND COLUMNS" line in there just to see if it's finding it, and for some reason it's not. Any ideas? <?php #-------------------------------------------------------------------------------VARIABLES---------------------------------------------------------------------------- #Path to the Mugen installation (NO SLASH AT THE END) $mugenPath = 'E:\GAMES\MUGEN'; #Path to the various data files inside Mugen $selectFile = $mugenPath . '\DATA\select.def'; $systemFile = $mugenPath . '\DATA\system.def'; #find the columns and rows $columns = 0; $rows = 0; #-------------------------------------------------------------------------------END VARIABLES------------------------------------------------------------------------ #-------------------------------------------------------------------------------FUNCTIONS---------------------------------------------------------------------------- function findColumnsAndRows($systemFile) { $fh = file($systemFile); foreach ($fh as $line) { $temp = explode(" ", $line); if ($temp[0] == "columns") echo "FOUND COLUMNS <br />\n"; switch ($temp[0]) { case "columns": $columns = (int)$temp[2]; break; case "rows": $rows = (int)$temp[2]; break; } } return array ($columns, $rows); } #---------------------------------------------------------------------------END FUNCTIONS---------------------------------------------------------------------------- if ($columns == 0 || $rows == 0) { list($columns, $rows) = findColumnsAndRows(); } echo "columns" . $columns . "\n"; echo "rows" . $rows . "\n"; $lines = file($selectFile); foreach ($lines as $line_num => $line) { print "<font color=red>Line #{$line_num}</font> : " . $line . "<br />\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/132931-comparing-results-of-an-explode-in-a-switch/#findComment-691276 Share on other sites More sharing options...
thebadbad Posted November 16, 2008 Share Posted November 16, 2008 You're not passing the $systemFile array to the function. What about turning errors on? Makes debugging so easier: <?php ini_set('display_errors', true); error_reporting(E_ALL); ?> Added $systemFile as function parameter: if ($columns == 0 || $rows == 0) { list($columns, $rows) = findColumnsAndRows($systemFile); } Quote Link to comment https://forums.phpfreaks.com/topic/132931-comparing-results-of-an-explode-in-a-switch/#findComment-691284 Share on other sites More sharing options...
pinkbits Posted November 16, 2008 Author Share Posted November 16, 2008 Thanks alot guys. Appreciate the help. I'll give that error reporting a go... I'm sure I'll need it Quote Link to comment https://forums.phpfreaks.com/topic/132931-comparing-results-of-an-explode-in-a-switch/#findComment-691583 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.