SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    Canada
    Posts
    373
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    General PHP Syntax Questions..

    Hey Everyone,

    In advance thanks for everyone who is willing to help people like me out!

    Here we go…

    #1 Am I right to say that any html inside of a <?php tag must be echoed out? Example, echo “<tr></tr>”.
    Often when I do this I get syntax errors such as “Unexpected “/” on line 1.

    #2 If I have links that look like this: <p><a href="' . $_SERVER['PHP_SELF'] .
    '?addjoke=1">Add a Joke!</a></p> do they have to be inside of a PHP tag?

    #3 I have a page that performs a function once a certain link is clicked, do I put the source code with the HTML containing the link first, or do I put the syntax of the function it will perform first?

    #4 In this tutorial here (http://www.sitepoint.com/article/pub...sql-data-web/3) there is a statement that says
    PHP Code:

    <?php if (isset($_GET['addjoke'])): // User wants to add a joke 
    ?>
    Some HTML….

    <?php else:
    Under this php else is where a connection with SQL is made, and is where all of the code takes place. What I don’t get is that the php if statement uses code from the ?php else: statement. I am not sure if I am making this clear enough, but if the user clicks the ‘addjoke’ link the HTML appears, however when the user then submits the information from the HTML the code from the else: statement takes over and is used. How can this be?

    Thanks for all the help!

  2. #2
    SitePoint Zealot
    Join Date
    Nov 2004
    Location
    Ankara/Turkey
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    #1
    you may write
    PHP Code:
    <?
    echo "hello";
    echo 
    "
    hello
    "
    ;
    echo 
    "<font size=\"2\">hello<font>";
    ?>
    do not forget semi-colon and slashes!

    #2

    you must write :

    PHP Code:
    <?
    echo "<p><a href=\"$_SERVER['PHP_SELF']?addjoke=1\">Add a Joke!</a></p>";
    echo 
    '<p><a href="' $_SERVER['PHP_SELF'] . 
    '?addjoke=1">Add a Joke!</a></p>'// this is suitable i think
    ?>
    #3

    i don't know it is same or not but use function first it is good for writing code habits.

    #4

    you may write
    PHP Code:
    if ($_GET["addjoke"]) {
          
    // actions
    } else {
        
    // else actions come

    i do not understand this question clearly but may be you want this...

  3. #3
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cappytoi
    #1
    you may write
    PHP Code:
     <?
     
    echo "hello";
     echo 
    "
     hello
     "
    ;
     echo 
    "<font size=\"2\">hello<font>";
     
    ?>
    Using Short open tags is not a good idea as some servers will have it turned off. always use the full tag
    PHP Code:
        //will work everywhere.
        <?php
       
       
         
    // will not work on servers that have short open tags turned off
        
    <?

  4. #4
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @#2 Just the php code itself needs to be in tags.

    PHP Code:
     <p><a href="<?php echo $_SERVER['PHP_SELF']; ?>?addjoke=1">Add a Joke!</a></p>

  5. #5
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by tdob
    #4 In this tutorial here (http://www.sitepoint.com/article/pub...sql-data-web/3) there is a statement that says
    PHP Code:

    <?php if (isset($_GET['addjoke'])): // User wants to add a joke 
    ?>
    That is the "Alternative syntax for control structures", see php.net: http://www.php.net/manual/en/control...ive-syntax.php

    You should read this if you have any problems with basic syntax. (Or if you just want to pick up some tips you wouldn't find from trial and error.)

    Douglas
    Hello World

  6. #6
    SitePoint Zealot
    Join Date
    Nov 2004
    Location
    Ankara/Turkey
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jump
    Using Short open tags is not a good idea as some servers will have it turned off. always use the full tag
    PHP Code:
        //will work everywhere.
        <?php
       
       
         
    // will not work on servers that have short open tags turned off
        
    <?
    many servers accept it... but you're right sometimes they do not

  7. #7
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    Canada
    Posts
    373
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot guys.. I really appreciate all the help!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •