Trivial help to a hopeless newbie

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
Bozack
Forum Newbie
Posts: 5
Joined: Mon Dec 15, 2008 10:34 am

Trivial help to a hopeless newbie

Post by Bozack »

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hi,

I'm just started on PHP after a way too long time without using it. I have simply forgotten everything I ever learned. Can anyone tell me why the code

Code: Select all

function top($pagetitle,$pagewidth,$dir) {
  echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
        <HTML>
        <head>
         <meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1" >
         <link rel=stylesheet type="text/css" href="'.$dir.'style.css">
         <title>'.$pagetitle.'</title>
        </head>
        
        <body><div align=center><table width='.$pagewidth.'><tr><td>';
  return;
}
results in the error
Parse error: parse error, unexpected T_FUNCTION in file on line line
?


~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
Reviresco
Forum Contributor
Posts: 172
Joined: Tue Feb 19, 2008 4:18 pm
Location: Milwaukee

Re: Trivial help to a hopeless newbie

Post by Reviresco »

Get rid of

Code: Select all

return;
-- not needed (and the correct usage would be:

Code: Select all

return();
at any rate).

When quoting an error message, be sure to leave in the line number so we can see where the error occurred.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Trivial help to a hopeless newbie

Post by jaoudestudios »

What line does it say the error is on?

I think it is your return. Because you are not returning anything, so either remove this line or return a value (i.e true/false)
Bozack
Forum Newbie
Posts: 5
Joined: Mon Dec 15, 2008 10:34 am

Re: Trivial help to a hopeless newbie

Post by Bozack »

The line it complains about is the line where I start defining the function, so it's complaining already on the line with

Code: Select all

function top($pagetitle,$pagewidth,$dir) {
moreover, it still gives the same error when I have removed the return.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Trivial help to a hopeless newbie

Post by jaoudestudios »

Whats on the previous line?

Can you post the few lines before too?
Bozack
Forum Newbie
Posts: 5
Joined: Mon Dec 15, 2008 10:34 am

Re: Trivial help to a hopeless newbie

Post by Bozack »

The whole start of the document:

Code: Select all

<?php
 
/* name:        top
   description: The top of all pages
   written by:  Bozack
   created:     2008/12/15
   modified:    2008/12/15
   
   pagetitle:   Title of the page
   pagewidth:   Width of the table containing the page
   dir:         Position from root  */
 
// $pagetitle = '...'       This is the default page title of the index page
global $pagewidth;
$pagewidth = '600px'
 
function top($pagetitle,$dir) {
  echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
        <HTML>
        <head>
         <meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1" >
         <link rel=stylesheet type="text/css" href="'.$dir.'style.css">
         <title>'.$pagetitle.'</title>
        </head>
        
        <body><div align=center><table width='.$pagewidth.'><tr><td>';
  return;
}
and it was complaining about line 17
Reviresco
Forum Contributor
Posts: 172
Joined: Tue Feb 19, 2008 4:18 pm
Location: Milwaukee

Re: Trivial help to a hopeless newbie

Post by Reviresco »

Missing a semi-colon at the end of line 15.
Bozack
Forum Newbie
Posts: 5
Joined: Mon Dec 15, 2008 10:34 am

Re: Trivial help to a hopeless newbie

Post by Bozack »

Argh, dammit, can't believe I didn't saw that! Thanks! :oops:
cavemaneca
Forum Commoner
Posts: 59
Joined: Sat Dec 13, 2008 2:16 am

Re: Trivial help to a hopeless newbie

Post by cavemaneca »

Also, why didn't you just do this?

Code: Select all

<?php
// other code
function top($pagetitle,$pagewidth,$dir) {
  ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
        <HTML>
        <head>
         <meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1" >
         <link rel=stylesheet type="text/css" href="<?php echo $dir; ?>/style.css">
         <title><?php echo $pagetitle; ?></title>
        </head>
       
        <body><div align=center><table width="<?php echo $pagewidth; ?>"><tr><td>
<?php
}
?>
It seems simpler to me that way, and you don't have to worry about anything stupid like escaping.

EDIT: Thanks for pointing that out jaoudestudios. I forgot to echo the variables. The above should be fixed.
Last edited by cavemaneca on Mon Dec 15, 2008 4:34 pm, edited 1 time in total.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Trivial help to a hopeless newbie

Post by jaoudestudios »

cavemaneca...
<title>'.$pagetitle.'</title>
That would not work! Unless within php tags.
i.e.

Code: Select all

<title><?php echo $pagetitle; ?></title>
Bozack
Forum Newbie
Posts: 5
Joined: Mon Dec 15, 2008 10:34 am

Re: Trivial help to a hopeless newbie

Post by Bozack »

Actually I didn't thought that you could go out of the <?php ... ?> tags within a function :) But yes, that is much easier, and tidier
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Trivial help to a hopeless newbie

Post by jaoudestudios »

No problem cavemaneca - easy mistake, I know you were going for principle rather than completing the whole code.
Post Reply