Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want to find how to get number of variables to be passed in function.
say suppose I have function
public function abc($id,$name,$email){
....
}
Now there is another function xyz in want to know number of variables that are need to be passed in this function.
Thanks in advance.
Now there is another function xyz in want to know number of variables that are need to be passed in this function.
Use this:
$rfunc = new ReflectionFunction('xyz');
echo $rfunc->getNumberOfRequiredParameters();
But I do not know for what this should be useful...
if you did not know how many number of variables that are need to be passed in calling function, then you can make use default value of parameter concept.
For ex:
// Function call-1
abc($a,$b,$c);
// Function call -2
abc($a,$b,$c,$d);
// Function defination goes here
function abc($a,$b,$c,$d=0)
{
/* if you did not pass $d value , $d gets assigned automatically to 0
as default value. So in this way, you should not be worried about how
many parameters you should pass */
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I was wondering if you could give two values to a class and then acces to the second one by POST, something like this (part of the code):
echo "<select name='selecttsk' id='selecttsk'>";
while($w = $bd->obtener_fila($tasker, 0)){
echo "<option class='opcion1' value ='".$w[1]/$w[2]."'>".$w[0]."</option>";
}
echo "</select>";
?>
and then i need to do something like this in other file
$var = $_POST[selecttsk];
but i need $w[2]
thanks
I suppose your $_POST['selecttsk'] does have the values in the following format:
"foo/bar"
You could use "explode" in PHP to get the second part (bar), for example:
$postvar = $_POST['selecttsk'];
$vars = explode("/", $postvar);
// Then
$var = $vars[1]; // Will be the $w[2] from the form
Look into: http://php.net/explode
Beware that if $w[1] or $w[2] ever contains a "/" you might get unexpected results, you could use the limit function of explode to mitigate that issue.
However - I would generally not recommend this workflow.
Why do you need to send two variables with one select?
(could you show us som example of what $w contains)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an array like
$remarks = array('Poor','Fair','Good');
I want to access the array index by function call in it.
echo $remarks[myindex($id)];
where myindex() is a function which returns some number value from database.
How can I access the array index of my array at run time
As per you say..
myindex is a function which returns a value between 0-2 then I want to
display array value of that index
<?php
$remarks = array('Poor','Fair','Good');
function myIndex()
{
return rand(0,2);
}
echo $remarks[myIndex()]; //"prints" either Poor , Fair or Good randomly..
The myIndex() function returns a random value between 0,1 or 2 , so that is passed as the index value to your array and it prints the values either Poor , Fair or Good.
it may be
function myindex($id){
return $id % 3;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
function myCheck($in)
{ return isset($in); }
$var1='Something';
$var2='$var1';
$var3='$varNonExitant';
What I'm trying to achive is to use myCheck to evaluate the existance of the content like this:
myCheck($var2) return true;
myCheck($var3) return false;
isset() is not really a function: it's a language construct. As such, it's allowed to do some magic that's not available to regular functions, such as being fed with non-existing variables.
To sum up: you cannot replicate it with a custom function.
Edit:
As DaveRandom pointed out in a comment below, all you can do is come close by checking if a variable isset for example:
function variable_isset(&$variable = NULL) {
return isset($variable);
}
This approach offers two drawbacks though:
This works by passing the unset variable by reference, thus creating it when called. As it's NULL it is still not set.
It'll trigger an Undefined variable notice if the variable does not exist, ruining the whole concept of gracefully handling optional variables.
Most likely this should not be needed. So question remains why you can not use isset in the first place which would be much more needed to give you better guidance.
When you cyall myCheck($abc), with set $abc = 123, it gets myCheck(123). It isn't any valid argument for isset.
You have to give the function a string with variable name:
function isset_global($variable_name)
{
return isset($GLOBALS[$variable_name]);
}
Of course, I am also wondering, why to do this, but it answers the question as long as you check a global variable.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i writing a PHP script that call a file in the server
by using :
<?php
$ret=system("command");
?>
the problem is when the file need some parameters
i can't find a way of doing that
because when using
<?php
$ret=system("command");
?>
the PHP skips that part of asking for variables
and assign to id a random one
and i can't pass theme at the start like
$ret = system("command argument1 argument2 argument3...");
beause the nmber of parametres depend on the user
i mean he keep entring data to a dynamic array entill he enter"end"
$ret = system("command argument1 argument2 argument3...");
Just load the arguments on, just like you were calling the program from a command line.
$cmd = "cmd param1 param2";
system($cmd,$return_value);
($return_value == 0) or die("returned an error: $cmd");
If what you mean is that you have an array that can have any number of parameters use this:
$commandParameters = implode(" ",$dynamicArray);
$ret = system("command ".escapeshellarg($commandParameters));
Get the parameters from the user via. HTML form and then, when you know what (and how many) the parameters are - you can use "system()" like everyone suggested!
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm finishing a script that converts a string and pass it as a link, but besides that, it also shortens the URL with an API. The problem is that I can not think how to get only the URL instead of the entire chain.
The function:
function findAndShort($string) {
$text = preg_replace("/(https?|ftps?|mailto):\/\/([-\w\p{L}\.]+)+(:\d+)?(\/([\w\p{L}\/_\.#]*(\?\S+)?)?)?/u", '$0', $string);
return $text;
}
Example:
$chk = findAndShort("Blahblah http://domain.tld");
echo $chk;
In this case, only need the http://domain.tld, i try with $chk[0], but ofcourse, print the first character on the line..
Just add ^.* at the begining of your regex and use $1 instead of $0:
$text = preg_replace("/^.*((https?|ftps?|mailto):\/\/([-\w\p{L}\.]+)+(:\d+)?(\/([\w\p{L}\/_\.#,]*(\?\S+)?)?)?)/u", '$1', $string);
You can also simplified a bit,
~^.*((https?|ftps?|mailto)://[-\p{L}\p{N}_.]+(:\d+)?(/([\p{L}\p{N}/_.#,]*(\?\S+)?)?)?)~u
The thing with functions is you can only return a single value that is under the return in the function, so in your case, after testing your function, the return value is the original input value.
If you want a function to return only the URL component I would use explode to separate the string then run preg_match on each part of the resulting array from the explode function looking for the https, ftp or mailto, once I know what part of the array I'm working with I would stop the process, define the var and as I believe you want to return code for a link, I would do is then do something like
return ''.$url.'';
Note that the var $url would be created from the above process.
I hope this helps.