0

I basically want a way to write an if statement with only one variable in it, like:

$var = "$foo == 'bar'"; if ($var) { // do code } 

Or to put it more descriptively, Instead of:

$run_list = array("normal"); if (!empty("extras")) array_push($run_list, "extras"); foreach ($run_list as $iteration) { if ($iteration == "normal") { if ($array1 == $array2) { # do 125 lines of code here } // Will not always be True (may only iterate once) } elseif ($iteration == "extras") { if ($array1 != $array2) { # do exact same 125 lines of code here } } } 

How could I do something similar to this, but that actually works:

$run_list = array("normal"); if (!empty("extras")) array_push($run_list, "extras"); foreach ($run_list as $iteration) { if ($iteration = "normal") { $if_statement = "$array1 == $array2"; } elseif ($iteration = "extras") { $if_statement = "$array1 != $array2"; } if ($if_statement) { # do 125 lines of code here } } 

And I don't want to put the 125 lines of code into a function because they contain dozens of variables set earlier in the code and I don't really want to create a function where I have to pass in dozens of variables for it to work.

1
  • 1
    if($iteration == "normal") why do you want to change that into something complicated? Commented Nov 22, 2014 at 15:25

2 Answers 2

2

You might want to merge your boolean expressions here

$run_list = array("normal"); if (!empty("extras")) array_push($run_list, "extras"); foreach ($run_list as $iteration) { if ( ($iteration == "normal" && array1 == $array2) || ($iteration == "extras" && $array1 != $array2) ) { # do 125 lines of code here } } 
Sign up to request clarification or add additional context in comments.

Comments

0

You could first create a boolean to later test in the condition like so:

 $conditional = $iteration === "normal"; 

And then later test that conditional:

if($conditional){ //execute code } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.