Solved

How do I "exicute" a variable?

Posted on 2007-11-14
9
210 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
 
LVL 27

Expert Comment

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

http://us2.php.net/manual/en/function.exec.php
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

930 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now