PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > alt.php > Simple quote with variable question
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Simple quote with variable question

Réponse
 
LinkBack Outils de la discussion
Vieux 13/12/2007, 02h17   #1
Mike
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Simple quote with variable question

I am trying to find the documentation on how and why "id" is quoted in
the first example, but not in the second.

..............elseif($_GET['id'] == 'pro')............

.............."http://www.xxx.xxx.edu/~xxx/app_upd_2.php?id=
$_GET[id]&key=$_GET[key]";............

I found this, but it doesnt really tell me why the first id HAS to
have single qoutes and the second CAN'T:
When a string is specified in double quotes or with heredoc, variables
are parsed within it.

I'm just looking for the official explanation.
Thanks
Mike
  Réponse avec citation
Vieux 13/12/2007, 02h29   #2
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Simple quote with variable question

Mike wrote:
> I am trying to find the documentation on how and why "id" is quoted in
> the first example, but not in the second.
>
> .............elseif($_GET['id'] == 'pro')............
>
> ............."http://www.xxx.xxx.edu/~xxx/app_upd_2.php?id=
> $_GET[id]&key=$_GET[key]";............
>
> I found this, but it doesnt really tell me why the first id HAS to
> have single qoutes and the second CAN'T:
> When a string is specified in double quotes or with heredoc, variables
> are parsed within it.
>
> I'm just looking for the official explanation.
> Thanks
> Mike
>


The second instance is incorrect and should produce an E_NOTICE.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

  Réponse avec citation
Vieux 13/12/2007, 02h45   #3
Mike
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Simple quote with variable question

On Dec 12, 9:29 pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> Mike wrote:
> > I am trying to find the documentation on how and why "id" is quoted in
> > the first example, but not in the second.

>
> > .............elseif($_GET['id'] == 'pro')............

>
> > ............."http://www.xxx.xxx.edu/~xxx/app_upd_2.php?id=
> > $_GET[id]&key=$_GET[key]";............

>
> > I found this, but it doesnt really tell me why the first id HAS to
> > have single qoutes and the second CAN'T:
> > When a string is specified in double quotes or with heredoc, variables
> > are parsed within it.

>
> > I'm just looking for the official explanation.
> > Thanks
> > Mike

>
> The second instance is incorrect and should produce an E_NOTICE.
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> ==================- Hide quoted text -
>
> - Show quoted text -


No, it doesnt, in fact it will only work that way.I'm sure because I
spent hours on it. And I copied it from my working file.
  Réponse avec citation
Vieux 13/12/2007, 02h48   #4
Mike
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Simple quote with variable question

On Dec 12, 9:45 pm, Mike <ampel...@gmail.com> wrote:
> On Dec 12, 9:29 pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
>
>
>
>
>
> > Mike wrote:
> > > I am trying to find the documentation on how and why "id" is quoted in
> > > the first example, but not in the second.

>
> > > .............elseif($_GET['id'] == 'pro')............

>
> > > ............."http://www.xxx.xxx.edu/~xxx/app_upd_2.php?id=
> > > $_GET[id]&key=$_GET[key]";............

>
> > > I found this, but it doesnt really tell me why the first id HAS to
> > > have single qoutes and the second CAN'T:
> > > When a string is specified in double quotes or with heredoc, variables
> > > are parsed within it.

>
> > > I'm just looking for the official explanation.
> > > Thanks
> > > Mike

>
> > The second instance is incorrect and should produce an E_NOTICE.

>
> > --
> > ==================
> > Remove the "x" from my email address
> > Jerry Stuckle
> > JDS Computer Training Corp.
> > jstuck...@attglobal.net
> > ==================- Hide quoted text -

>
> > - Show quoted text -

>
> No, it doesnt, in fact it will only work that way.I'm sure because I
> spent hours on it. And I copied it from my working file.- Hide quoted text -
>
> - Show quoted text -


To be more specific:
if (isset($_SERVER['QUERY_STRING'])) {
$newquerystring = "http://www.xxxxxx.xxxxxxxx.edu/~xxxxxxx/
app_upd_2.php?id=$_GET[id]&key=$_GET[key]";
$updateGoTo = $newquerystring;
}
header(sprintf("Location: %s", $updateGoTo));
}
  Réponse avec citation
Vieux 13/12/2007, 03h40   #5
Norman Peelman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Simple quote with variable question

Mike wrote:
> I am trying to find the documentation on how and why "id" is quoted in
> the first example, but not in the second.
>
> .............elseif($_GET['id'] == 'pro')............
>
> ............."http://www.xxx.xxx.edu/~xxx/app_upd_2.php?id=
> $_GET[id]&key=$_GET[key]";............
>
> I found this, but it doesnt really tell me why the first id HAS to
> have single qoutes and the second CAN'T:
> When a string is specified in double quotes or with heredoc, variables
> are parsed within it.
>
> I'm just looking for the official explanation.
> Thanks
> Mike


