Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

145
Views
de php 5.4 a php 7, se confunde el uso de variables dinámicas

mi php se actualizó de php 5.4 a php 7.0.33, pero el código php recibe un error con variables dinámicas

php 5.4 algún código como este...

 $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

cuando uso en php 7 el código shome get epmty, ¿cómo puedo hacer para resolver este problema?

Ya estoy viendo la página de referencia Uso de llaves con nombres de variables dinámicas en PHP , pero aún estoy confundido

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Para variables dinámicas como esta, debe envolver la cadena entre llaves. Casi lo tenías allí en tu última línea. Sin embargo, recuerde que no es la matriz $attributes lo que está usando para indicar la variable dinámica, es la cadena en $attributes[0] .

Así que esto debería funcionar:

 $this->{$attributes[0]}

Los nombres de las variables dinámicas son poderosos, pero es fácil confundirlos. Si tiene la opción, le recomiendo usar solo una matriz:

 $this->attributes = [ "boy" => "eric", "girl" => "lili", "none" => "who" ]; echo $this->attributes["boy"];
over 4 years ago · Santiago Trujillo Report

0

La sintaxis que está buscando es

 print_r( $this->{$attributes[0]} );
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!