Can variables contain flow control elements?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
enfys
Forum Newbie
Posts: 4
Joined: Thu Nov 18, 2004 4:32 am

Can variables contain flow control elements?

Post by enfys »

I need to execute some flow control but from within code that is embedded in quote marks e.g.

Code: Select all

<?php
$test = if($counter == 12) &#123;

echo "Counter equals twelve";
&#125;
$counter=12;
$test;

?>
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Code: Select all

$code = '$counter = 12;';
eval($code);
echo $counter;
etc. Just eval() the code.
jl
Forum Commoner
Posts: 53
Joined: Tue Nov 09, 2004 12:05 am

Post by jl »

would this work?

Code: Select all

$func="if (\$counter==12) { echo 'foo' ; }" ;
$counter=12 ;
eval($func) ;
Last edited by jl on Sun Nov 21, 2004 1:28 am, edited 2 times in total.
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

No. [php_man]exec()[/php_man] is used to execute an external program. Check out the php manual entry on exec() for more information.
User avatar
potsed
Forum Commoner
Posts: 50
Joined: Sat Oct 09, 2004 12:00 pm
Location: - South Africa

Post by potsed »

im not sure this is what your looking for

Code: Select all

function echoFoo($foo) {
    echo $foo;
}
then call the function like

Code: Select all

$counter = 12;
$show = ($counter === 12) ? echoFoo($counter) : false;
jl
Forum Commoner
Posts: 53
Joined: Tue Nov 09, 2004 12:05 am

Post by jl »

nigma wrote:No. [php_man]exec()[/php_man] is used to execute an external program. Check out the php manual entry on exec() for more information.
Typo, clearly I meant eval()
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

jl wrote:
nigma wrote:No. [php_man]exec()[/php_man] is used to execute an external program. Check out the php manual entry on exec() for more information.
Typo, clearly I meant eval()
... Well how was I supposed to know :?:
Post Reply