Majes Posted May 10, 2007 Share Posted May 10, 2007 <? $majes=18986; $$majes="fecha de nacimiento de majes '' to pegada'' "; echo ("el valor acual de ajes es : " . $majes . "<p>"); $majes+=4; echo ("ahora que he añadido 4 al numero de majes, el resultado es éste" . $majes . "<p>"); echo ("Pero al fin y al cabo, que es este numero? : " $majes . " es la " . $$majes . "<p>"); ?> That's it. Why does it give me the error: Parse error: parse error, unexpected T_VARIABLE in /home/content/m/e/h/mehadejado/html/php/2.php on line 21 Line 21= the last echo. Why could it be ? cheers. Quote Link to comment https://forums.phpfreaks.com/topic/50838-problem-on-a-basic-vairable-of-variable/ Share on other sites More sharing options...
bbaker Posted May 10, 2007 Share Posted May 10, 2007 you missed your period....see red dot below <? $majes=18986; $$majes="fecha de nacimiento de majes '' to pegada'' "; echo ("el valor acual de ajes es : " . $majes . "<p>"); $majes+=4; echo ("ahora que he añadido 4 al numero de majes, el resultado es éste" . $majes . "<p>"); echo ("Pero al fin y al cabo, que es este numero? : " . $majes . " es la " . $$majes . "<p>"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/50838-problem-on-a-basic-vairable-of-variable/#findComment-250003 Share on other sites More sharing options...
Majes Posted May 10, 2007 Author Share Posted May 10, 2007 stupid mistake !!! Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/50838-problem-on-a-basic-vairable-of-variable/#findComment-250004 Share on other sites More sharing options...
Majes Posted May 10, 2007 Author Share Posted May 10, 2007 Gasp! It doesn't output $$majes Why could it be? el valor acual de ajes es : 18986 ahora que he añadido 4 al numero de majes, el resultado es éste18990 Pero al fin y al cabo, que es este numero? : 18990 es la Quote Link to comment https://forums.phpfreaks.com/topic/50838-problem-on-a-basic-vairable-of-variable/#findComment-250008 Share on other sites More sharing options...
bbaker Posted May 10, 2007 Share Posted May 10, 2007 i didn't even realize the two $$. Try changing it to something else. Instead of $$majes, try $majes1 (only 1 $) Quote Link to comment https://forums.phpfreaks.com/topic/50838-problem-on-a-basic-vairable-of-variable/#findComment-250012 Share on other sites More sharing options...
Majes Posted May 10, 2007 Author Share Posted May 10, 2007 In fact I don't realize it either, it's wrote in some documents which my teacher gave us... Ill ask her, because it's running me mad Thank you Quote Link to comment https://forums.phpfreaks.com/topic/50838-problem-on-a-basic-vairable-of-variable/#findComment-250014 Share on other sites More sharing options...
genericnumber1 Posted May 10, 2007 Share Posted May 10, 2007 no, it's not that bbaker, he's meaning to use the horribly messy and frowned upon $$ his problem is that he is changing the value of $majes, $majes+=4, thus, changing the pointer variable.... when you call the second $$majes, it's an undefined variable because you added 4 to $majes.... just use a simple variable! $$ is just messy coding here. Quote Link to comment https://forums.phpfreaks.com/topic/50838-problem-on-a-basic-vairable-of-variable/#findComment-250015 Share on other sites More sharing options...
Majes Posted May 10, 2007 Author Share Posted May 10, 2007 Yeah, I know it's kinda messy. Thanks, I thought it was the fact of adding 4 to the variable, but I thought that if it's a variable it should accept manipulations. So it's not essential to know this variable method, right? Many thank yous And sorry for my non-trained english Quote Link to comment https://forums.phpfreaks.com/topic/50838-problem-on-a-basic-vairable-of-variable/#findComment-250026 Share on other sites More sharing options...
bbaker Posted May 10, 2007 Share Posted May 10, 2007 I think he's trying to use 2 variable here, not just 1. $majes = 18986; $$majes1 = "fecha de nacimiento de majes '' to pegada'' "; after the first echo, he's adding 4 to $majes, but still expects $$majes1 = "fecha de nacimiento de majes '' to pegada'' "; And yes, the code is quite messy. what are the <p> for? I think you might want to change this code to something like this: <?php $majes = 18986; $majes1 = "fecha de nacimiento de majes '' to pegada'' "; echo "<p>el valor acual de ajes es : $majes</p>"; $majes+=4; echo "<p>ahora que he añadido 4 al numero de majes, el resultado es éste $majes</p>"; echo "<p>Pero al fin y al cabo, que es este numero? : $majes es la $majes1</p>"; ?> The above will output this: el valor acual de ajes es : 18986 ahora que he añadido 4 al numero de majes, el resultado es éste 18990 Pero al fin y al cabo, que es este numero? : 18990 es la fecha de nacimiento de majes '' to pegada'' Quote Link to comment https://forums.phpfreaks.com/topic/50838-problem-on-a-basic-vairable-of-variable/#findComment-250028 Share on other sites More sharing options...
Majes Posted May 10, 2007 Author Share Posted May 10, 2007 I've confused <P> and <BR>... Today it's not my day Why am I using "$$" variables? based upon this code: <?php $lol="green"; $$lol="grass"; ?> You can use: <?php echo ("the grass is: " . $green); ?> Or at least, it says so in this document... :S (I have not tested it) Anyway, as you all say, I'ts preferable to use two dirrerent variables as: <?php $majes="Me"; $majes_definition="extremely pro coding person (joke)"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/50838-problem-on-a-basic-vairable-of-variable/#findComment-250040 Share on other sites More sharing options...
genericnumber1 Posted May 10, 2007 Share Posted May 10, 2007 well yes, you can do that... but consider this code... <?php $variableName = 'joe'; $$variableName = 'blah'; echo $$variableName; // blah $variableName = 'sally'; echo $$variableName; // undefined, there is no $sally ?> as I explained above, this was your problem, you incremented your pointer variable, so it ended up being undefined when you used it as a variable name. avoid variables of variables! $$ is evil Quote Link to comment https://forums.phpfreaks.com/topic/50838-problem-on-a-basic-vairable-of-variable/#findComment-250043 Share on other sites More sharing options...
Majes Posted May 10, 2007 Author Share Posted May 10, 2007 I understand , thx. Anyway... where could it be useful? it's like a constant... or even worse, because you can "break" the code... Quote Link to comment https://forums.phpfreaks.com/topic/50838-problem-on-a-basic-vairable-of-variable/#findComment-250047 Share on other sites More sharing options...
per1os Posted May 10, 2007 Share Posted May 10, 2007 I think he is getting mixed signals here, maybe this explains it better. <?php $myVar = "testing"; $$myVar = "One Two Three"; // unsure if this part works but yea echo $testing; // prints "One Two Three" ?> As far as I know the $$ means take the value that is housed in the string and make it it's own variables. EDIT: The code can be very useful when you have an array that you do not want to be an array and you want those values assigned to a variable without alot of extra work. IE: <?php $array = array("one" => 1, "two" => 2); foreach ($array as $key => $val) { $$key = $val; } print $one . '<br />'; print $two; ?> Quote Link to comment https://forums.phpfreaks.com/topic/50838-problem-on-a-basic-vairable-of-variable/#findComment-250049 Share on other sites More sharing options...
genericnumber1 Posted May 10, 2007 Share Posted May 10, 2007 The code can be very useful when you have an array that you do not want to be an array and you want those values assigned to a variable without alot of extra work. nope, that's what extract() is for <?php $array = array("one" => 1, "two" => 2); extract($array); echo $one; echo $two; Quote Link to comment https://forums.phpfreaks.com/topic/50838-problem-on-a-basic-vairable-of-variable/#findComment-250195 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.