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

    Join Date
    Dec 2005
    Location
    Vancouver, WA, USA
    Posts
    397
    Rep Power
    189

    Constant interpretation


    I'm sure I know the answer already, but I've thought it was needed many times....

    Is there any way to interpret a constant within a string, like we do with other strings and arrays? Example:

    PHP Code:
    $works='lazy ';
    define('TESTING','brown ');
    echo 
    "The quick {TESTING}fox jumped over the {$works}dog"
    Thomas Tremain
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,992
    Rep Power
    9397
    No. PHP needs to see a $ and constants don't have any.

    Closest you can get is using constant and variable variables but I recommend you don't do this.
    PHP Code:
    $c "constant"// the function constant()
    echo "The quick {$c("TESTING")}fox jumped over the {$works}dog"
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2005
    Location
    Vancouver, WA, USA
    Posts
    397
    Rep Power
    189
    I like that even less than:

    PHP Code:
    echo "The quick ".TESTING."fox jumped over the {$works}dog"
    Thomas Tremain
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,850
    Rep Power
    6351
    You could do it the other way around:

    PHP Code:
    define("CONSTANT""The quick brown fox jumped over the %s dog.");

    $fill "lazy";
    printf(CONSTANT$fill); 
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2005
    Location
    Vancouver, WA, USA
    Posts
    397
    Rep Power
    189
    If I started using printf() I would no longer have the need....

    PHP Code:
    $works='lazy ';
    define('TESTING','brown ');

    printf("The quick %s fox jumped over the %s dog",TESTING,$works); 
    Something to consider... To me, it's less readable....
    Thomas Tremain

IMN logo majestic logo threadwatch logo seochat tools logo