www.webdeveloper.com
Results 1 to 9 of 9

Thread: my php if function

  1. #1
    Join Date
    Jul 2007
    Posts
    76

    my php if function

    this code does work it just doesn't show up my iframe why?

    <?php
    $apt = isset($_GET['apt']) ? $_GET['apt'] : 'apt';
    if ($apt=="KLU")
    echo "Have a nice weekend!";
    else
    echo "<iframe name="ab_golf" class="mid" id="ab_golf" src="http://www.xxxxxxx.com/iframes/golf/<?=$apt;?>" longdesc="" vwidth="480" height="900" noresize="noresize" frameborder="0" scrolling="no" onload="window.scrollTo(0,0);"><a href="http://www.xxxxxxx.com/iframes/golf/<?=$apt;?>" target="_blank">Click here to view content of this frame</a></iframe>";
    ?>

    the function in its original state

    <?php
    $d=date("D");
    if ($d=="Fri")
    echo "Have a nice weekend!";
    else
    echo "Have a nice day!";
    ?>

    thanks!

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,609
    If you are going to use double-quotes within a double-quoted string literal, then those inner double-quotes must be escaped. Also, since you are already in PHP mode (doing an echo), you do not want to have PHP tags within the string; instead, just use the desired variable.
    PHP Code:
    echo "<iframe name=\"ab_golf\" class=\"mid\" id=\"ab_golf\" src=\"http://www.xxxxxxx.com/iframes/golf/$apt;\" longdesc=\"\" vwidth=\"480\" height=\"900\" noresize=\"noresize\" frameborder=\"0\" scrolling=\"no\" onload=\"window.scrollTo(0,0);\"><a href=\"http://www.xxxxxxx.com/iframes/golf/$apt;\" target=\"_blank\">Click here to view content of this frame</a></iframe>"
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jul 2007
    Posts
    357
    I would use single quotes. You don't have to escape every double quote. It's supposedly faster to output and it's faster to type . The only thing you have to remember is concatenation is needed. Usually a period, but with echo you can use a comma - its said to give a little bit more performance.

    PHP Code:
    echo '<iframe name="ab_golf" class="mid" id="ab_golf" src="http://www.xxxxxxx.com/iframes/golf/',$apt,'" longdesc="" vwidth="480" height="900" noresize="noresize" frameborder="0" scrolling="no" onload="window.scrollTo(0,0);"><a href="http://www.xxxxxxx.com/iframes/golf/',$apt,'" target="_blank">Click here to view content of this frame</a></iframe>'
    Last edited by ellisgl; 08-01-2007 at 06:12 PM.

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,609
    Simpler yet is to use single quotes for the HTML attribute values:
    PHP Code:
    echo "<iframe name='ab_golf' class='mid' id='ab_golf' src='http://www.xxxxxxx.com/iframes/golf/$apt;' longdesc='' vwidth='480' height='900' noresize='noresize' frameborder='0' scrolling='no' onload='window.scrollTo(0,0);'><a href='http://www.xxxxxxx.com/iframes/golf/$apt;' target='_blank'>Click here to view content of this frame</a></iframe>"
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Jul 2007
    Posts
    357
    You could that too. But here's a bench mark for double vs. single - and with a variable in place.

    Notice the concatenation on the echo also.

    http://spindrop.us/2007/03/03/php-do...single-quotes/
    http://karma.nucleuscms.org/item/27

    http://www.thespanner.co.uk/2007/07/...p-performance/ -- other stuff for php performance.
    Last edited by ellisgl; 08-01-2007 at 08:15 PM.

  6. #6
    Join Date
    Mar 2007
    Posts
    65
    <?=$apt;?> looks kind of funny.. should it be <?php echo $apt;?> ?

    It occurs twice in your script

  7. #7
    Join Date
    May 2007
    Posts
    16

    Smile

    Quote Originally Posted by Beedge
    <?=$apt;?> looks kind of funny.. should it be <?php echo $apt;?> ?

    It occurs twice in your script
    yeah you're right i m also frustrated by seeing this.... very funny LOL

  8. #8
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,609
    Quote Originally Posted by Beedge
    <?=$apt;?> looks kind of funny.. should it be <?php echo $apt;?> ?

    It occurs twice in your script
    It is valid, as long as short_open_tags are enabled in the PHP configuration. However, since you cannot guarantee they will be enabled on all servers (and in fact it is now recommended that they not be since they clash with XML declaration tags), the better practice is to always use long tags or script tags. The <?= tag is a special instance of the short tag <? which implies the contents of the tag are to be echoed.

    As stated in the manual on the Basic Syntax page:
    There are four different pairs of opening and closing tags which can be used in php. Two of those, <?php ?> and <script language="php"> </script>, are always available. The other two are short tags and ASP style tags, and can be turned on and off from the php.ini configuration file. As such, while some people find short tags and ASP style tags convenient, they are less portable, and generally not recommended.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  9. #9
    Join Date
    Mar 2007
    Posts
    65
    Quote Originally Posted by NogDog
    It is valid, as long as short_open_tags are enabled in the PHP configuration. However, since you cannot guarantee they will be enabled on all servers (and in fact it is now recommended that they not be since they clash with XML declaration tags), the better practice is to always use long tags or script tags. The <?= tag is a special instance of the short tag <? which implies the contents of the tag are to be echoed.

    As stated in the manual on the Basic Syntax page:
    Ok.. Interesting!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles