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

138
Views
Asignar una condición de declaración if a una variable

¿Es posible asignar una declaración condicional a una variable y usarla en una declaración if ? Por ejemplo, tengo el siguiente código donde la condición en la instrucción if cambiará de vez en cuando:

 <?php $x = 2; //This "x > 5" value will come from a database $command = "x > 5";//Some times this value might be x > 5 || x == 1 if($command)//This will always be true no matter what since the content of the $command is not empty echo "x is bigger than 5"; else echo "x is smaller than 5";//I want this line to get executed since x is smaller than 5 in this case ?>

El resultado esperado es x is smaller than 5 , pero lo que obtengo es que x is bigger than 5

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Necesitas usar eval() . El $command tendrá que contener $ antes de las variables para que sea una sintaxis PHP válida.

 $x = 2; $command = '$x > 5'; $result = eval("return ($command);"); if ($result) { echo "x is bigger than 5"; } else { echo "x is smaller than 5"; }

Dado que eval() puede ejecutar código arbitrario, debe tener mucho cuidado con lo que permite poner en $command .

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!