473,396 Members | 2,009 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

Sep 15 '07 #1
12 1458
On Sep 15, 10:32 am, Gandalf <goldn...@gmail.comwrote:
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]

Sep 15 '07 #2
Gandalf wrote:
On Sep 15, 10:32 am, Gandalf <goldn...@gmail.comwrote:
>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
Sep 15 '07 #3
On Sep 15, 11:07 am, gosha bine <stereof...@gmail.comwrote:
Gandalf wrote:
On Sep 15, 10:32 am, Gandalf <goldn...@gmail.comwrote:
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 $_COOKIE, $_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";
}

Sep 15 '07 #4
..oO(Gandalf)
>$var gets the value '_POST'
function create_par($var){
global $_COOKIE, $_POST, $active;
$_COOKIE, $_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
Sep 15 '07 #5
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.
Sep 16 '07 #6
..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
Sep 16 '07 #7
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.
Sep 16 '07 #8
..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 helpful - 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
Sep 16 '07 #9
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 helpful - 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'?
Sep 16 '07 #10
..oO(Sanders Kaufman)
>Michael Fesser wrote:
>But there's a similar thing that can be
really helpful - 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
Sep 16 '07 #11
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.
js*******@attglobal.net
==================
Sep 17 '07 #12
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.

Sep 17 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Scott | last post by:
I have an XML Document in a format like: <Variable name="Bob">ABCDEFG</Variable> <Variable name="Steve">QWERTYUI</Variable> <Variable name="John">POIUYTR</Variable> <Variable...
4
by: Frederik Sørensen | last post by:
I include a xslt stylesheet with variables for all the error messages in my system. <xsl:variable name="Banner_error_1"> errormessage 1 for banner </xsl:variable> <xsl:variable...
134
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
10
by: Blaxer | last post by:
There is probably a really easy way to do this, so please forgive me but I would like to set the value of a variable from a variable, an example would be... function Calculate_Something(ByVal...
23
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
3
by: rls03 | last post by:
I have the following which creates a variable containing a relative path where <xsl:value-of select="."/returns a portion of the filename: <xsl:variable...
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
2
by: Florian Loitsch | last post by:
hi, What should be the output of the following code-snippet? === var x = "global"; function f() { var x = 0; eval("function x() { return false; }"); delete x; alert(x); }
37
by: minkoo.seo | last post by:
Hi. I've got a question on the differences and how to define static and class variables. AFAIK, class methods are the ones which receives the class itself as an argument, while static methods...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.