SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Thread: Proper syntax

  1. #1
    SitePoint Enthusiast johnoz's Avatar
    Join Date
    Mar 2002
    Location
    here
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Proper syntax

    Hi,
    I am trying write a script and I want to drop all config variables and language variables into 2 seperate files as an include.
    ie

    PHP Code:
    include lang.php ;
    include 
    config.php
    I was wondering what is the proper syntax to do this?
    PHP Code:
    1.$_lang[linkurl] = "Link URL: " //no quotes
    2.$_lang['linkurl'] = "Link URL: " ;  // single quotes
    3.$_lang["linkurl"] = "Link URL: " ;  //double quotes

    $_lang[linkurl] = "Link URL: " ;
    $_lang[linktitle] = "Link Title: " ;
    $_lang[lastname] = "Last name: " ;
    $_lang[location] = "Location: " ;
     
    $CONFIG['db_hostname'] = 'localhost';
    $CONFIG['db_database'] = 'lite';
    $CONFIG['db_username'] = 'root';
    $CONFIG['db_password'] = 'muhsin'
    I prefer the number 1 solution without any quotes so that I don't have to connatete when making an echo or print statement. But what is the proper way?

  2. #2
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    // This ...
    $_lang['linkurl'] = "Link URL: " ;
    // ... or this

    $CONFIG['db_hostname'] = 'localhost'
    Except there is no real difference between them.

  3. #3
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Note that this works:
    PHP Code:
    $ar['element'] = 'Hi!';
    echo 
    "$ar[element]"
    In other words, storing an array one way doesn't mean you can't retrieve it another in this case.

    The "without quotes" method is kind of cheating in the sense that PHP will automatically consider the word inside the brackets as a string... however, my guess is that PHP first looks to see if that word is a constant, which will eat some (incredibly miniscule) amount of execution time.

    In general, you are supposed to use single quotes around a string unless otherwise needed, so I'd say use #2. If you need or want to reference that element using #1 you still can.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  4. #4
    SitePoint Enthusiast johnoz's Avatar
    Join Date
    Mar 2002
    Location
    here
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Gaheris
    PHP Code:
    // This ...
    $_lang['linkurl'] = "Link URL: " ;
    // ... or this

    $CONFIG['db_hostname'] = 'localhost'
    Except there is no real difference between them.
    So you are saying I can getaway without using any quotes at all?
    ie


    PHP Code:
    $_lang[linkurl] = "Link URL: " 

  5. #5
    SitePoint Enthusiast johnoz's Avatar
    Join Date
    Mar 2002
    Location
    here
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cool. Many thanks

    Quote Originally Posted by samsm
    Note that this works:
    PHP Code:
    $ar['element'] = 'Hi!';
    echo 
    "$ar[element]"
    In other words, storing an array one way doesn't mean you can't retrieve it another in this case.

    The "without quotes" method is kind of cheating in the sense that PHP will automatically consider the word inside the brackets as a string... however, my guess is that PHP first looks to see if that word is a constant, which will eat some (incredibly miniscule) amount of execution time.

    In general, you are supposed to use single quotes around a string unless otherwise needed, so I'd say use #2. If you need or want to reference that element using #1 you still can.

  6. #6
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by johnoz
    So you are saying I can getaway without using any quotes at all?
    Yes, but it will produce an error notice. Something like
    Notice: Use of undefined constant linkurl - assumed 'linkurl' in (....) on line #
    So it's not a very good solution. In my opinion it's indeed very bad.

    To see the notices, add this to the top of your page
    PHP Code:
    error_reporting(E_ALL); 
    HTH

    -Helge

  7. #7
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, that's why I picked out the lines which had quotes around the index.

  8. #8
    SitePoint Enthusiast johnoz's Avatar
    Join Date
    Mar 2002
    Location
    here
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually, I just used error_reporting(0) on my common.inc.php scriptto avoid seeing any errors. As long as the way I use doesn't cause a mjor compatibility issues (win vs *nix) I guess I am happy.

    Thank you alll

    Quote Originally Posted by Helge
    Yes, but it will produce an error notice. Something like So it's not a very good solution. In my opinion it's indeed very bad.

    To see the notices, add this to the top of your page
    PHP Code:
    error_reporting(E_ALL); 
    HTH [img]images/smilies/smile.gif[/img]

    -Helge

  9. #9
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So you're writing sloppy code but you don't care even if you know how to fix it? What happens if you declare a constant called like a array index? What happens if the PHP guys decide to change it and throw an error for a undefined constant?

  10. #10
    SitePoint Enthusiast johnoz's Avatar
    Join Date
    Mar 2002
    Location
    here
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually, in the final version I will re-write the code but for now(since I am still developing it) I don't want to go back and edit all those echo statements(yet).

    Once I am sure that the script is what I want, I will re write optimize everything.

    Many thanks oder vielen dank.

    Quote Originally Posted by Gaheris
    So you're writing sloppy code but you don't care even if you know how to fix it? What happens if you declare a constant called like a array index? What happens if the PHP guys decide to change it and throw an error for a undefined constant?


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
  •