This question already has answers here:
Using Default Arguments in a Function
(16 answers)
Closed 5 years ago.
when and where I should keep the Parameters Null or FALSE?
for stance here is a function... and what this function will do when i called it or how can i call it?
<?php
function Show_result($id=Null, $name=FALSE){
//whatever i want code goes here
}
?>
In this declaration you set default values so you can call this function without passing parameters.
This could be useful if you don't want to pass the same parameters most of the times but still be able to pass specific ones in other cases.
For example, a function like this :
function log($message, $tag = null) {
if ($tag == null)
$tag = "info";
echo $tag . ": " . $message;
}
This function can be called two ways:
log("text")
or
log("text", "error")
The first call will print
info: text
The second will print
error: text
Related
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:
Are PHP Variables passed by value or by reference?
(16 answers)
How to declare a global variable in php?
(10 answers)
Closed 3 years ago.
I would like to be able to assign the name of a variable outside the function so that the function can assign the chosen variable. It does not seem to work. Anyone have any ideas?
Below is my attempt, however the $admin variable is empty after running the function.
function assign_check($variable, $check) {
if (empty($_POST[$check])) {
$variable = "no";
} else {
$variable = $_POST[$check];
}
}
assign_check('$admin', 'admin');
My question is not about the use of global variables.
You can request a reference in the function body.
function assign_check(&$variable, $check) {
$variable = 'hello';
}
And call passing a variable (reference).
assign_check($admin, 'admin');
$admin value is now 'hello';
Fitting that to your code would result in
function assign_check(&$variable, $check) {
$variable = empty($_POST[$check]) ? "no" : $_POST[$check];
}
assign_check($admin', 'admin');
But returning a proper value would be much cleaner code than using references. Using a ternary operator like presented above would it even simplify without need of a function at all.
A normal way to assign the result of a function to a variable name specified outside the function would be to have the function return the result and assign it directly to the variable when you call the function.
function assign_check($check) {
if (empty($_POST[$check])) {
return "no";
} else {
return $_POST[$check];
}
}
$admin = assign_check('admin');
I would do it this way unless there was a compelling reason to do it otherwise.
For the specific type of thing it looks like this function is intended to do, I would suggest looking at filter_input.
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.
I have function as below.
function test($username, $is_active=1, $sent_email=1, $sent_sms=1) {
echo $sent_email; // It should print default ie 1
}
Call to function :
test($username, 1, null, 1);
How to call function if need to use default value in function. $sent_email should be 1. Can not change sequence of parameter.
When you start the value parameters happens:
function makecoffee($type = "cappuccino")
{
return "Making a cup of $type.\n";
}
echo makecoffee();
echo makecoffee(null);
echo makecoffee("espresso");
?>
The above example will output:
Making a cup of cappuccino.
Making a cup of .
Making a cup of espresso.
To fulfill what you want to check with conditions as follows:
function test($username, $is_active=1, $sent_email=1, $sent_sms=1) {
if($sent_email!=1)
$sent_email=1;
echo $sent_email; // It should print default ie 1
}
In php you cannot declare more than 1 parameter with default value in your function.
Php could not know wich param you do not give if you have multiple default value...
In your case, you give the param, with null value. That's a lot different ! So, you can use the following :
function test($username, $is_active, $sent_email, $sent_sms) {
$username = ($username != null) ? $username : 1;
$is_active = ($is_active != null) ? $is_active : 1;
$sent_email = ($sent_email != null) ? $sent_email : 1;
echo $sent_email; // It should print default ie 1
}
as it, if you give null, your function will use "1" value, and if not null, the value you pass as parameter ;)
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);