When inside double quotes, PHP doesn't look for defined constants so
you don't need to enclose associative array keys in quotes... unless you
bracket them:

--code--

error-reporting(E_ALL); // everyone knows this constant so we'll use it.

$arr['E_ALL'] = 'world'; // array key is 'E_ALL' not E_ALL
echo "Hello $arr[E_ALL].<br>"; // no errors - PHP does not confuse them.

echo "Hello {$arr['E_ALL']}.<br>"; // no errors - PHP does not confuse them.

echo "Hello {$arr[E_ALL]}.<br>"; // now errors - PHP assumes the defined
constant.

echo "Hello ".$arr[E_ALL]."<br>"; // now errors - PHP assumes the
defined constant.

--end code--

In example #1 the array key name is technically already quoted by the
double quotes (in which php scans for variables) so quotes are not
needed.PHP does not look for defined constants while inside double quotes.
In example #2, the array variable has been isolated with brackets
(which tells PHP to resolve the variable as if it were not in double
quotes and so requires quotes around the key name or PHP assumes the
defined constant.
Examples #3 & #4 show the errors that are thrown when quotes are not
used properly.

It's all in the quotes and brackets:

echo "Hello $arr[E_ALL]";
echo "Hello ".$arr['E_ALL'];
echo "Hello {$arr['E_ALL']}";

....all have the same output.

Also to wrap up, you need to use brackets for multi-dimensional
arrays when inside double quotes:

$arr['cars']['chevy'] = 'Corvette';
echo "MY favorite car is the $arr[cars][chevy].<br>";

....will not have the desired output but (php only sees the first
dimension of the array),

echo "MY favorite car is the {$arr['cars']['chevy']}.<br>";

....does.

---
Norm
  Réponse avec citation
Vieux 13/12/2007, 04h07   #6
Michael Fesser
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Simple quote with variable question

..oO(Mike)

>I am trying to find the documentation on how and why "id" is quoted in
>the first example, but not in the second.
>
>.............elseif($_GET['id'] == 'pro')............
>
>............."http://www.xxx.xxx.edu/~xxx/app_upd_2.php?id=
>$_GET[id]&key=$_GET[key]";............
>
>I found this, but it doesnt really tell me why the first id HAS to
>have single qoutes and the second CAN'T:
>When a string is specified in double quotes or with heredoc, variables
>are parsed within it.
>
>I'm just looking for the official explanation.


It's described in the array type section. See the third example there:

http://www.php.net/manual/en/languag...es.array.donts

Micha
  Réponse avec citation
Vieux 13/12/2007, 13h15   #7
Mike
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Simple quote with variable question

On Dec 12, 11:07 pm, Michael Fesser <neti...@gmx.de> wrote:
> .oO(Mike)
>
> >I am trying to find the documentation on how and why "id" is quoted in
> >the first example, but not in the second.

>
> >.............elseif($_GET['id'] == 'pro')............

>
> >............."http://www.xxx.xxx.edu/~xxx/app_upd_2.php?id=
> >$_GET[id]&key=$_GET[key]";............

>
> >I found this, but it doesnt really tell me why the first id HAS to
> >have single qoutes and the second CAN'T:
> >When a string is specified in double quotes or with heredoc, variables
> >are parsed within it.

>
> >I'm just looking for the official explanation.

>
> It's described in the array type section. See the third example there:
>
> http://www.php.net/manual/en/languag...language.types....
>
> Micha


Great !
Thanks
Mike
  Réponse avec citation
Vieux 13/12/2007, 19h17   #8
Tom
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Simple quote with variable question

On Thu, 13 Dec 2007 05:07:24 +0100, Michael Fesser wrote...
>
>.oO(Mike)
>
>>I am trying to find the documentation on how and why "id" is quoted in
>>the first example, but not in the second.
>>
>>.............elseif($_GET['id'] == 'pro')............
>>
>>............."http://www.xxx.xxx.edu/~xxx/app_upd_2.php?id=
>>$_GET[id]&key=$_GET[key]";............
>>
>>I found this, but it doesnt really tell me why the first id HAS to
>>have single qoutes and the second CAN'T:
>>When a string is specified in double quotes or with heredoc, variables
>>are parsed within it.
>>
>>I'm just looking for the official explanation.

>
>It's described in the array type section. See the third example there:
>
>http://www.php.net/manual/en/languag...es.array.donts
>
>Micha



Since I started off learning Perl before PHP, I had gotten into the habit of
quoting any array keys that weren't numeric or a variable. Thanks for the link.

Tom
--
NewsGuy Accounts Go Jumbo!
NewsGuy Express increased from 30 to 50 GB of download capacity
http://newsguy.com/overview.htm

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h14.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,24765 seconds with 16 queries