Neoseeker : Blogs : Chiggins : Wtf is this PHP?

And lord monkey says...

Wtf is this PHP?

I was taking a look at another blog site I found today, and this one article caught my eye. It was about PHP variables. Now if you wanted to use a variable in a string, you would use one of the following:

php code

$food = "chicken";

echo "Bobby ate some . " $food;
echo "Bobby ate some $food";
 


Well, this blog article introduced me into another way of using a variable in a string:

php code

$food = "chicken";

echo "Bobby ate some {$food}";
echo "Bobby ate some ${food}";
 


Wtf is this PHP? How come you never told me about this before? I thought we had something special. :(

Source: Catch My Frame Article

Comments

  • 0 thumbs!
    huntyr since Feb 2007 | Feb 3, 09
    its a secret php thing only the cool kids know.

    So, now you're a cool kid ;-)

    I use it all the time, but I find "Bobby ate some {$food}";
    to be more unreadable than its worth so I usually seperate it to
    code

    'Bobby ate some ' .$food;

    because $food is usually some long object string
    • 0 thumbs!
      Chiggins since Jun 2003 | Feb 4, 09
      Yay, I'm cool.

      I'm still going to code using "Bobby ate some " . $food; though. I still think it to be at least a little bit easier to read.
  • 0 thumbs!
    bobbonew since Dec 2002 | Feb 4, 09
    Hmmm interesting. In my opinion huntyr, using it like {$food} in a string would seem slightly more readable for me.

    Perhaps this is just a variation of using variable variables, but in this case its still just one var.

    Good find LM
    Last edited by bobbonew :: Feb 4, 09
  • 0 thumbs!
    Xenctuary since May 2001 | Feb 14, 09
    I didn't know about this. Looks a lot more readable than concatenation to me. Very nice find.
  • 0 thumbs!
    gdog2011 since Feb 2003 | Apr 20, 09
    Wow, I didn't actually know this either. Can't say I'll change my ways, but it is nice to learn something new.
  • 0 thumbs!
    Armed Rebel since Apr 2003 | Apr 20, 09
    I knew about this.
  • 0 thumbs!
    Vermillion since Jan 2006 | Jun 27, 09
    I learned about it in my Learning PHP book a few years ago D:.
Add your comment:
Name *:  Members, please LOGIN before posting
Email:
Live user
verification *:

Enter the letters you see in the image (without spaces)
Comment *:
(0.1502/d/aeon)