How do you embed PHP commands inside another PHP variable set?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phil Powell

    How do you embed PHP commands inside another PHP variable set?

    I would like a very nice PHP shortcut to dynamically setting a
    variable value by embedding PHP commands inside, which is what I can
    easily do in TCL:

    set stuff "hello [if {[regexp {/dev} $env(HTTP_HOST)]} {set x "
    devworld"} else {set x " world"}]"

    in TCL this would set stuff to either "hello world" or, if you're in
    /dev "hello devworld"

    how can I accomplish this great TCL feature in PHP? I haven't found a
    way yet, everything I have done has so far produced parse errors:

    $stuff = "hello" . (if (preg_match('/\/dev/i', $HTTP_HOST)) { $stuff
    ..= ' devworld'; } else { $stuff .= ' world'; });

    Phil
  • Jeffrey Silverman

    #2
    Re: How do you embed PHP commands inside another PHP variable set?

    On Tue, 30 Sep 2003 10:03:22 -0700, Phil Powell wrote:
    <snip!>[color=blue]
    > $stuff = "hello" . (if (preg_match('/\/dev/i', $HTTP_HOST)) { $stuff .= '
    > devworld'; } else { $stuff .= ' world'; });
    >
    > Phil[/color]

    try it like this:

    // Separate statments into two. I don't think an if..else statment returns
    // anything and thus you are concatenating NULL onto the end of $stuff.
    $stuff = "hello";
    if (preg_match('/\/dev/i', $HTTP_HOST) {
    $stuff .= 'devworld';
    } else {
    $stuff .= ' world';
    }


    OR like this:

    $stuff="hello" .
    (preg_match('/\/dev/i', $HTTP_HOST) ? 'devworld' : 'world';
    // Line is broken after concat operator to fit it in 80 columns!

    Warning: I have not tested either solution!

    later...
    --
    Jeffrey D. Silverman | jeffrey AT jhu DOT edu
    Johns Hopkins University | Baltimore, MD
    Website | http://www.wse.jhu.edu/newtnotes/

    Comment

    Working...