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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
m0u53m4t
Forum Contributor
Posts: 101
Joined: Wed Apr 19, 2006 7:47 am
Location: Wales

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

Post 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";

?>
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post 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) {
User avatar
m0u53m4t
Forum Contributor
Posts: 101
Joined: Wed Apr 19, 2006 7:47 am
Location: Wales

Post 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
phpCCore Brad
Forum Commoner
Posts: 47
Joined: Sun Dec 04, 2005 5:46 pm
Location: Michigan, USA
Contact:

Post 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){
User avatar
m0u53m4t
Forum Contributor
Posts: 101
Joined: Wed Apr 19, 2006 7:47 am
Location: Wales

Post 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
phpCCore Brad
Forum Commoner
Posts: 47
Joined: Sun Dec 04, 2005 5:46 pm
Location: Michigan, USA
Contact:

Post 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
User avatar
m0u53m4t
Forum Contributor
Posts: 101
Joined: Wed Apr 19, 2006 7:47 am
Location: Wales

Post 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";
}
?>
phpCCore Brad
Forum Commoner
Posts: 47
Joined: Sun Dec 04, 2005 5:46 pm
Location: Michigan, USA
Contact:

Post by phpCCore Brad »

Then you should probably remove it since you removed the if statement it belongs to.
User avatar
m0u53m4t
Forum Contributor
Posts: 101
Joined: Wed Apr 19, 2006 7:47 am
Location: Wales

Post 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
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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);
?>
User avatar
m0u53m4t
Forum Contributor
Posts: 101
Joined: Wed Apr 19, 2006 7:47 am
Location: Wales

Post 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
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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!!!
User avatar
m0u53m4t
Forum Contributor
Posts: 101
Joined: Wed Apr 19, 2006 7:47 am
Location: Wales

Post by m0u53m4t »

Thanks. What does /R/N do?
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post 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'.
Post Reply