Solved

How do I "exicute" a variable?

Posted on 2007-11-14
9
213 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
Industry Leaders: 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!

 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

752 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