I have an array like this:
$cmds = array(
'info' => call_user_func('_info', $cmd1),
'e1' => 'something'
);
The func itself:
function _info($cmd1){
if(strlen($cmd1) < 2){
die('Invalid Username');
}else{
echo 'cmd1 > 2';}
}
The problem is it being executed before i call it giving undefined variable error because $cmd1 not exist yet, it will be exist when i call it in a foreach function.
Been searching for a solution for hours but didn't find.
I tried setting the array as this 'info' => _info($cmd1) and 'info' => '_info' and putted $cmd1 directly in the func like this
function _info(){
if(strlen($cmd1) < 2){die('invalid');
}
But i still get the same error as it being called before i want to be.
You can send parameters to be executed later
$cmds = array(
'info' => [ '_info', '$cmd1' ], // single quotes
'e1' => 'something'
);
In the meantime your cmd1 is defined
$cmd1 = "hello";
foreach ($cmds as $key => $cmdset) {
if (!is_array($cmdset)) {
// do something with e1 => something
} else {
$function = array_shift($cmdset);
foreach ($cmdset as &$param) {
// Only variables are treated below, values are passed through as is
if (stripos($param, '$') === 0) {
// Remove $
$cmd1 = substr($param, 1);
// Set param to the value of cmd1
$param = $$param; // variable varaible $$
}
}
call_user_func_array($function, $cmdset);
}
}
While this works it is really bad practice to do like this. Depending on a variable function that is not clear if it is set or not. The code is hard to understand.
You should think about another solution where you can ensure that $cmd1 is set or can be retrieved and if it is not have some way to deal with that as well.
Related
i wondering how i can make it simpler...
the thing is
if($results['experience'] == 0) { loadViev('experience',$experience, $careerGoals,$mainSectionLines); }
if($results['Education'] == 0) { loadViev('Education',$graduate,$careerGoals,$mainSectionLines); }
if($results['Extra'] == 0) { loadViev('Extra',$extra,$careerGoals,$mainSectionLines); }
if($results['Licence'] == 0) { loadViev('Licence',$licence,$careerGoals,$mainSectionLines); }
if($results['Cert'] == 0) { loadViev('Certyfikaty',$cert,$careerGoals,$mainSectionLines); }
if($results['conferences'] == 0){ loadViev('Conferences',$conferences,$careerGoals,$mainSectionLines); }
if($results['Courses'] == 0) { loadViev('Courses',$Courses,$careerGoals,$mainSectionLines); }
if($results['Hobbys'] == 0) { loadViev('Hobby',$hobby,$careerGoals,$mainSectionLines); }
As you can see, if some "name" == 0 function will run, there is for now around 14 combination, i know there is possible to do it faster than copy and paste whole code 14 times...
result code:
$results =[];
foreach ($userChoice as $key => $value) {
$setVal = $value['key'];
$results[$setVal] = $value['order'];
}
Result only grab name of a section and order nr.
'$userChoice' is just a array with data
Do anybody have a idea how i can do it?
Also the thing is result collect all the section data (14 section) where as you can see i need only selected 8.
The only difference is the word being loaded and the name of the variable. The word remains the same, but the variable is lowercase.
As such, all you would need to do is loadViev the $setVal name (the word itself) as the first argument, and $$setval as the second. This executes the word as a variable, making use of variable variables.
Unfortunately you can't (easily) use something like strtolower() on a variable variable directly, so you'll have to convert these to lowercase independently first.
This can be seen in the following:
$results =[];
foreach ($userChoice as $key => $value) {
$setVal = $value['key'];
$results[$setVal] = $value['order'];
if ($value['order'] == 0) {
$lower = strtolower($setVal);
loadViev($setVal, $$lower, $careerGoals, $mainSectionLines);
}
}
How about creating a structure holding relevant values and iterating over it?
$mapping = [
['key' => 'experience', 'view' => 'experience','data' => $experience],
['key' => 'Education', 'view' => 'Education','data' => $graduate],
['key' => 'Extra', 'view' => 'Extra','data' => $extra],
...
...
];
foreach ($mapping as $m)
{
if ($results[$m['key']]==0)
{
loadViev($m['view'], $m['data'], $careerGoals,$mainSectionLines);
break;
}
}
if you can make your key/variable names consistent, you could simplify the code even further. E.g.
$validKeys = ['experience', 'education', ... ];
foreach($validKeys as $k)
{
if($results[$k] == 0)
{
loadviev($k, $$k, $careerGoals,$mainSectionLines)
}
}
I have the following code:
function filterUsers(array $setOfAllUsers) {
if (empty($setOfAllUsers)) {
return array(array(), array());
}
$activeUsers = array();
$inactiveUsers = array();
foreach($setOfAllUsers as $userRow) {
$var = ($userRow['IsActive'] ? '' : 'in') . 'activeUsers';
$$var[$userRow['CID']]['Label'] = $userRow['UserLabel'];
// Error happens here ---^
$$var[$userRow['CID']]['UserList'][$userRow['UID']] = array(
'FirstName' => $userRow['FName'],
'LastName' => $userRow['LName'],
... More data
);
}
return array($activeUsers, $inactiveUsers);
}
I get the following error: Warning: Illegal string offset 'Label' in ...
How can I fix this? I tried defining Label part first like this: $$var[$userRow['CID']] = array(); $$var[$userRow['CID']]['Label'] = ''; but did not work.
To make things clear what I am trying to achieve is this:
if ($userRow['IsActive']) {
$activeUsers[$userRow['CID']]['Label'] = $userRow['UserLabel'];
$activeUsers[$userRow['CID']]['UserList'][$userRow['UID']] = array(
'FirstName' => $userRow['FName'],
'LastName' => $userRow['LName'],
... More data
);
} else {
$inactiveUsers[$userRow['CID']]['Label'] = $userRow['UserLabel'];
$inactiveUsers[$userRow['CID']]['UserList'][$userRow['UID']] = array(
'FirstName' => $userRow['FName'],
'LastName' => $userRow['LName'],
... More data
);
}
Instead of repeating above in if/else I wanted to achieve it using $$
Try using ${$var} instead of $$var.
EDIT
From PHP Manual (http://php.net/manual/en/language.variables.variable.php):
In order to use variable variables with arrays, you have to resolve an
ambiguity problem. That is, if you write $$a[1] then the parser needs
to know if you meant to use $a[1] as a variable, or if you wanted $$a
as the variable and then the [1] index from that variable. The syntax
for resolving this ambiguity is: ${$a[1]} for the first case and
${$a}[1] for the second.
function filterUsers(array $setOfAllUsers) {
if (empty($setOfAllUsers)) {
return array(array(), array());
}
$users = array(
'inactiveUsers' => array(),
'activeUsers' => array()
);
foreach($setOfAllUsers as $userRow) {
$status = ($userRow['IsActive'] ? '' : 'in') . 'activeUsers';
$users[$status][$userRow['CID']] = array();
$users[$status][$userRow['CID']]['Label'] = $userRow['UserLabel'];
$users[$status][$userRow['CID']]['UserList'] = array(
$userRow['UID'] => array(
'FirstName' => $userRow['FName'],
'LastName' => $userRow['LName'],
)
);
}
return $users;
}
Try the following:
$myUsers = $$var;
$myUsers[...][...] = ...;
The problem with using $$var[...][...] is that first $var[...][...] is evaluated and then it tries to find the variable named $$var[...][...].
Don't use var-vars like this. You run into a PHP syntax glitch. And even if there wasn't a syntax glitch, you shouldn't be using var-vars in the first place. They make for near-impossible-to-debug code.
$x = 'foo';
$foo = array();
$$x[0] = 1;
var_dump($x); // string(3) "foo"
var_dump($foo); // array(0) { }
$$x[1][2] = 3;
PHP Notice: Uninitialized string offset: 2 in php shell code on line 1
Note how the array didn't get modified by the $$x[0] assignment, and how the 2+ dimensional assigment causes "undefined string offset". Your var-var isn't being treated as an array - it's being treated as a STRING, and failing, because it's a syntax glitch. You can treat strings as arrays, but not using var vars:
$bar = 'baz';
$bar[1] = 'q';
echo $bar; // output: bqz
There appears to be NO way to use a var-var as an array, especially a multi-dimensional array.
I would like to know how to run the php scripts which are stored in the database without using eval().
The expected process is as follow
When user POST a string which contains {{typed_alias }}, the system will search the table whether this alias has record in the alias column.
If yes -> replace what user typed with the correspond script which is stored in the replacement column.
If not -> show the original string including {{ wrong_alias }}
The expected result is as follow
When user posts
Hello, {{morninggg}}, the current unix time is {{nowTime}}
Array output from db
array
0 =>
array
'ID' => 445
'alias' => 'morning'
'replacement' => 'Good morning'
1 =>
array
'ID' => 446
'alias' => 'nowTime'
'replacement' => time()
2 =>
array
'ID' => 447
'alias' => 'tommorowNow'
'replacement' => time()+86400
Return
Hello, {{morninggg}}, the current unix time is 147855220
Now I have already solved the database array by using foreach and also can replace the alias with script by using str_replace().
Current classI use to foreach data from database and do the replacement is as follow
class replace {
public $definitions;
public function setDefinitions($definitions) {
$this->definitions = $definitions;
}
public function tag($input) {
if($this->definitions && is_array($this->definitions)) {
foreach ($this->definitions as $definition) {
if($defintion['alias'] == 'time') {
$input = str_replace('{{' . $definition['alias'] . '}}', date('Y-m-d'), $input);
} else {
$input = str_replace('{{' . $definition['alias'] . '}}', $definition['replacement'], $input);
}
}
}
return $input;
}
}
Current using method
$replace = new replace();
$replace->setDefinitions($tagEngine);
$parsedString = $replace->tag($__input);
//$__input is what user POST to the server
echo $parsedString;
However, the current result is as follow
Hello, {{morninggg}}, the current unix time is time()
The script can't be run successfully on the page
But when I give the definition manually like this
$definition = array('morning' => 'Good Morning', 'nowTime' => time());
foreach ($definition as $key => $value)
$source = str_replace('{{' . $key . '}}', $value, $source);
return $source;
The script can be run and returns
Hello, {{morninggg}}, the current unix time is 147855220
I know that using eval() can run the scripts, however, it is regarded as a dangerous method in a real-world application by people.
Can anyone give me suggestions about how to deal with this problem?
Thank you!
You should not use functions like eval() to fix this. You should pull all php-code out of your database and parse the different aliasses as follows (I have just altered the tag() method in the replace class:
public function tag($input) {
if($this->definitions && is_array($this->definitions)) {
foreach ($this->definitions as $definition) {
$replacement = $definition['replacement'];
switch($definition['alias']) {
case 'nowTime':
$replacement = date('Y-m-d');
break;
case 'tommorowNow':
$replacement = date('Y-m-d', (time() + 86400));
break;
}
$input = str_replace('{{' . $definition['alias'] . '}}', $replacement, $input);
}
}
return $input;
}
As you can see, for every php-code alias, you can add another case in the switch() statement. You can read up on the switch() control structures at the following link:
PHP: switch - Manual
Is there a shortcut method to assigning $_GET['values'] to variables?
I currently do like others do:
if(isset($_GET['type'],$_GET['case'])
$type = $_GET['type'];
$case = $_GET['case'];
Is there a cleaner method to do this instead of doing like below separately.
$type = $_GET['type'];
$case = $_GET['case'];
http://docs.php.net/extract
I think you're looking for extract function.
extract($_GET); //now, all of the functions are in current symbol table
Well, with array map you can you get the case not just once, but all at once, and you can also check for isset() and empty() at the same time too.
Suppose, you have this URL: read.php?id=1&name=foo&job=student&country=Brazil
Your problem is fetching the $_GET type, and you may need to check if is it empty/isset or not right?
Well, first you create a function to iterate through it.
function checkGet($val){
return (isset($val) && !empty($val)) ? $val : null;
}
Then, you callback that function with array_map()
$check = array_map('checkGet', $_GET);
And that is it!
If you were to do var_dump($check); now, you would get get all the types, and values:
array (size=4)
'id' => string '1' (length=1)
'name' => string 'foo' (length=3)
'job' => string 'student' (length=7)
'country' => string 'Brazil' (length=6)
Meaning, after this, instad of doing:
if(isset($_GET['something']) && !empty($_GET['something']))
$var = $_GET['something'];
echo $var;
Just do:
echo $check['something']
The only one-line code I can think of, to make sure that you still do the necessary checks, is
$type = (isset($_GET['type'])) ? $_GET['type'] : 'a default value or false';
Reading comments, I understand you may want to do this:
foreach($_GET as $key=>$value) {
$$key = $value;
}
I would suggest though, to always initialize the variables you need only. The above code will result in getting unknown variables, which may actually give the user a way to manipulate your script.
Example:
index.php?ExpectedVar=1&UserDefinedVar=2
will generate the following variables in your code:
$ExpectedVar // 1 <- you wanted this one
$UserDefinedVar // 2 <- but what about this?
What if you had this script called by some other script?
Then even if you have this code at the top of your file, you may have some variables overwritten from a user defined $_GET!
Disaster case Scenario:
script1.php
<?php
$tableToDelete = "old_products";
include("script2.php");
?>
script2.php
<?php
foreach($_GET as $key=>$value) {
$$key = $value;
}
// user added &tableToDelete=users
// DROP TABLE $table
// will gloriously delete users
?>
Instead by writing a few lines with the original code I posted, you can get the variables you need at the start of your php script and use them with a clear mind.
Try like
foreach($_GET as $key=>$value) {
$get_arr[$key] = $_GET[$key];
}
print_r($get_arr);
I would do it that way, this way you make sure that it will only return TRUE or FALSE
if (!isset($_GET['type']) || empty($_GET['type'])) {
// Display error
} else {
$type = $_GET['type'];
$case = $_GET['case'];
}
Or you can do it that way as well
$type = (isset($_GET['type'])===false)?'':trim($_GET['type']);
$case = (isset($_GET['case'])===false)?'':trim($_GET['case']);
$_GET is table, so you can easy use foreach function
For example
foreach ($_GET as $key => $value) {
... = $value;
}
If you would like to create variables with $key names use variable variables
PHP Manual Variable Variables
You can do it through extract()
extract($_GET, EXTR_PREFIX_ALL, 'g');
so that
$_GET['val'] becomes $g_val
Note the third parameter: g it prepends g_ to the keys.
This (untested) class should help you:
class MyGet {
public static $myValues = array();
public static function setMyValues($keywords, $where) {
MyGet::$myValues = array();
for ($index = 0; $index < count($keywords); $index++) {
if ((!(isset($where[$keywords[$index]]))) || (empty($where[$keywords[$index]]))) {
MyGet::$myValues = array();
return false;
}
MyGet::$myValues[$keywords[$index]] = $where[$keywords[$index]];
}
}
}
You can use it like this:
if (MyGet::setMyValues(array(0 => "type", 1 => "case"), $_GET)) {
//the values are initialized
} else {
//the values are not initialized
}
I want to pass one argument to a function, rather than multiple arguments, that tend to grow unexpectedly. So I figure an array will get the job done. Here's what I've drafted so far...
<?php
function fun_stuff($var){
// I want to parse the array in the function, and use
}
$my = array();
$my['recordID'] = 5;
$my['name'] = 'John Smith';
$my['email'] = 'john#someemail.com';
echo fun_stuff($my);
?>
I haven't quite grasped the concept of parsing an array. And this is a good way for me to learn. I generally pass the same variables, but on occasion a record does not have an email address, so I do need to make a condition for missing keys.
Am I doing this right so far? Can I pass an array as an argument to a function?
And if so, how do I parse and search for existing keys?
Hopefully this isn't too far off topic...but you sounded like you were just trying to avoid multiple parameters when some can be NULL. So, I would recommend that you use an object instead of an array for clarity...that way, there is no confusion as to what properties should exist. If you're using PHP 5, you can also strongly type the parameter so nothing else can get in. So:
class Record {
public $Id;
public $Name;
public $Email
}
function fun_stuff( Record $record ) {
// you will now have better intellisense if you use an IDE
// and other develoers will be able to see intended parameters
// clearly, while an array would require them to know what's
// intended to be there.
if( !empty($record->Email) ) {
// do whatever.
}
}
Yes you are on the right track. The approach I take is put required paramters as the first parameters and all optional parameters in the last argument which is an array.
For example:
function fun_stuff($required1, $required2, $var = array()) {
// parse optional arguments
$recordId = (key_exists('recordID', $var) ? $var['recordId'] : 'default value');
$name = (key_exists('name', $var) ? $var['name'] : 'default value');
$email = (key_exists('email', $var) ? $var['email'] : 'default value');
}
Then you can call your function like so:
fun_stuff('val 1', 'val 2', array(
'recordId' => 1,
'name' => 'John',
'email' => 'john#stackoverflow.com'
));
This is a bad design practice, but that's not the question here. You can "parse" array's like so...
if( array_key_exists( 'email', $var ))
{
// use email field
}
If you need to, you can loop through all elements like so...
foreach( $var as $key => $value )
{
echo '$var[\''.$key.'\'] = '.$value;
}
I'm not recommend you to use array for this.
You can define optional arguments with default values:
//$name and $email are optional here
function fun($record_id, $name='', $email='')
{
if (empty($name)) print '$name is empty';
}
//Usage:
fun(5, 'Robert');
fun(5);
fun(5, 'Robert', 'robert#gmail');
fun(3,'','robert#gmail');
If you will use array, IDE will not be able to show autocomplete suggestions, it means more typos, and you have to remember all keys of this array forever or look at code of the function each time.
I'm not really sure what you want to achieve, but I suspect something like this:
$aPersons = array();
$aPersons[] = array('name' => 'name1', 'age' => 1);
$aPersons[] = array('name' => 'name2', 'age' => 2);
array_map('parsePerson', $aPersons);
function parsePerson($aPerson) {
echo $aPerson['name'];
echo $aPerson['age'];
}
The problem with your current array is that it only has one dimension.
You can simple do echo $my['name'];. There are easier ways to parse arrays though.
foreach($aPersons as $aPerson) {
echo $aPerson['name'];
echo $aPerson['age'];
}
$iLength = sizeof($aPersons);
for($i = 0; $i <= $iLength; $i++) {
echo $aPersons[$i]['name'];
echo $aPersons[$i]['age'];
}
To parse and view, there is the signficant print_r function which gives out the array details.
When calling a function you need the return syntax at the end that will parse out anything you call in the return.
You obviously can pass array to the function. Inside it read the variable as you were assigning values before it. If you assign:
$my['key'] = 'value';
In you function use:
echo $var['key'];
Why you don't use a foreach to walk in array?
function fun_stuff($var){
foreach($var as $key => $item){
echo '[', $key, "] => ", $item, "\n";
}
}
$my = array();
$my['recordID'] = 5;
$my['name'] = 'John Smith';
$my['email'] = 'john#someemail.com';
fun_stuff($my);
Yes, this is correct (though your question is a bit broad). You're already referencing the array values via indexes when you set up the $my variable. You can do the same thing within your function (with the $var variable).
I recommend taking a look at all of PHP's built-in array functions: http://php.net/manual/en/ref.array.php
Try this:
function fun_stuff($var){
// I want to parse the array in the function, and use
$fun_string = "";
if( is_array( $var ) {
if( array_key_exists( "name", $var ) )
$fun_string .= "For " . $var["name"];
else $fun_string .= "A nameless one ";
if( array_key_exists( "email", $var ) )
$fun_string .= " (email: " . $var["email"] . ")";
else $fun_string .= " without a known e-mail address";
if( array_key_exists( "recordID", $var ) )
$fun_string .= " has record ID of " . $var["recordID"];
else $fun_string .= " has no record ID set";
$fun_string .= "\n";
}
return $fun_string;
}
Yes You can! Just pass the array and inside the function just use a foreach loop to parse it!
function myFunction($array)
{
foreach($array as $value)
{
echo $value;
}
}
or If you want to have full control over the pair key/value:
function myFunction($array)
{
foreach($array as $key=>$value)
{
echo "key:".$array[$key]."value:".$values;
}
}