PHWinfo banniere

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

Réponse
 
LinkBack Outils de la discussion
Vieux 15/09/2007, 10h32   #1
Gandalf
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut variable

I don't understand why this expression doesn't work:

$var= '$_POST';

$name= ${$var}[name];


the ${$var}[name] expression wont return the same expression of
$_POST[name].
why does it happens, and how can i solve it?

thanks

  Réponse avec citation
Vieux 15/09/2007, 10h44   #2
Gandalf
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: variable

On Sep 15, 10:32 am, Gandalf <goldn...@gmail.com> wrote:
> I don't understand why this expression doesn't work:
>
> $var= '$_POST';
>
> $name= ${$var}[name];
>
> the ${$var}[name] expression wont return the same expression of
> $_POST[name].
> why does it happens, and how can i solve it?
>
> thanks


i meant to write $var ='_POST',
and the that ${$var}[name] wont return the same value as $_POST[name]

  Réponse avec citation
Vieux 15/09/2007, 11h07   #3
gosha bine
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: variable

Gandalf wrote:
> On Sep 15, 10:32 am, Gandalf <goldn...@gmail.com> wrote:
>> I don't understand why this expression doesn't work:
>>
>> $var= '$_POST';
>>
>> $name= ${$var}[name];
>>
>> the ${$var}[name] expression wont return the same expression of
>> $_POST[name].
>> why does it happens, and how can i solve it?
>>
>> thanks

>
> i meant to write $var ='_POST',
> and the that ${$var}[name] wont return the same value as $_POST[name]
>


hi

this should work as expected. Care to post more code?

--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
  Réponse avec citation
Vieux 15/09/2007, 11h22   #4
Gandalf
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: variable

On Sep 15, 11:07 am, gosha bine <stereof...@gmail.com> wrote:
> Gandalf wrote:
> > On Sep 15, 10:32 am, Gandalf <goldn...@gmail.com> wrote:
> >> I don't understand why this expression doesn't work:

>
> >> $var= '$_POST';

>
> >> $name= ${$var}[name];

>
> >> the ${$var}[name] expression wont return the same expression of
> >> $_POST[name].
> >> why does it happens, and how can i solve it?

>
> >> thanks

>
> > i meant to write $var ='_POST',
> > and the that ${$var}[name] wont return the same value as $_POST[name]

>
> hi
>
> this should work as expected. Care to post more code?
>
> --
> gosha bine
>
> extended php parser ~http://code.google.com/p/pihipi
> blok ~http://www.tagarga.com/blok


$var gets the value '_POST'
function create_par($var){
global $_, $_POST, $active;

if(${$var}[no_win_from]!=0 || ${$var}[no_win_to]!=${$var}[games])
$active[0]="no_win"; else die(${$var}[games]);
if(${$var}[out_win_from]!=0 || ${$var}[out_win_to]!=${$var}[games])
$active[1]="out_win";

if(${$var}[yedaActive_2]=="true"){
if(${$var}[range_yeda1_from]!=0 || ${$var}[range_yeda1_to]!=${$var}
[games])$active[2]="range_yeda1";
}
if(${$var}[yedaActive_3]=="true"){
if(${$var}[range_yeda2_from]!=0 || ${$var}[range_yeda2_to]!=${$var}
[games]) $active[3]="range_yeda2";
}
if(${$var}[home_win_from]!=0 || ${$var}[home_win_to]!=${$var}[games])
$active[4]="home_win";
}



  Réponse avec citation
Vieux 15/09/2007, 20h09   #5
Michael Fesser
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: variable

..oO(Gandalf)

