Solved

How do I "exicute" a variable?

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

How do I "exicute" a variable?

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

0
Comment
Question by:hankknight
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

 
LVL 27

Expert Comment

by:Cornelia Yoder
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:Cornelia Yoder
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:Cornelia Yoder
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

636 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