Pergunta

i'm doing this code from the book and i can't seem to get it to work

<html>
<head> <h3> <u> PHP tutorials  </u> </h3> </head>
<body>

<?php

$Texas = "large";
$RhodeIsland = "small";

$statement = "Texas";
echo "$statement is $($statement)<br>";

$statement = "RhodeIsland";
echo "$statement is $($statement)<br>";

?>
</body>
</html>

this is the output i keep getting:

Texas is $(Texas)

RhodeIsland is $(RhodeIsland)

instead of:

Texas is large

RhodeIsland is small

Foi útil?

Solução

The syntax is : ${$variable_name}, not $($variable_name). You need to change your code to:

$statement = "Texas";
echo "$statement is ${$statement}<br>";

See the PHP manual documentation on Variable Variables for more information.

Demo

Outras dicas

There you go:

 <?php

    $Texas = "large";
    $RhodeIsland = "small";

    $statement = "Texas";
    echo $statement." is ".${$statement}."<br />";

    $statement = "RhodeIsland";
    echo $statement." is ".${$statement} ."<br />";

    ?>

You should be using curly brackets instead of parentheses.

$statement = "RhodeIsland";
echo "$statement is ${$statement}<br>";

PHP documentation on the subject is here: http://www.php.net/manual/en/language.variables.variable.php

this works :)

    <?php
    $Texas = "large";
    $RhodeIsland = "small";

    $statement = "Texas";
    echo $statement." is ".$$statement."<br>";

    $statement = "RhodeIsland";
    echo $statement." is ".$$statement."<br>";

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top