This question already has answers here:
original variable name passed to function? [duplicate]
(2 answers)
Closed last month.
The community reviewed whether to reopen this question last month and left it closed:
Original close reason(s) were not resolved
I would like to get the name (not the value) of a paramter.
In the given case it should be $param.
function testfunc($param = 'i do not want this..', $array = array()) {
$args = func_get_args();
var_dump($args);
echo $param;
}
If I call the testfunc like this:
testfunc($testString,$testArray);
the result should be "$testString" ,"$testArray"
Is there a way in PHP?
Use reflection
$myParams = array_map( function( $parameter ) { return $parameter->name; }, (new ReflectionFunction(__FUNCTION__))->getParameters() );
Thanks
This question already has an answer here:
How to get the string name of the argument's type hint?
(1 answer)
Closed 2 years ago.
I have method which cast array to object by using
$class = get_class($object);
$methodList = get_class_methods($class);
But now I need had information about expected type of variable too. For example from this method:
public function setFoo(int $foo)
{
}
I need get int too. There is any option to get it?
You can use Reflection. Specifically ReflectionParameter::getType().
function someFunction(int $param, $param2) {}
$reflectionFunc = new ReflectionFunction('someFunction');
$reflectionParams = $reflectionFunc->getParameters();
$reflectionType1 = $reflectionParams[0]->getType();
$reflectionType2 = $reflectionParams[1]->getType();
assert($reflectionType1 instanceof ReflectionNamedType);
echo $reflectionType1->getName(), PHP_EOL;
var_dump($reflectionType2);
The above example will output:
int
NULL
This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 8 years ago.
What does "&" mean in '&$var' in PHP? Can someone help me to explain this further. Thank you in advance.
It means pass the variable by reference, rather than passing the value of the variable. This means any changes to that parameter in the preparse_tags function remain when the program flow returns to the calling code.
function passByReference(&$test) {
$test = "Changed!";
}
function passByValue($test) {
$test = "a change here will not affect the original variable";
}
$test = 'Unchanged';
echo $test . PHP_EOL;
passByValue($test);
echo $test . PHP_EOL;
passByReference($test);
echo $test . PHP_EOL;
Output:
Unchanged
Unchanged
Changed!
You can pass a variable to a function by reference. This function will be able to modify the original variable.
You can define the passage by reference in the function definition with &
for example..
<?php
function changeValue(&$var)
{
$var++;
}
$result=5;
changeValue($result);
echo $result; // $result is 6 here
?>
By default, function arguments are passed by value (so that if the value of the argument within the function is changed, it does not get changed outside of the function). To allow a function to modify its arguments, they must be passed by reference.
To have an argument to a function always passed by reference, prepend
an ampersand (&) to the argument name in the function definition.
This question already has answers here:
difference between call by value and call by reference in php and also $$ means?
(5 answers)
Closed 6 years ago.
I am wondering about returning value to a parameter instead of using =
example of "normal" way how to get return value
function dummy() {
return false;
}
$var = dummy();
But I am looking for a method how to associate return value without using =, like preg_match_all()
function dummy($return_here) {
return false;
}
dummy($var2);
var_dump($var2); //would output BOOLEAN(FALSE)
I am sure it has something to do with pointers but in preg_match_all() I never send any pointer to variable or am I mistaken?
preg_match_all('!\d+!', $data, $matches); // I am sending $matches here not &$matches
//I didn't know what to search for well it is simple CALL BY REFERENCE
It is CALL BY REFERENCE:
function dummy(&$var) { //main difference is in this line
$var = false;
}
dummy($var);
Use references
i.e.
function dummy(&$return_here) {
$return_here = false;
}
dummy($var2);
var_dump($var2);
This question already has answers here:
Does PHP allow named parameters so that optional arguments can be omitted from function calls?
(17 answers)
Closed 2 years ago.
In PHP you can call a function with no arguments passed in so long as the arguments have default values like this:
function test($t1 ='test1',$t2 ='test2',$t3 ='test3')
{
echo "$t1, $t2, $t3";
}
test();
However, let's just say I want the last one to be different but the first two parameters should use their default values. The only way I can think of is by doing this with no success:
test('test1','test2','hi i am different');
I tried this:
test(,,'hi i am different');
test(default,default,'hi i am different');
Is there clean, valid way to do this?
Use arrays :
function test($options = array()) {
$defaults = array(
't1' => 'test1',
't2' => 'test2',
't3' => 'test3',
);
$options = array_merge($defauts, $options);
extract($options);
echo "$t1, $t2, $t3";
}
Call your function this way :
test(array('t3' => 'hi, i am different'));
You can't do that using raw PHP. You can try something like:
function test($var1 = null, $var2 = null){
if($var1 == null) $var1 = 'default1';
if($var2 == null) $var2 = 'default2';
}
and then call your function, with null as the identifier of the default variable. You can also use an array with the default values, that will be easier with a bigger parameter list.
Even better is to try to avoid this all, and rethink your design a bit.
The parameters with default values have to be last, after the others, in PHP and all the others up to that point must be filled in when calling the function. I don't know of anyway to pass a value that triggers the default value.
What I normally do in those situations is to specify the parameters as an array instead. Have a look at the following example (untested):
<?php
test(array('t3' => 'something'));
function test($options = array())
{
$default_options = array('t1' => 'test1', 't2' => 'test2', 't3' => 'test3');
$options = array_merge($default_options, $options);
echo $options['t1'] . ', ' . $options['t2'] . ', ' . $options['t3'];
}
?>
you could define the function like:
function grafico($valores,$img_width=false,$img_height=false,$titulo="title"){
if ($img_width===false){$img_width=450;}
if ($img_height===false){$img_height=300;}
...
}
and call to it without the lasting params or
replacing one or several with "false":
grafico($values);
grafico($values,300);
grafico($values,false,400);
grafico($values,false,400,"titleeee");