This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
named PHP optional arguments?
I want to do this:
function they_said($alice = "", $bob = "", $charlie = "") {
echo "Alice said '$alice'";
echo "Bob said '$bob'";
echo "Charlie said '$charlie'";
}
they_said($charlie => "Where are the cookies!?");
That way I can ignore passing the first 2 arguments and simply pass the one I want.
Here's an example in python.
No, but you can pass an array:
function they_said($persons)
{
foreach ($persons as $person => $text)
{
echo "$person said '$text'";
}
}
they_said( array('charlie' => 'Where are the cookies!?') );
Related
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 answers here:
Replace Exact Occurrence of Word in PHP?
(3 answers)
Closed 2 years ago.
I have an issue with str_replace,
I have an object which has strings 'menu-item', 'item'.
All I want is to replace 'item' with 'item new-one', I tried:
function wp_list_pages_filter($output) {
$output = str_replace('item', 'item new-one', $output);
return $output;
}
And it's working, but the problem that the function also replace 'menu-item' string, because it includes 'item'.
How to replace only 'item'? If it has additional symbols the function should ignore this. The function should be universal, not depends on exact characters.
Can you help me, please? Thanks in advance.
It was mentioned above but a simple if condition will solve your problem, plenty of different ways to use this solution. Inside our outside your function.
Inside
function wp_list_pages_filter($output) {
if ($output != 'menu-item') {
$output = str_replace('item', 'item new-one', $output);
}
return $output;
}
Outside
$if ($output == 'item') {
$output = 'item new-one';
}
This question already has answers here:
How would I skip optional arguments in a function call?
(21 answers)
Closed 7 years ago.
I have a function like:
function do_something($first, $second=null, $third=null) {
if ($isset($second)) {
// Do something here
}
}
Now, I want to pass a value for $third to the function, but I want $second to remain NULL:
do_something('abc','','def');
Now, $second is no longer NULL. How can I pass a value for $third while leaving $second NULL?
There is no other way:
do_something('abc', null,'def');
I think it is quite easy to do this:
do_something('abc', null,'def');
Otherwise, I am linking this SO question which uses array arguments, in order to have optional arguments (credits to sanmai - I'd take no credit for this):
function do_something($arguments = array()) {
// set defaults
$arguments = array_merge(array(
"argument" => "default value",
), $arguments);
var_dump($arguments);
}
With example usage:
do_something(); // with all defaults, or:
do_something(array("argument" => "other value"));
This question already has answers here:
How do I dynamically write a PHP object property name?
(5 answers)
Closed 7 years ago.
This is not only tricky to explain, but tricky to do:
I'm trying to access and replace
$myObject->customField[0] = "some value";
but if I do
$str = "customField";
$myObject->$str[0] = "some value";
That doesn't work and if I do
$str = "customField";
$obj = $myObject->$str;
$obj[0];
That won't work either. I can change the values if I don't do this dynamically but I'm having to loop through a lot so doing it dynamic will be very helpful.
EDIT (answer)
Turns out curly braces does the trick. ie
$str = "customField";
$myObject->{$str}[0] = "some value";
Why do you want to have dynamic property names? The best answer is: don't do it this way. Consider using an associative array instead:
$myObject->customFields = array();
$myObject->customFields[$str] = "some value";
This question already has answers here:
PHP Object Variable variables name?
(5 answers)
Closed 9 years ago.
Why does this work
foreach ($items as $i) {
$dataTitle = $this->dataTitle;
$title = $i->$dataTitle;
}
when this doesn't?
foreach ($items as $i) {
$title = $i->$this->dataTitle;
}
Is there a better way to do this?
Try this:
$title = $i->{$this->dataTitle};
Your expression is being parsed as:
$title = ($i->$this)->dataTitle;
$this referes to current object parsed in not obvious order. You need to use {expr} notation, to dynamicly evaluate property name.
Try to use {} around $this->dataTitle:
$title = $i->{$this->dataTitle};
Look at bottom part of last example in variable variables section of manual.