Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

154
Visualizações
from php 5.4 to php 7 , useing dynamic variables is confused

my php is update from php 5.4 to php 7.0.33 , but php code get an error with Dynamic variables

php 5.4 some code like this...

$attributes=array('boy','girl','none' );
$this->boy = 'eric';
$this->girl = 'lili';
$this->none = 'who';
            
echo $attributes[0];                    //=>php 5 is "boy"  | php 7 is "boy"
print_r( $this->$attributes[0] );       //=>php 5 is "eric" | php 7 is empty
print_r( $this->${attributes}[0] );     //=>php 5 is "eric" | php 7 is empty

when I use in php 7 shome code get epmty, how can I do to resolve this problem

I already watching the refer page Using braces with dynamic variable names in PHP , but still confused

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

For dynamic variables like this, you need to wrap the string in curly braces. You almost had it there in your last line. However, remember that it's not the array $attributes that you're using to indicate the dynamic variable, it's the string at $attributes[0].

So this should work:

$this->{$attributes[0]}

Dynamic variables names are powerful, but easy to get confused with. If you have the option, I'd recommend using just an array:

$this->attributes = [
    "boy"  => "eric",
    "girl" => "lili",
    "none" => "who"
];

echo $this->attributes["boy"];
over 4 years ago · Santiago Trujillo Relatório

0

The syntax you are searching for is

 print_r( $this->{$attributes[0]} ); 
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda