PHP pass second argument into function [duplicate] - php

This question already has answers here:
How to set optional parameter to default without passing it?
(4 answers)
Closed 5 years ago.
I have a function like this:
function myfunc(arg1=false, arg2=false){
//some code here......
}
If I only want to pass arg1 into the function, I can write like this myfunc("arg1 only variable"). But how can I pass only arg2 into the function?
Thank you.

try like this,
myfunc(false, $arg2);

Any of the following would be legal calls to your function:-
myfunc();
myfunc(true, true);
myfunc(true, null);
myfunc(null, true);
myfunc(null, null);

Related

What is the difference between "?" and "= null" in PHP function parameters [duplicate]

This question already has answers here:
What is the purpose of the question marks before type declaration in PHP7 (?string or ?int)?
(4 answers)
Closed 1 year ago.
I was doing some research but wasn't able to find an answer (probably beacause I did not searched it right)
Consider this piece of code:
public function foo(?array $optionalParam);
And then this one:
public function foo(array $optionalParam = null);
What differs between them? Using PHPstorm I noticed that when I use the ?, it creates a PHPdoc and mark the variable type as type|null. But when I call the function without that argument, PHP screams on my face "you kidding me? where is $optionalParam". In the other side, I managed to use with no problems the =null option.
Sorry if this question is too simple, but i did not find any answers online.
First of all, the ? goes before the type, not after... other than this:
Using
public function foo(?array $optionalParam);
you are forced to pass something, that can be either null or an array, infact:
<?php
function foo(?array $optionalParam){
echo "test";
}
foo(); // doesn't work
foo(null); // works
foo([]); // works
where instead using
public function foo(array $optionalParam = null);
will accept null, an array, or 0 parameters
<?php
function foo(array $optionalParam = null){
echo "test";
}
foo(null); // works
foo(); // work
foo([]); // works
It's a PHP 7.1 feature called Nullable Types
Both of the lines you wrote are identical.
array ?$optionalParam : either an array or null
array $optionalParam = null : either an array or null
Tho using ? you'd still need to add the parameter when calling the function.

What does these ... mean in this example? [duplicate]

This question already has answers here:
What is the meaning of three dots (...) in PHP?
(9 answers)
Closed 5 years ago.
I'm learning PHP http://php.net/manual/en/migration70.new-features.php and in the following example, I don't understand ... prepended with the $ints parameter in the function definition.
<?php
// Coercive mode
function sumOfInts(int ...$ints)
{
return array_sum($ints);
}
var_dump(sumOfInts(2, '3', 4.1));
Can anybody please tell me what those dots are for?
Thanks.
that means that when you call that function, you can pass X integers and the function will process them, doesn't matter how many are they. If you call sumOfInts(3,4,6,2,9) or sumOfInts(3,2,9), the function works, no matter how many arguments you pass

Call a dynamic function in php [duplicate]

This question already has answers here:
Use a variable to define a PHP function
(1 answer)
How to call a function from a string stored in a variable?
(18 answers)
Closed 5 years ago.
Is there a way to call dynamic functions in php. For an example lets guess I have a variable like below.
$myVar = 'my_test_function';
And I have a function named myTestFunction(). Is there a way to call the mentioned function using the $myVar variable's value? How to structure that variable as myTestFunction and call the mentioned function using that variable
Just do the following:
lcfirst(str_replace('_', '', ucwords($myVar, '_')))();
convert your string to function name first:
$myVar = 'my_test_function';
echo $myVar = str_replace('_', '', ucwords($myVar, '_')); //output myTestFunction
$myVar();

Directly execute anonymous function? [duplicate]

This question already has answers here:
How do I immediately execute an anonymous function in PHP?
(9 answers)
Closed 8 years ago.
Currently I am doing this:
$f = create_function(null, somecode);
$f();
Is there anyway to not assign it to a variable, but instead directly execute it?
I'm thinking of something like
create_function(null, somecode)();
But this is not working unfortunately. Do not ask me why I want to do this, there is no special reason, I was just wondering this the other day.
You can just execute an anonymous function...
call_user_func(function(){
echo 'I am a function!';
});
PHP VERSION > 5.3
Normal closures, > PHP 5.3:
$func = function($str) { echo $str; };
$func('hello world');
If you directly want to execute code, why would you even want to put it into a function?
Turning the above code into the following is equivalent to instantly executing:
echo 'hello world';

php function variable arguments [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to pass variable number of arguments to a PHP function
I make my framework in php
I want to have variable function arguments
ex
if I have 2 parameter
$a,$b;
the function become
function name($a,$b);
and if I have 3 parameter
$a,$b,$c;
the function become
function name($a,$b,$c);
Check out http://www.php.net/manual/en/functions.arguments.php#functions.variable-arg-list. Is that what you're looking for? If not, can you just accept an array?

Categories