Page 1 of 1

I can't understand brackets

Posted: Mon Feb 20, 2006 8:55 pm
by Altec88
Ok, I'm trying to make a simple username,password system, and I need to know where the { } marks go. This is my first time with php. Here is the code:

Code: Select all

<?php
$found=0;
$filefopen("members.dat", "r");
(!feof($file))
$line=fgets($file,255);
$line=chop($line);
$field=split("#",$line,6);
if($username==$field[0])
$found=1;
if(password!=$field[1])
print "<h2>Wrong Password</h2>";
die();
if($found==0)
{print "<h2>Username does not exist</h2>";
die();
?>
Thanks a ton! I don't understand these {} marks.

This part is supposed to authenticate a user before displaying the page.

Posted: Mon Feb 20, 2006 9:54 pm
by pennythetuff

Code: Select all

<?php
$found=0;
$file = fopen("members.dat", "r");
while(!feof($file)) {
  $line=fgets($file,255);
  $line=rtrim($line);
  $field=split("#",$line,6);
  if($username==$field[0]) {
    $found=1;
  if($password!=$field[1]) {
    echo "<h2>Wrong Password</h2>";
    die();
  }
  if($found==0) {
    echo "<h2>Username does not exist</h2>";
    die();
  }
}
?>
There's where the brackets would go, and I changed your first if-statement into a while loop to loop through each line.

Posted: Mon Feb 20, 2006 10:24 pm
by Benjamin
Brackets are used to encapsulate code in a control structure. Control structures include statements such as IF ELSE ELSEIF WHILE FOR SWITCH and a few others. Download the PHP reference manual from php.net or do a google search for PHP Tutorials to learn more.

Code: Select all

// $x == $y means "is equal to"
if ($x == $y) {
  // if this condition is true
  // execute the code in the brackets
}