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.

<?=$var['info']?> vs <? echo {$var['info']}; ?>

Discussion in 'PHP' started by x0x, Jul 14, 2009.

  1. #1
    <?=$var['info']?> VS <? echo {$var['info']}; ?>

    Which should I use? The first one is much sorter and works perfectly. Although if it works faster with echoing it, then I'll use that... What do you think?

    Also in a strings and queries, which is better to use:

    ".$var['info']."

    or

    {$var['info']}

    or

    $var[info]
     
    x0x, Jul 14, 2009 IP
  2. Mano2

    Mano2 Greenhorn

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #2
    simply:

    <?php
    echo $var['info'];
    ?>
     
    Mano2, Jul 14, 2009 IP
  3. garrettheel

    garrettheel Peon

    Messages:
    341
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #3
    watch out when using short tags "<?", as there's a chance your code might have to run on a server that doesn't have them enabled in the future.

    as far as ".$var." or {$var} goes, it wont make much difference. Just choose one you like and be consistent in using it.
     
    garrettheel, Jul 14, 2009 IP
  4. dannywwww

    dannywwww Well-Known Member

    Messages:
    804
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    110
    #4
    There is no difference..the only difference would be if your servers' php ini file didn't have short tags set as on, though most usually do, i don't think windows servers have them on as default though.
     
    dannywwww, Jul 14, 2009 IP
  5. anthonywebs

    anthonywebs Banned

    Messages:
    657
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #5
    if <?=$var['info']?>
    is enabled, its easier to write
     
    anthonywebs, Jul 14, 2009 IP
  6. garrettheel

    garrettheel Peon

    Messages:
    341
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #6
    What's more important? Easier to write, or compatibility
     
    garrettheel, Jul 14, 2009 IP
  7. SEOpaw

    SEOpaw Peon

    Messages:
    437
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #7
    you can either both of those codes but if you using php templating i prefer <?=$var['info']?> not to much code in it
     
    SEOpaw, Jul 14, 2009 IP
  8. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #8
    Personally I prefer complete opening tags rather than short tags because some servers don't have it enabled.

    <?php
    echo $var['info'];
    ?>

    For string, personally I don't think they matter. I prefer concatenation using dot : ".$var['info']."
     
    ads2help, Jul 15, 2009 IP
  9. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #9
    Ok. Thanks.

    Another one. Is there a difference between
     if(isset($rnd){}
    PHP:
    and
    if($rnd){}
    PHP:
    I just get those "Notice: Undefined index: $rnd" errors and when I use "isset" they seem to go away. Should I just ignore those errors? I turned all errors on and I'm trying to solve every one of them...
     
    x0x, Jul 15, 2009 IP
  10. goscript

    goscript Prominent Member

    Messages:
    2,753
    Likes Received:
    306
    Best Answers:
    0
    Trophy Points:
    315
    #10
    Yes there is.

    isset checks to see if that variable contains a value.
    If you don't use isset most likely will throw an error if the variable is not initialized yet.
     
    goscript, Jul 15, 2009 IP
  11. wd_2k6

    wd_2k6 Peon

    Messages:
    1,740
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    0
    #11
    So if we are checking if a post has been submitted or if a session variable exists there is no real reason to use isset ?
    
    if($_POST['submit']){ //Then the post has been submitted }
    
    if($_SESSION['auth']){ //Then the user has been logged in }
    
    PHP:
    Cause surley theres no way for these variables to have been created without a proper login or form submission, so there's no need for an isset check?

    Infact couldn't we shorten that down further and simply just say
    
    if($_POST){ //Then the post has been submitted }
    
    PHP:
    cause the post array woudln't have anything in if it hasnt been submitted
     
    wd_2k6, Jul 15, 2009 IP