Solved

How do I "exicute" a variable?

Posted on 2007-11-14
9
211 Views
Last Modified: 2009-12-16
Hello.

How do I "exicute" a variable?

$code = "echo('Hello World');"

0
Comment
Question by:hankknight
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 49

Expert Comment

by:Roonaan
ID: 20280877
hankknight,

ob_start();
// execute code

$code = ob_get_clean();


Kind regards,

-r-
0
 
LVL 17

Expert Comment

by:nplib
ID: 20280945
define execute,

cause you can't quite execute a variable,

variables store information,

you can execute a function

function myFirstFunction() {
     $var = "Hello World";
     echo $var;
}

myFirstFunction();
///this will output hello world to the screen

or

function myFirstFunction() {
     $var = "Hello World";
     return $var;
}

$var = myFirstFunction();

////This will return the variable in the function to your global variable.

Although this a simple demonstration, if there were a series of complex instructions in the function the output is all that would be returned
0
 
LVL 16

Author Comment

by:hankknight
ID: 20280980
If $code = "echo('Hello World');" then I want this to be done:
echo('Hello World')

If $code = "include('file.php');" then I want this to be done:
include('file.php');
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 27

Expert Comment

by:yodercm
ID: 20280989
$code = "echo('Hello World');"
exec($code);

http://us2.php.net/manual/en/function.exec.php
0
 
LVL 49

Accepted Solution

by:
Roonaan earned 500 total points
ID: 20281086
you would use eval(). Exec is used to do system calls.

-r-
0
 
LVL 17

Expert Comment

by:nplib
ID: 20281100
exec() is for executing external commands that exist on the server.

Why do you want it to work that way, what are you trying to achieve?

I don't think it can work that way.
$variables are either
Integer,
String,
Boolen,
Arrays,
or Objects.

functions are executables
create functions like

function myEcho ($var) {
     echo $var;
}

then all you do is type
myEcho("hello World");
 and it will print it to the screen,

or create and include function,

function myInclude($var) {
    include($var);
}

then all you do is type,

myInclude("page.php");

and it will include it,

but these are all predefined executables in PHP,

So What are you trying to achieve and don't just say, trying to execute a variable, because you can't.
0
 
LVL 27

Expert Comment

by:yodercm
ID: 20282641
They're exactly right, my bad, sorry ... eval() is the one you want, not exec().

http://us2.php.net/manual/en/function.eval.php

$code = "echo('Hello World');"
eval($code);
0
 
LVL 21

Expert Comment

by:nizsmo
ID: 20282822
so what about the if statement? something like this?

<?php
if($code == "echo('Hello World');")
    eval($code);

if($code == "include('file.php');")
    eval($code);
?>

Hope this helps.
0
 
LVL 27

Expert Comment

by:yodercm
ID: 20283308
nizsmo, I don't quite understand why you want if statements.  eval() will execute whatever statement is in $code, so it shouldn't be necessary to check what that is.  Am I missing something?
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question