1

I'm just wondering on how I'd be able to use PHP coding in PHP variables? So for example. I want something like

$Start = " IF($lala == "0"){ echo '";
$End = " '; }";

and to be able to use it as

echo ''.$start.' hello '.End.'';

It may sound weird, but I'd like it to be done in a similar way to this.

Thank you

Gesset
  • 53
  • 1
  • 7
  • 4
    Why do you want to do this? – Parthapratim Neog Mar 17 '16 at 13:00
  • 5
    I guess this will help you: http://php.net/manual/en/function.eval.php – akasummer Mar 17 '16 at 13:00
  • 2
    `$start != $Start`, and `End != $End` – devpro Mar 17 '16 at 13:01
  • wow, totally weird! just out of curiosity what is the use case? I'm sure we can find a much better alternative. – azerafati Mar 17 '16 at 13:01
  • I want to do this, because, well its strange, but i'm going to be using the exact same if statement about 20 times, and I'm using str_replace to use {Start} tags inside the html. Which I don't wan to be using php inside. So pretty much I want to be able to use {Start} and for it to turn out the result for $Start which will give me the if statement. – Gesset Mar 17 '16 at 13:02
  • 1
    This is such a humugously bad idea! – RiggsFolly Mar 17 '16 at 13:08
  • @RiggsFolly Any suggestions on a better way? – Gesset Mar 17 '16 at 13:09
  • 4
    Why not use a function? What you described is the exact use case for a function. – César Mar 17 '16 at 13:10
  • Perhaps a `switch` - http://php.net/manual/en/control-structures.switch.php – Nick R Mar 17 '16 at 13:14
  • Possible duplicate of [instantiate a class from a variable in PHP?](http://stackoverflow.com/questions/534159/instantiate-a-class-from-a-variable-in-php) – Giacomo1968 Mar 17 '16 at 13:22
  • Possible duplicate of [Execute PHP code in a string](http://stackoverflow.com/questions/10866301/execute-php-code-in-a-string) – SOFe Mar 17 '16 at 13:23
  • 1
    Let's put it this way: lots of PHP apps have been written without resorting to this weirdness. That should be an indication that it's either not a good idea and/or that it's at least not very feasible. – deceze Mar 17 '16 at 13:27

3 Answers3

1

I can't even begin to tell you how dangerous executing code from a string is. So don't even think about it...

Instead use a function or a Closure (once you feel somewhat safe with PHP)

<?php
function myFunction($str) {
  $lala = lalaFunction();
  if ($lala) { return $str; }
}

And then just call:

echo myFunction('something');
César
  • 370
  • 1
  • 9
0

Yes, you can by using eval():

<?php
    $lala = 0;
    $Start = "if($lala == '0'){ echo ";
    $End = ";}";
    eval($Start.'hello'.$End);
?> 

output:

hello
mitkosoft
  • 5,262
  • 1
  • 13
  • 31
0

in that case for if condition you can simply write the code like this:

<?php
$check=0;
$x='true';
$y='false';
$value=$check==0?$y:$x;
?>

in this $check==0 is used for if AND ? is used for the event to run if condition passed is true and : is used for else part.

deceze
  • 510,633
  • 85
  • 743
  • 889
Varun Malhotra
  • 1,202
  • 3
  • 15
  • 28