Page 1 of 1

Help: Parse error: parse error, unexpected T_IF on line 5

Posted: Sun Jun 18, 2006 9:54 am
by m0u53m4t
This is my php code designed to take a number of hours, convert them into days and hours, then write to time.php in this format: "days:hours" but I'm getting this error:
Parse error: parse error, unexpected T_IF on line 5

Code: Select all

<?php
$filename = "time.php";
$hours = $_GET["hours"]

if $hours > 24 then {
   if $hours > 48 then {
      if $hours > 72 then {

      $days = 3;
      $hours = $hours - 72;
   }

   $days = 2;
   $hours = $hours - 48;
}

$days = 1;
$hours = $hours - 24;
}

if (!$handle = fopen($filename, 'a')) {
  die ("Error while connecting, please try again.");
 } else {
  if (fwrite($handle, "\r\n" . $days, "\r\n" . ":" . $hours, "\r\n" .) === FALSE){
   die("Error while writing.");
  }
 }
echo "Successful";
 fclose($handle);
} else {
 echo "Error: No time was specified";

?>

Posted: Sun Jun 18, 2006 10:12 am
by bdlang
You're missing a statement ending semicolon at the end of your $hours assignment, and there is no 'if...then' in PHP.

Code: Select all

<?php
$filename = "time.php";
$hours = $_GET["hours"];

if ($hours > 24) {
   if ($hours > 48) {
      if ($hours > 72) {

Posted: Sun Jun 18, 2006 10:18 am
by m0u53m4t
I updated the code to this:

Code: Select all

<?php
$filename = "time.php";
$hours = $_GET["hours"];

if ($hours > 24) {
   if ($hours > 48) {
      if ($hours > 72) { 

      $days = 3;
      $hours = $hours - 72;
   }

   $days = 2;
   $hours = $hours - 48;
}

$days = 1;
$hours = $hours - 24;
}

if (!$handle = fopen($filename, 'a')) {
  die ("Error while connecting, please try again.");
 } else {
  if (fwrite($handle, "\r\n" . $days, "\r\n" . ":" . $hours, "\r\n" .) === FALSE){
   die("Error while writing.");
  }
 }
echo "You've been online for $days days and $hours hours this week.;
 fclose($handle);
} else {
 echo "Error: No time was specified";

?>
but am now getting this error:
Parse error: parse error, unexpected ')' on line 24

Posted: Sun Jun 18, 2006 10:23 am
by phpCCore Brad
Line 24 has an extra . which is telling php you should have something else after it, but you don't. Simply remove it and you get:

Code: Select all

//Before:
 if (fwrite($handle, "\r\n" . $days, "\r\n" . ":" . $hours, "\r\n" .) === FALSE){

//After:
 if (fwrite($handle, "\r\n" . $days, "\r\n" . ":" . $hours, "\r\n") === FALSE){

Posted: Sun Jun 18, 2006 10:30 am
by m0u53m4t
That fixed that. Still another bug. Here's the code:

Code: Select all

<?php
$filename = "time.php";
$hours = $_GET["hours"];

if ($hours > 24) {
   if ($hours > 48) {
      if ($hours > 72) {

      $days = 3;
      $hours = $hours - 72;
   }

   $days = 2;
   $hours = $hours - 48;
}

$days = 1;
$hours = $hours - 24;
}

if (!$handle = fopen($filename, 'a')) {
  die ("Error while connecting, please try again.");
 } else {
  if (fwrite($handle, "\r\n" . $days, "\r\n" . ":" . $hours, "\r\n") === FALSE){ 
   die("Error while writing.");
  }
 }
echo "You've been online for $days days and $hours hours this week.";
 fclose($handle);
} else {
 echo "Error: No time was specified";
}
?>
And here's the error I'm getting:
Parse error: parse error, unexpected '}' on line 30

Posted: Sun Jun 18, 2006 10:37 am
by phpCCore Brad
I retabbed your document... I could make an assumuption to what that else belongs to, but maybe with the retabs you will figure out your own mistake

Code: Select all

<?php
$filename = "time.php";
$hours = $_GET["hours"];

if ($hours > 24) {
	if ($hours > 48) {
		if ($hours > 72) {
	
			$days = 3;
			$hours = $hours - 72;
		}
		$days = 2;
		$hours = $hours - 48;
	}

	$days = 1;
	$hours = $hours - 24;
}

if (!$handle = fopen($filename, 'a')) {
	die ("Error while connecting, please try again.");
} else {
	if (fwrite($handle, "\r\n" . $days, "\r\n" . ":" . $hours, "\r\n") === FALSE){
		die("Error while writing.");
	}
}
echo "You've been online for $days days and $hours hours this week.";
fclose($handle);



} else {
 echo "Error: No time was specified";
}
?>

Edit: I retabbed some that were spaces instead of tabs making them not appear on the same line

Posted: Sun Jun 18, 2006 10:47 am
by m0u53m4t
I'm not sure. I adapted it from this script:

Code: Select all

<?php
$filename = "logfile.txt";
if (isset($_GET["comments"])){
 if (!$handle = fopen($filename, 'a')) {
  die ("Error while connecting, please try again.");
 } else {
  if (fwrite($handle, "\r\n" . $_GET["comments"]."\r\n" ) === FALSE){
   die("Error while writing.");
  }
 }
echo "Comments successful";
 fclose($handle);
} else {
 echo "Error: no comments entered";
}
?>

Posted: Sun Jun 18, 2006 10:56 am
by phpCCore Brad
Then you should probably remove it since you removed the if statement it belongs to.

Posted: Sun Jun 18, 2006 10:59 am
by m0u53m4t
Ah. So it should be:

Code: Select all

<?php
$filename = "time.php";
$hours = $_GET["hours"];

if ($hours > 24) {
        if ($hours > 48) {
                if ($hours > 72) {
       
                        $days = 3;
                        $hours = $hours - 72;
                }
                $days = 2;
                $hours = $hours - 48;
        }

        $days = 1;
        $hours = $hours - 24;
}

if (!$handle = fopen($filename, 'a')) {
        die ("Error while connecting, please try again.");
} else {
        if (fwrite($handle, "\r\n" . $days, "\r\n" . ":" . $hours, "\r\n") === FALSE){
                die("Error while writing.");
        }
}
echo "You've been online for $days days and $hours hours this week.";
fclose($handle);
?>
I'm just getting this error:
Warning: Wrong parameter count for fwrite() on line 25

Posted: Sun Jun 18, 2006 11:37 am
by tecktalkcm0391
Try it like this:

Code: Select all

<?php
$filename = "time.php";
$hours = $_GET["hours"];

if ($hours > 24) {
        if ($hours > 48) {
                if ($hours > 72) {
       
                        $days = 3;
                        $hours = $hours - 72;
                }
                $days = 2;
                $hours = $hours - 48;
        }

        $days = 1;
        $hours = $hours - 24;
}

if (!$handle = fopen($filename, 'a')) {
        die ("Error while connecting, please try again.");
} else {
         $write = "\r\n" . $days, "\r\n" . ":" . $hours, "\r\n";
         if (fwrite($handle, $write) === FALSE){
                die("Error while writing.");
        }
}
echo "You've been online for $days days and $hours hours this week.";
fclose($handle);
?>

Posted: Sun Jun 18, 2006 12:09 pm
by m0u53m4t
Still more errors :roll:
Parse error: parse error, unexpected ',' in /home/freehost/t35.com/j/u/juniorfiles/sig/updatetime.php on line 23

Posted: Sun Jun 18, 2006 3:25 pm
by tecktalkcm0391
opps I made an error.

Try this:

Code: Select all

<?php 
$filename = "time.php"; 
$hours = $_GET["hours"]; 

if ($hours > 24) { 
        if ($hours > 48) { 
                if ($hours > 72) { 
        
                        $days = 3; 
                        $hours = $hours - 72; 
                } 
                $days = 2; 
                $hours = $hours - 48; 
        } 

        $days = 1; 
        $hours = $hours - 24; 
} 

if (!$handle = fopen($filename, 'a')) { 
        die ("Error while connecting, please try again."); 
} else { 
         $write = "\r\n" . $days. "\r\n" . ":" . $hours. "\r\n"; 
         if (fwrite($handle, $write) === FALSE){ 
                die("Error while writing."); 
        } 
} 
echo "You've been online for $days days and $hours hours this week."; 
fclose($handle); 
?>
I put commas by accident instead of periods.

SORRY!!!

Posted: Mon Jun 19, 2006 11:26 am
by m0u53m4t
Thanks. What does /R/N do?

Posted: Mon Jun 19, 2006 1:28 pm
by bdlang
m0u53m4t wrote:Thanks. What does /R/N do?
That does nothing; however, \r\n are escape characters that create a return and a newline, respectively. Essentially, two ways to create a new line in your output.

If you're dealing with a UNIX based file, use "\n" to create a newline in the file.
PHP output to a file on Windows should use "\r\n".
Output on a Mac just "\r".

Please note these need to be wrapped in double quotes to be effective. Single quotes will output a literal '\n'.