I am attempting to run a function named from a variable. There is no syntax error. Temperature is not returned. I don't know for certain if the problem is with the function itself or the eval. Variations on the eval theme have not worked so far.
function getBtemp($lum, $sub){
$tempsB = array(
"V" => array( 30000, 25400, ... ),
"III" => array( 29000, 24000, ... ),
"I" => array( 26000, 20800, ... ) );
if($lum == "VI"){ $lum = "V"; }
else if($lum == "IV"){ $lum = "III"; }
else if($lum == "II" || $lum == "Ib" || $lum == "Ia" ){ $lum = "V"; }
return $tempsB['$lum']['$sub']; }
// Variables:
$spectralclass = "B";
$luminosityclass = "V";
$subclass = 5;
// Needed:
$temp = getBtemp($luminosityclass, $subclass);
// Functions are named from spectral class, e.g. get.$spectralclass.temp()
// Attempt:
$str = "$temp = get".$spectralclass."temp($luminosityclass, $subclass);";
eval($str);
Try doing this:
$func = 'get'.$spectralclass.'temp';
$temp = $func($luminosityclass, $subclass);
You might be better off doing something like
$functionName = 'get' . $spectralclass . 'temp';
$temp = $functionName($luminosityclass, $subclass);
This is what the PHP manual calls a "variable function". In most cases PHP lets you treat a string variable as a function name, and doing so is a bit safer (more restrictive, less error-prone) than eval.
You need to pass parameters after set function name. See a sample:
function getAtemp($a = 'default') {
echo $a;
}
$name = 'Trest';
$function = 'A';
$temp = 'get' . $function . 'temp';
echo($temp('teste'));
Additionally, read from Eric Lippert: Eval is Evil
Replace the following line:
$str = "$temp = get".$spectralclass."temp($luminosityclass, $subclass);";
by
$str = '$temp = get'.$spectralclass.'temp($luminosityclass, $subclass);';
or
$str = "\$temp = get".$spectralclass."temp(\$luminosityclass, \$subclass);";
See http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.double
Related
I'm not sure it's even the right way to define this question.
I have string that may be exist, and may not. It happens to be a number: $number
If $number doesn't exist, then I want to use the PHP variable $url.
But if $number does exist, then I want to use the PHP variable which is named $url+the number, i.e, $url2 if $number=2
So I tried this code, but it doesn't work:
$number = "2"; //(Can be either missing, or equal to 1, 2, or 3)
$url = "www.0.com"; // Fallback
$url1 = "www.1.com";
$url2 = "www.2.com";
$url3 = "www.3.com";
$result = $url.=$number ;
// If $number=1, I want $result to be : www.1.com
// If $number=2, I want $result to be : www.2.com
// If $number=3, I want $result to be : www.3.com
// If $number IS NOT SET, I want $result to be : www.0.com
// Now do something with $result
Perhaps there's a completely better way to achieve what I want (will be happy to see example), but anyway I'm curious as well to understand how to achieve it my way.
Okay, so you're talking about a variable variable.
You should define the name of the variable you need to use in a string, and then pass that to a variable variable using $$ syntax:
if( isset($number) && is_numeric($number) )
{
$name = 'url'.$number;
$result = $$name;
}
else
{
$result = $url;
}
That having been said, you may be better off using an array for this:
$urls = [ 'www.0.com', 'www.1.com', 'www.2.com', 'www.3.com' ];
$result = (!isset($number)) ? $urls[0] : $urls[ intval($number) ];
You can use ternary with in_array and empty.
$number = "2"; //(Can be either missing, or equal to 1, 2, or 3)
$url = "www.0.com"; // Fallback
$url1 = "www.1.com";
$url2 = "www.2.com";
$url3 = "www.3.com";
$result = (!empty($number) && in_array($number, array(1,2,3))) ? ${'url' . $number} : $url;
echo $result;
Demo: https://eval.in/821737
In php you can have things like dynamic variable names:
$variableName = "url".$number;
$result = $$variableName;
However, you should make sure, that $variableName refers to an existing variable:
$result = "www.fallbackURL.com";
if(isset($$variableName)) $result = $$variableName;
Or Try this code:
$number = 5;
$url[0] = "www.0.com"; // Fallback
$url[1] = "www.1.com";
$url[2] = "www.2.com";
$url[3] = "www.3.com";
if (!isset($number)) $number=0;
if (!isset($url[$number])) $number=0;
$result = $url[$number];
If you add $ front of string, it define variable, so you can use following code:
<?php
$number = "2"; //(Can be either missing, or equal to 1/2/3)
$url = "www.0.com"; // Fallback
$url1 = "www.1.com";
$url2 = "www.2.com";
$url3 = "www.3.com";
if(isset($number) && is_numeric($number) && $number <= 3) {
$variable_name = 'url' . $number; //string like url2
} else {
$variable_name = 'url';
}
$result = $$variable_name ; //define $url2 from url2 string
echo $result;
// Now do something with $result
Example for define variable with string variable:
$string = 'hello';
$$string = 'new variable'; //define $hello variable
echo $hello; //Output: "new variable"
if the url need just a number, you can do this easy way
($number)?$number:0;
$url = "www.".$number.".com";
if there are specific real url, you can use array
$array[0] = "www.google.com";
$array[1] = "www.facebook.com";
($number)?$number:0;
url = $array[$number];
Updated code:
$number = "2";
if(isset($number)){
$res = "url".$number;
$result=$$res;
}else{
$result=$url;
}
echo $result;
My goal is:
If a value in those arrays match with an extention of a file, then I can builds a nice URL encoded string which I can append to a url using the http_build_query() function. If no match is found, it return nothing.
I have tried it every possible way and it doesn't work, as the code below. can you check on which part is wrong ?.
$name = 'myfile.mp4';
$list = array(
'video' => array('3gp', 'mkv', 'mp4'),
'photo' => array('jpg', 'png', 'tiff')
);
// File Ext Check
$ext = (strpos($name, '.') !== false) ? strtolower(substr(strrchr($name, '.'), 1)) : '';
$type = null;
$find = $ext;
array_walk($list, function ($k, $v) use ($find, &$type)
{
if (in_array($find, $k))
{
$type = $v;
$data = array();
switch ($type)
{
case 'video':
$data['title'] = 'video';
$data['description'] = 'my video';
break;
case 'photo':
$data['title'] = 'photo';
$data['description'] = 'my photo';
break;
}
}
else {
echo "not both!.";
exit;
{
});
if ($type == 'video') {
$link = 'https://www.video.com' . http_build_query($data);
}
else
if ($type == 'photo') {
$link = 'https://www.photo.com' . http_build_query($data);
}
echo $link;
Thank you...
You $data is set in the array_walk() scope, but out of the array_walk() scope is not defined. So in the http_build_query($data), here $data has not defined.
You may referende the $data in the use(), then after the array_walk(), you can use its value. In your code you use in_array() to check the file type in a list, for performance I recomend you to see this post.
I'm trying to build a small CMS using CodeIgniter, and I need to be able to dynamically update some variables within the application/config.php
So far I did:
private function update_file ($file, $var, $var_name) {
$start_tag = "<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');\n";
if (file_exists($file)) {
require_once ($file);
$updated_array = array_merge($$var_name, $var);
$data = $start_tag."\$".$var_name." = ".var_export($updated_array, true).";";
file_put_contents($file, $data);
} else {
return false;
}
}
Everything works just fine! The result in the config.php file will be:
<?php ...;
$config = array (
'base_url' => '',
...
...
);
But what if I would like to maintain the original config.php file format with comments, spaces and
separated declared $config['key'] = 'value' ... ?
Is that possible ?
EDIT:
Thank you for your answers, very precious.
I found a slightly different solution for my needs, performing a preg_replace on the return of file_get_contents() and then write back on the file the new resulting string. File maintains the exact original clean format.
private function update_file ($file, $var, $var_name) {
if (file_exists($file)) {
require_once ($file);
$contents = file_get_contents($file);
$updated_array = array_merge($$var_name, $var);
$search = array();
$replace = array();
foreach($$var_name as $key => $val) {
$pattern = '/\$'.$var_name.'\[\\\''.$key.'\\\'\]\s+=\s+[^\;]+/';
$replace_string = "\$".$var_name."['".$key."'] = ".var_export($updated_array[$key], true);
array_push($search, $pattern);
array_push($replace, $replace_string);
}
$new_contents = preg_replace($search, $replace, $contents);
write_file($file, $new_contents);
}
Maybe it requires some slight performance improvements. But this is my baseline idea.
create the keys with empty values
$config['base_url'] = '';
then set them inside any of your controllers.
This works best if you store the values in the db, and initialize them in MY_Controller.
$this->config->set_item('base_url', 'value');
It is possible. I can't find the code , but once i have written something like that. Whole idea was based on tokenizing template file and substitute values in an array, preserving key order, line numbers and comments from the template.
[+] Found it. It's purpose was to fill values from template that looked like this (it was much bigger of course):
<?php
$_CFG = array(
// DB section
'db_host' => 'localhost',
'db_user' => 'root',
'db_pass' => '',
'db_name' => 'test',
// Site specific
'lang' => array('pl','en'),
'admin' => 'admin#example.com',
);
And the code that was doing all the magic:
$tokens = token_get_all(file_get_contents('tpl/config.php'));
$level = -1;
$buffer = '';
$last_key = 0;
$iteration = 0;
foreach($tokens as $t){
if($t === ')'){
$iteration = 0;
$last_key = 0;
$level--;
}
if(is_array($t)){
if($t[0] == T_ARRAY && strtolower($t[1]) === 'array')
$level++;
if($t[0] == T_CONSTANT_ENCAPSED_STRING){
if($last_key){
if($level){
if(isset($new_config[$last_key][$iteration])){
$buffer .= var_export($new_config[$last_key][$iteration], TRUE);
}
else
$buffer .= 'null';
$iteration++;
}
else{
if(isset($new_config[$last_key]))
$buffer .= var_export($new_config[$last_key], TRUE);
else
$buffer .= 'null';
$last_key = 0;
}
}
else{
$buffer .= $t[1];
$last_key = trim($t[1],"'");
}
}
else
$buffer .= $t[1];
}
else
$buffer .= $t;
}
file_put_contents('config.php',$buffer);
My goal is to echo the argument passed to a function. For example, how can this be done?
$contact_name = 'foo';
function do_something($some_argument){
// echo 'contact_name' .... How???
}
do_something($contact_name);
You can't. If you want to do that, you need to pass the names as well, e.g:
$contact_name = 'foo';
$contact_phone = '555-1234';
function do_something($args = array()) {
foreach ($args as $name => $value) {
echo "$name: $value<br />";
}
}
do_something(compact('contact_name', 'contact_phone'));
Straight off the PHP.net variables page:
<?php
function vname(&$var, $scope=false, $prefix='unique', $suffix='value')
{
if($scope) $vals = $scope;
else $vals = $GLOBALS;
$old = $var;
$var = $new = $prefix.rand().$suffix;
$vname = FALSE;
foreach($vals as $key => $val) {
if($val === $new) $vname = $key;
}
$var = $old;
return $vname;
}
?>
Not possible.
Variables are just means to address values or areas in the memory. You cannot get the variable name that’s value has been passed to a function.
Disclaimer: this will oonly work if you pass a variable to the function, not a value, and it only works when your not in a function or a class. So only the GLOBAL scope works :)
Good funct($var)
Bad funct(1)
You can do it actually contrary to popular believe ^_^. but it involves a few lookup tricks with the $GLOBALS variable.
you do it like so:
$variable_name = "some value, better if its unique";
function funct($var) {
foreach ($GLOBALS as $name => $value) {
if ($value == $var) {
echo $name; // will echo variable_name
break;
}
}
}
this method is not fool proof tho. Because if two variables have the same value, the function will get the name of the first one it finds. Not the one you want :P
Its best to make the variable value unique before hand if you want accuracy on variable names
Another method would be to use reference to be accurate like so
$variable_name = 123;
function funct(&$var) {
$old = $var;
$var = $checksum = md5(time()); // give it unique value
foreach ($GLOBALS as $name => $value) {
if ($value == $var) {
echo $name; // will echo variable_name
$var = $old; // reassign old value
break;
}
}
}
so it is entirely possible :)
Based on PTBNL's (most definately correct) answer i came up with a more readable (at least i think so) approach:
/**
* returns the name of the variable posted as the first parameter.
* If not called from global scope, pass in get_defined_vars() as the second parameter
*
* behind the scenes:
*
* this function only works because we are passing the first argument by reference.
* 1. we store the old value in a known variable
* 2. we overwrite the argument with a known randomized hash value
* 3. we loop through the scope's symbol table until we find the known value
* 4. we restore the arguments original value and
* 5. we return the name of the symbol we found in the table
*/
function variable_name( & $var, array $scope = null )
{
if ( $scope == null )
{
$scope = $GLOBALS;
}
$__variable_name_original_value = $var;
$__variable_name_temporary_value = md5( number_format( microtime( true ), 10, '', '' ).rand() );
$var = $__variable_name_temporary_value;
foreach( $scope as $variable => $value )
{
if ( $value == $__variable_name_temporary_value && $variable != '__variable_name_original_value' )
{
$var = $__variable_name_original_value;
return $variable;
}
}
return null;
}
// prove that it works:
$test = 1;
$hello = 1;
$world = 2;
$foo = 100;
$bar = 10;
$awesome = 1;
function test_from_local_scope()
{
$local_test = 1;
$local_hello = 1;
$local_world = 2;
$local_foo = 100;
$local_bar = 10;
$local_awesome = 1;
return variable_name( $local_awesome, get_defined_vars() );
}
printf( "%s\n", variable_name( $awesome, get_defined_vars() ) ); // will echo 'awesome'
printf( "%s\n", test_from_local_scope() ); // will also echo awesome;
Sander has the right answer, but here is the exact thing I was looking for:
$contact_name = 'foo';
function do_something($args = array(), $another_arg) {
foreach ($args as $name => $value) {
echo $name;
echo '<br>'.$another_arg;
}
}
do_something(compact(contact_name),'bar');
class Someone{
protected $name='';
public function __construct($name){
$this->name=$name;
}
public function doSomthing($arg){
echo "My name is: {$this->name} and I do {$arg}";
}
}
//in main
$Me=new Someone('Itay Moav');
$Me->doSomething('test');
I've got a multidimensional associative array which includes an elements like
$data["status"]
$data["response"]["url"]
$data["entry"]["0"]["text"]
I've got a strings like:
$string = 'data["status"]';
$string = 'data["response"]["url"]';
$string = 'data["entry"]["0"]["text"]';
How can I convert the strings into a variable to access the proper array element? This method will need to work across any array at any of the dimensions.
PHP's variable variables will help you out here. You can use them by prefixing the variable with another dollar sign:
$foo = "Hello, world!";
$bar = "foo";
echo $$bar; // outputs "Hello, world!"
Quick and dirty:
echo eval('return $'. $string . ';');
Of course the input string would need to be be sanitized first.
If you don't like quick and dirty... then this will work too and it doesn't require eval which makes even me cringe.
It does, however, make assumptions about the string format:
<?php
$data['response'] = array(
'url' => 'http://www.testing.com'
);
function extract_data($string) {
global $data;
$found_matches = preg_match_all('/\[\"([a-z]+)\"\]/', $string, $matches);
if (!$found_matches) {
return null;
}
$current_data = $data;
foreach ($matches[1] as $name) {
if (key_exists($name, $current_data)) {
$current_data = $current_data[$name];
} else {
return null;
}
}
return $current_data;
}
echo extract_data('data["response"]["url"]');
?>
This can be done in a much simpler way. All you have to do is think about what function PHP provides that creates variables.
$string = 'myvariable';
extract(array($string => $string));
echo $myvariable;
done!
You can also use curly braces (complex variable notation) to do some tricks:
$h = 'Happy';
$n = 'New';
$y = 'Year';
$wish = ${$h.$n.$y};
echo $wish;
Found this on the Variable variables page:
function VariableArray($data, $string) {
preg_match_all('/\[([^\]]*)\]/', $string, $arr_matches, PREG_PATTERN_ORDER);
$return = $arr;
foreach($arr_matches[1] as $dimension) { $return = $return[$dimension]; }
return $return;
}
I was struggling with that as well,
I had this :
$user = array('a'=>'alber', 'b'=>'brad'...);
$array_name = 'user';
and I was wondering how to get into albert.
at first I tried
$value_for_a = $$array_name['a']; // this dosen't work
then
eval('return $'.$array_name['a'].';'); // this dosen't work, maybe the hoster block eval which is very common
then finally I tried the stupid thing:
$array_temp=$$array_name;
$value_for_a = $array_temp['a'];
and this just worked Perfect!
wisdom, do it simple do it stupid.
I hope this answers your question
You would access them like:
print $$string;
You can pass by reference with the operator &. So in your example you'll have something like this
$string = &$data["status"];
$string = &$data["response"]["url"];
$string = &$data["entry"]["0"]["text"];
Otherwise you need to do something like this:
$titular = array();
for ($r = 1; $r < $rooms + 1; $r ++)
{
$title = "titular_title_$r";
$firstName = "titular_firstName_$r";
$lastName = "titular_lastName_$r";
$phone = "titular_phone_$r";
$email = "titular_email_$r";
$bedType = "bedType_$r";
$smoker = "smoker_$r";
$titular[] = array(
"title" => $$title,
"first_name" => $$firstName,
"last_name" => $$lastName,
"phone" => $$phone,
"email" => $$email,
"bedType" => $$bedType,
"smoker" => $$smoker
);
}
There are native PHP function for this:
use http://php.net/manual/ru/function.parse-str.php (parse_str()).
don't forget to clean up the string from '"' before parsing.
Perhaps this option is also suitable:
$data["entry"]["0"]["text"];
$string = 'data["entry"]["0"]["text"]';
function getIn($arr, $params)
{
if(!is_array($arr)) {
return null;
}
if (array_key_exists($params[0], $arr) && count($params) > 1) {
$bf = $params[0];
array_shift($params);
return getIn($arr[$bf], $params);
} elseif (array_key_exists($params[0], $arr) && count($params) == 1) {
return $arr[$params[0]];
} else {
return null;
}
}
preg_match_all('/(?:(\w{1,}|\d))/', $string, $arr_matches, PREG_PATTERN_ORDER);
array_shift($arr_matches[0]);
print_r(getIn($data, $arr_matches[0]));
P.s. it's work for me.