You are here: Re: Foreach displaying data twice « PHP Programming Language « IT news, forums, messages
Re: Foreach displaying data twice

Posted by Schraalhans Keukenmeester on 06/01/07 19:28

At Fri, 01 Jun 2007 19:29:46 +0300, Rami Elomaa let h(is|er) monkeys type:

> Schraalhans Keukenmeester kirjoitti:

>>> ********************************
>>> $OpenQ = mysql_query("SELECT column FROM table WHERE q_uid =
>>> $_SESSION[userid] AND status = 1",$db); // SQL query on DB returns 1
>>> result
>>
>> Rami has answered your question. I just wondered whether:
>> a) userid is a defined constant. If not, it should be in quotes.
>
> I used to think this too, but after reading the manual once again
> carefully, I discovered that this is not the case. Singlequotes in array
> keys aren't required when inside double quotes. It will not look for the
> constant, _unless_ you also use curly braces around it.
>
> $foo = "$array[key]"; // This is right, assumes key is a string
>
> $foo = "{$array[key]}"; // this assumes key is a constant, then falls
> back to string, so it'll trigger some stupid notice..
>
> $foo = "{$array['key']}"; // This again is right
>
> $foo = "$array['key']"; // quotes without curly braces is wrong, parse
> error.
>
> The thing is, if you're calling a multidimensional array, you gotta use
> the curly braces, and that forces you to use the single quotes:
> $foo = "{$array[0]['key']}";
>
> It's all here in the better book:
> http://fi.php.net/manual/en/language.types.string.php

Thanks for setting me straight Rami! Just checked and found it works the
other way around as well: if you DO use single quotes around an array
index inside a double-quoted string curly's are required, lest php throws
an error.
$arr = array('foo'=>'bar');
echo "I met my friends in the $arr[foo]";
I met my friends in the bar
echo "I met my friends in the $arr['foo']";
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE,
expecting T_STRING or T_VARIABLE or T_NUM_STRING
echo "I met my friends in the {$arr['foo']}";
I met my friends in the bar

SOmehow I never thought about the reason why I had to em-brace
one-dimensional array references inside "strings" or heredoc, which also
expands variables of course.

I think I prefer consistent use of curly braces in these situations, and
have the same syntax with any kind of array. Personal taste & habit I
guess.

Never too old to learn.
Rgds

--
Schraalhans Keukenmeester - schraalhans@the.Spamtrapexample.nl
[Remove the lowercase part of Spamtrap to send me a message]

"strcmp('apples','oranges') < 0"

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация