LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-11-2006, 11:45 AM   #1
joelhop
Member
 
Registered: Mar 2004
Location: Pennsylvania::USA
Distribution: Fedora Core 6
Posts: 100

Rep: Reputation: 15
Question Php Coding Question


Hello All,

I am writing some php code where I have a string that I want to be evaluated by php. This is a very simplified example of what I am trying to do on a larger scale, I am just wondering if this is possible:


function getSomeNum()
{
return "23";
}

$funkvar = "getSomeNum()";

echo $funkvar;

Instead of it literally echoing the string: getSomeNum()

I would like it to echo the result of the actual function getSomeNum() or 23.

Is there a way to tell php to evaluate a string,like echo eval($funkvar) and have it give me the result of getSomeNum() which is 23 instead of the literal string "getSomeNum()"?

Some way to make: echo $funkvar and echo getSomeNum() both result in 23;

Instead of echo getSomeNum() = 23
and echo $funkvar = getSomeNum()

Thanks!
-Karl

Last edited by joelhop; 08-11-2006 at 11:49 AM.
 
Old 08-11-2006, 11:54 AM   #2
pnellesen
Member
 
Registered: Oct 2004
Location: Missouri, USA
Distribution: Slackware 12.2, Xubuntu 9.10
Posts: 371

Rep: Reputation: 31
Did you try
Code:
$funkvar = getSomeNum();
without the double quotes?
 
Old 08-11-2006, 01:33 PM   #3
joelhop
Member
 
Registered: Mar 2004
Location: Pennsylvania::USA
Distribution: Fedora Core 6
Posts: 100

Original Poster
Rep: Reputation: 15
That solution will work if I wish to store the value of the result of getSomeNum() into $funkvar. I am looking to store the actual function name as a string getSomeNum() and then be able to have that string be evaluated later on.

I ended up solving the problem with this solution:

<?php

function getSomeNum()
{
return "23";
}


$funkvar = "return getSomeNum();";

echo eval($funkvar);

?>

Putting the return statement before the function name string, allows eval to evaluate that function and return the result.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Php Coding Question: Static Functions joelhop Programming 2 08-09-2006 12:17 PM
Php Coding Question joelhop Programming 5 08-07-2006 03:09 PM
PHP Coding Question joelhop Programming 12 07-24-2006 06:52 PM
PHP Coding Question joelhop Programming 2 07-20-2006 07:16 PM
PHP 4.4.0 Coding Error Phaux Linux - Software 1 12-30-2005 08:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration