Page 1 of 1

Some basic question about PHP

Posted: Mon Jan 17, 2011 7:14 pm
by paleredstream
Hello
I've just started to learn PHP and had somewhat too basic question about language
I'll show code first

Code: Select all

#Code start
$First = "name1";

$$First = "IVR Test";
printf("%s\n", $name1);

$First = "name2";		
#unset($First)		<- problem code
#printf("%s\n", $name2); <- problem code
			
printf("%s\n", $name1);
# code ends



I'm testing indirect referencing, and when I uncomment "unset($First)", then interpreter gives error about
last printf($name1)
I'm curious
1. If indirect referencing(IR from here...) work by string "value",
(I have to guess like this because of printf($name1), where "name1" was the string value of $First)

then why unsetting $first affects last printf?
because I guess string value(not variable itself) is already used as name, and now $$First is nothing to do with $First

2. If IR work by referencing $First variable itself(somewhat like a pointer, or "real reference"), then why string value is necessary?
why just not print($$First)?

3. I changed value of %First to "name2" AFTER I assign IR,
but i can't access to $$First with printf("%s\n", $name2);
I can't understand this with either 1 or 2

Excuse my poor English, thank in advance

Re: Some basic question about PHP

Posted: Tue Jan 18, 2011 2:18 am
by Peter Kelly
I've never used printf before I don't think but I would also like to know, as I'm not sure why you have done the $$First.