Can't figure out why: Parse error: syntax error, unexpected T_VARIABLE
Hi, What's wrong with my code? It's to do with if ( $field1 == null || $field2 == null ) { because if I remove the error check, the code works fine.
PHP Code:
<?php
function generatePassword ($length = 6) { // start with a blank password $password = ""; $possible = "1234567890abcdefghijklmnopqrstuvwxyz";
// we refer to the length of $possible a few times, so let's grab it now $maxlength = strlen($possible);
// set up a counter for how many characters are in the password so far $i = 0;
// add random characters to $password until $length is reached while ($i < $length) { // pick a random character from the possible ones $char = substr($possible, mt_rand(0, $maxlength-1), 1); // have we already used this character in $password? $password .= $char; $i++; }
// done! return $password; }
//echo generatePassword();
// Gets DB file into an array. $lines = file('userlist.txt'); // format = email@email.com::Firstname Lastname
// Loop through the array and organise into 2 columns. foreach ($lines as $line) {
list ($field1, $field2) = split ('::', $line);
if ( $field1 == null || $field2 == null ) { // Missing data -- Email or Name. echo "<br><br>There were empthy fields. Check the list and correct. <br><br>"; } else { echo '<br><br>'; echo "$field1$field2"; echo generatePassword(); echo '<br />'; } } ?>
No parse errors for me when I copy-and-paste your code into my editor. Maybe you have some non-printing/white-space character in there? (You could try copy-and-pasting from here, too, to see if that fixes it, and make sure your editor is saving as a plain ASCII text file, or if needed, UTF-8 without BOM.)
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
From what I remember, the line was " if ( $field1 == null || $field2 == null ) { ", but I removed this part of the code completely and got it working without error checking.
After reading about the possible formatting problems in this post though, I've just copied the sample code to another file and ran it without error. I guess it was something to do with the file encoding then.
Bookmarks