#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2009
    Posts
    71
    Rep Power
    9

    When do you use ' '


    When in php do you use the single apostrophes? When you want whatever it is you're doing to treat the element as a string correct? There is an example in my book where we defined constants (e.g. CONSTANT1 --> define('CONSTANT1','blah')) and then in a function, mysql_connect specifically, passed the constants as arguments without the ' ' literals characters. So I'm a little wishy washy on exactly when and when not to use these things.

    Thanks
  2. #2
  3. Permanently Banned
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2006
    Location
    In a whale
    Posts
    4,127
    Rep Power
    0
    With the quotes they are a string. Without the quotes, PHP will look for a constant named that; if one isn't found, then it is treated as a string.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Nov 2001
    Location
    North West UK
    Posts
    1,147
    Rep Power
    398
    The difference is pretty straightforward
    PHP Code:
    define('CONSTANT1','blah');

    myFunction($txt) {
       echo 
    $txt;
    }

    myFunction('CONSTANT1');    //  echoes CONSTANT1
    myFunction(CONSTANT1);      //  echoes blah
    myFunction(blah);           //  Gives a notice and then echoes blah 

    Comments on this post

    • ManiacDan agrees
    9 out of 10 PHP problems can be resolved by setting
    PHP Code:
    error_reporting(E_ALL);
    ini_set('display_errors'1); 
    php -l <filename> will identify 9 out of the remaining 10 problems
    Remember, the command line is your friend

    Development Projects:
    PHPExcel
    PHPPowerPoint
  6. #4
  7. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,396
    Rep Power
    9645
    Just to spell it out:

    When you're trying to define the constant it doesn't exist yet. Saying
    PHP Code:
    define(CONSTANT1"value"); 
    won't work because PHP tries to look up CONSTANT1 before calling the function. You have to pass it as a string so that PHP doesn't try to do anything; then define() will take the string value you gave it and create a constant.
  8. #5
  9. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,659
    Rep Power
    4127
    Both single and double quote marks delimit strings

    using double quote marks means you can concatenate variables inside strings

    eg

    $foo = 'bar';

    $str = "Here is a $foo example";

    $str = 'Here is a '.$foo.' example';

    now then,

    define() is a function that takes two strings. it adds a constant to the global scope with the name of the first string and the value of the second string, eg

    PHP Code:
    define("TEST_CONSTANT","HELLO");

    echo 
    TEST_CONSTANT// displays HELLO;

    define(TEST_CONSTANT,"WORLD");

    echo 
    TEST_CONSTANT//displays HELLO (as above)
    echo HELLO//displays WORLD (as there is now a constant called HELLO as it was passed the the define function as the value of TEST_CONSTANT and given the value "WORLD" 
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2007
    Location
    Glendale AZ
    Posts
    230
    Rep Power
    96
    And let's not forget...
    PHP Code:
    $answer "PHP Power";
    echo 
    "The answer is $answer."// echos "The answer is PHP Power."

    echo 'The answer is $answer.'// echos "The answer is $answer." 

    Comments on this post

    • woger agrees : Just what I was looking for

IMN logo majestic logo threadwatch logo seochat tools logo