Jump to content

problem on a basic vairable of variable.


Majes

Recommended Posts

<?

$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.

Link to comment
https://forums.phpfreaks.com/topic/50838-problem-on-a-basic-vairable-of-variable/
Share on other sites

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>");

 

?>

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.

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

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''

I've confused <P> and <BR>... Today it's not my day :P

 

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)";

?>

 

 

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

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;
?>

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;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.