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

 

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

?>

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.