Displaying PHP Variables in Quotes, Arrays

Posted by: scoopseven 15 years, 9 months ago

Trying to output a variable without using dot (period) syntax. It's much easier to output your variables within quotes. This code works:
while ($element = each( $query)) { echo "my text" . $element[value][query_column] ; }
but I have a whole paragraph to format like this, so it's really a PITA to write . myvar . "sometext" . myVar, etc. and get everything formatted correctly. Below are some things I tried before finding the solution below. this code...
while ($element = each( $query)) { echo "my text $element['value']['query_column']" ; }
throws this error (because of the single quotes): Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in Topmenu.php on line 113 and this code (single quotes removed):
while ($element = each( $query)) { echo "my text $element[value][query_column]" ; }
displays "my text Array[query_column]" instead of the results in that query_column. Almost like I need a evaluate function or something. Turns out that when you're displaying the contents of an array within quotes, you need to wrap your variable in curly quotes. It's always best to use single quotes around array indexes too. This is a little faster, as PHP doesn't first look to see if the string has been defined as a constant, or is just a string index name. The final working code looks like this:
while ($element = each( $query)) { echo "my text {$element['value']['query_column']}" ; }

Currently unrated


Recent Tweets

Recent Posts

Archive

2013
2012
2011
2010
2009
2008
2007
2006

Categories

Authors

Feeds

RSS / Atom