>$var gets the value '_POST'
>function create_par($var){
> global $_, $_POST, $active;


$_, $_POST etc. are always available, no need to use 'global' on
them. But:

| Please note that variable variables cannot be used with PHP's
| Superglobal arrays within functions or class methods.

Micha
  Réponse avec citation
Vieux 16/09/2007, 04h51   #6
Sanders Kaufman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: variable

Michael Fesser wrote:

> | Please note that variable variables cannot be used with PHP's
> | Superglobal arrays within functions or class methods.


I read that, and re-read it, and re-read it... and I can't figure out
what it means. Can you re-phrase it?

I was good right up to "within functions...". I use super globals
within functions all the time.
  Réponse avec citation
Vieux 16/09/2007, 13h36   #7
Michael Fesser
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: variable

..oO(Sanders Kaufman)

>Michael Fesser wrote:
>
>> | Please note that variable variables cannot be used with PHP's
>> | Superglobal arrays within functions or class methods.

>
>I read that, and re-read it, and re-read it... and I can't figure out
>what it means. Can you re-phrase it?
>
>I was good right up to "within functions...". I use super globals
>within functions all the time.


A variable variable is something like that:

$foo = 'bar';
$bar = 42;

print ${$foo}; // prints 42

This means the name of the variable is taken from another (string)
variable. The same can be done with superglobals, but not if you're
inside a function or method:

function test() {
$foo = '_GET';
var_dump(${$foo}); // throws a notice
}

Micha
  Réponse avec citation
Vieux 16/09/2007, 22h46   #8
Sanders Kaufman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: variable

Michael Fesser wrote:

> A variable variable is something like that:
>
> $foo = 'bar';
> $bar = 42;
>
> print ${$foo}; // prints 42
>
> This means the name of the variable is taken from another (string)
> variable. The same can be done with superglobals, but not if you're
> inside a function or method:
>
> function test() {
> $foo = '_GET';
> var_dump(${$foo}); // throws a notice
> }


I think that what's confusing me is that double-dollar thing.
I've seen it before, but thought it was a typo.

Could you tell me what it means, or where to look in the docs to find
out about it.
  Réponse avec citation
Vieux 16/09/2007, 23h15   #9
Michael Fesser
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: variable

..oO(Sanders Kaufman)

>I think that what's confusing me is that double-dollar thing.
>I've seen it before, but thought it was a typo.
>
>Could you tell me what it means, or where to look in the docs to find
>out about it.


It's called a variable variable. As said - instead of accessing a
variable directly, it's done by taking the name of the variable from
_another_ string variable:

$foo = 42;
print $foo; // prints 42
$bar = 'foo';
print ${$bar}; // prints 42, too

The latter accesses a variable whose name is stored in another variable.

http://www.php.net/manual/en/languag...s.variable.php

Usuallly it's considered bad style to use variable variables, in most
cases there's a better way. But there's a similar thing that can be
really ful - variable class names, i.e. the class name is stored in
a variable:

$foo = 'TMyClass';
$bar = new $foo(); // creates an instance of the class TMyClass

Micha
  Réponse avec citation
Vieux 16/09/2007, 23h53   #10
Sanders Kaufman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: variable

Michael Fesser wrote:

> It's called a variable variable. As said - instead of accessing a
> variable directly, it's done by taking the name of the variable from
> _another_ string variable:


Oh, WOW!!! That is so cool.
I was kinda thinkin about doing something like that all hodge-podge with
"eval" statements, but figured it was too hair-brained.



> Usuallly it's considered bad style to use variable variables, in most
> cases there's a better way.


So... it is a little hair-brained, eh?
Still - I'll have some fun exploring that.


> But there's a similar thing that can be
> really ful - variable class names, i.e. the class name is stored in
> a variable:
>
> $foo = 'TMyClass';
> $bar = new $foo(); // creates an instance of the class TMyClass


Now I'm confused again.
Maybe I'm applying Java logic here or something.
Why doesn't that just create a new string containing, 'TMyClass'?
  Réponse avec citation
Vieux 17/09/2007, 00h41   #11
Michael Fesser
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: variable

..oO(Sanders Kaufman)

>Michael Fesser wrote:
>
>> But there's a similar thing that can be
>> really ful - variable class names, i.e. the class name is stored in
>> a variable:
>>
>> $foo = 'TMyClass';
>> $bar = new $foo(); // creates an instance of the class TMyClass

>
>Now I'm confused again.
>Maybe I'm applying Java logic here or something.
>Why doesn't that just create a new string containing, 'TMyClass'?


Because PHP is not Java.

In this case the name of the class is taken from the variable (notice
the parentheses after the variable name). The same can also be done with
functions, which is a common way to implement callback functions in PHP.

Micha
  Réponse avec citation
Vieux 17/09/2007, 04h11   #12
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: variable

Sanders Kaufman wrote:
> Michael Fesser wrote:
>
>> A variable variable is something like that:
>>
>> $foo = 'bar';
>> $bar = 42;
>>
>> print ${$foo}; // prints 42
>>
>> This means the name of the variable is taken from another (string)
>> variable. The same can be done with superglobals, but not if you're
>> inside a function or method:
>>
>> function test() {
>> $foo = '_GET';
>> var_dump(${$foo}); // throws a notice
>> }

>
> I think that what's confusing me is that double-dollar thing.
> I've seen it before, but thought it was a typo.
>
> Could you tell me what it means, or where to look in the docs to find
> out about it.


Sanders,

Not unusual. It is confusing, and IMHO not a good programming
technique, IMHO.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
  Réponse avec citation
Vieux 17/09/2007, 04h36   #13
Sanders Kaufman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: variable

Jerry Stuckle wrote:
> Sanders Kaufman wrote:


>> I think that what's confusing me is that double-dollar thing.
>> I've seen it before, but thought it was a typo.
>>
>> Could you tell me what it means, or where to look in the docs to find
>> out about it.

>
> Not unusual. It is confusing, and IMHO not a good programming
> technique, IMHO.


Yeah - after contemplating it, I think maybe it's an accident that the
PHP dev guys just decided to feature, rather than fix.

  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 01h36.


É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 ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,16611 seconds with 21 queries