1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP Error " unexpected T_STRING " , please help !

Discussion in 'PHP' started by FR3@K, Oct 7, 2007.

  1. #1
    Hello friends.
    I am using this code.. thats the first time ever i wrote a long php code myself..
    but it doesnt works :( it gives " unexpected T_STRING " error.
    can anyone please tell me why is it giving error??

    <?php $thisPage="Home"; ?>
    <html><head>......</head><body>

    <?php if ($thisPage!="Home") echo "<div class="footer"> <a href='javascript:history.go(-1)'> [ Back ]</a> </div>"; ?>

    </body>
    </html>
     
    FR3@K, Oct 7, 2007 IP
  2. james_r

    james_r Peon

    Messages:
    194
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <?php if ($thisPage!="Home") { echo "<div class=\"footer\"> <a href='javascript:history.go(-1)'> [ Back ]</a> </div>"; } ?>
     
    james_r, Oct 7, 2007 IP
  3. FR3@K

    FR3@K Banned

    Messages:
    182
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    can u tell me why did you put slashes before and after " footer" ??
     
    FR3@K, Oct 7, 2007 IP
  4. chickens

    chickens Peon

    Messages:
    242
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    That tells PHP to ignore those quotes. Without them PHP tries to parse them and therefore the script errors out.
     
    chickens, Oct 7, 2007 IP
  5. FR3@K

    FR3@K Banned

    Messages:
    182
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    and why did he write { and } ??
     
    FR3@K, Oct 7, 2007 IP
  6. james_r

    james_r Peon

    Messages:
    194
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    the { } tells PHP that everything inside the curly brackets should happen if ($thispage != "Home")

    This is useful if you want to have more than one command in your if statement. In this case it's not really NEEDED because there's only one command after the IF statement. But it's good coding practice to use it. :)
     
    james_r, Oct 7, 2007 IP
  7. FR3@K

    FR3@K Banned

    Messages:
    182
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    but we can use elseif syntax to have more commands..
    why did u write { } ?? is there any special use of it?
     
    FR3@K, Oct 7, 2007 IP
  8. chickens

    chickens Peon

    Messages:
    242
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Technically you do not need them for a single line, but they are a good coding practice. The brackets tell the PHP parser where to start and stop, in this case the if statement.

    For good clean clode it is best to use line breaks to separate the code.

    <?php 
      $thisPage="Home"; 
    ?>
    <html>
      <head>......</head>
      <body>
    
      <?php 
        if ($thisPage!="Home") {
          echo "<div class=\"footer\"> 
          <a href='javascript:history.go(-1)'> [ Back ]</a> 
          </div>"; 
        }
      ?>
    
      </body>
    </html>
    Code (markup):
     
    chickens, Oct 7, 2007 IP
  9. FR3@K

    FR3@K Banned

    Messages:
    182
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    yeah u are right , but what if we dont write the brackets??
    will it work the same way ??
     
    FR3@K, Oct 7, 2007 IP
  10. james_r

    james_r Peon

    Messages:
    194
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #10
    if you had more complicated code, it would be useful. else or elseif commands only happen if the condition in your IF statement is not true. so for example:

    
    <?php 
         if ($thisPage!="Home") 
         { 
              echo "<div class=\"footer\"> <a    href='javascript:history.go(-1)'> [ Back ]</a> </div>"; 
              $some_variable = 0;
         } 
         else
         {
              echo "Next >>";
              $some_variable = 1;
         }
    
    ?>
    
    PHP:
     
    james_r, Oct 7, 2007 IP
  11. chickens

    chickens Peon

    Messages:
    242
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Will it work your way, yes it should.
    Is it a good coding practice, no.
     
    chickens, Oct 7, 2007 IP
  12. FR3@K

    FR3@K Banned

    Messages:
    182
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    yeah i understood what you mean.. but what will happen if i dont write the brackets?? will the code work ?
     
    FR3@K, Oct 7, 2007 IP
  13. james_r

    james_r Peon

    Messages:
    194
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Probably not. :)
     
    james_r, Oct 7, 2007 IP
  14. FR3@K

    FR3@K Banned

    Messages:
    182
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    ok lets try this code , with and without bracket..
     
    FR3@K, Oct 7, 2007 IP
  15. james_r

    james_r Peon

    Messages:
    194
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Oh, I see... yes, your code should work both ways, but it's bad coding practice to not use the brackets. It's a bad habit that could cause confusion for you or anyone reading your code in the future!
     
    james_r, Oct 7, 2007 IP
  16. FR3@K

    FR3@K Banned

    Messages:
    182
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #16
    hey this code isnt working..
    i wrote :


    <?php $thisPage="Home"; ?>
    <html><head><body>
    <?php 
         if ($thisPage!="Home") 
         { 
              echo "<div class=\"footer\"> <a    href='javascript:history.go(-1)'> [ Back ]</a> </div>"; 
              $page = "not home";
         } 
         else
         {
              echo "Next >>";
              $page = "Home";
         }
    
    ?>
    
    <?php echo "$page"; ?>
    </body></html>
    PHP:

    and it gave output as
    while it should be showing Next's link becuz i defined the page as home..
     
    FR3@K, Oct 7, 2007 IP
  17. james_r

    james_r Peon

    Messages:
    194
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #17
    hmmmm... that's strange. try this:

    
    <?php $thisPage = "Home"; ?>
    <html><head><body>
    <? 
         if ($thisPage != "Home") 
         { 
              echo "<div class=\"footer\"> <a href='javascript:history.go(-1)'> [ Back ]</a></div>"; 
              $page = "not home";
         } 
         else
         {
              echo "Next >>";
              $page = "Home";
         }
    
    ?>
    
    <? echo $page; ?>
    </body></html>
    
    PHP:
    It's exactly the same as what you posted, just with some spaces added in the code... I'm not sure why that wouldn't work. Anyone else?
     
    james_r, Oct 7, 2007 IP
  18. FR3@K

    FR3@K Banned

    Messages:
    182
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #18
    wait.. why did u put off the " " from <? echo $page; ?> ?


    ok wait.. that code was working fine.. i forgot to save the file :p
    so why did u put off the "" ?
    this code is working fine
    but it is with " " .
    i wanted to ask why did u put them off?

    
    <?php $thisPage = "Homefg"; ?>
    <html><head><body>
    <? 
         if ($thisPage != "Home") 
         { 
              echo "<div class=\"footer\"> <a href='javascript:history.go(-1)'> [ Back ]</a></div>"; 
              $page = "not home";
         } 
         else
         {
              echo "Next >>";
              $page = "Home";
         }
    
    ?>
    
    <? echo "$page"; ?>
    </body></html>
    PHP:
     
    FR3@K, Oct 7, 2007 IP
  19. james_r

    james_r Peon

    Messages:
    194
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #19
    Oh sorry, it's not necessary, since you're just echoing the contents of a variable.
     
    james_r, Oct 7, 2007 IP
  20. FR3@K

    FR3@K Banned

    Messages:
    182
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #20
    ok thanks for the help :D + rep to you
     
    FR3@K, Oct 7, 2007 IP