We got a PHP file in school with some functions and one of them is the following:
function serviceRec($db,$table,$afields=null,$avalues=null){ .... }
My question: What does the $afields=null and $avalues=null mean?
Thank you!
function serviceRec($db,$table,$afields=null,$avalues=null){ .... }
It means that, when you call your function and don't pass those parameters then it'll by default place value as null
Example :
function hello($name = "anonymous"){
return "Hello $name \n";
}
echo hello();//Hello anonymous
echo hello("BigSeeProduction");//Hello BigSeeProduction
DOCS
These assignments are default values. If you were to call the function as e.g.
serviceRec($a, $b)
the omitted parameters would be assumed to be null. If, on the other hand, you called the function as e.g.
serviceRec($a, $b, $c, $d)
$afields would be set to $c and $avalues to $d.
Of course, you could also call with 3 parameters.
It Means it's the default value. So when u don't fill this parameter it will be set as null.
See the man here :
PHP.net : default value function
That indicates, that if you leave that parameter out(Don't specify it at all), the value after the =, in this case null is used. So if you don't care about these parameters just leave them out. It has the same effect as just supllying null.
Related
These function statements are confusing me.
I'm new to php, help me to understand these functions:
function addFive($num)
{
$num += 5;
}
function addSix(&$num)
{
$num += 6;
}
$orignum = 10;
addFive( $orignum );
echo "Original Value is $orignum<br />";
addSix( $orignum );
echo "Original Value is $orignum<br />";
first echo outputs 10
Second echo outputs 16
What is the difference between these 2 functions?
There are two types of call:
1) Call by value: addFive($num)
2) Call by reference: addSix(&$num)
In first case, you are just passing value of the variable.
Hence, only value gets modified keeping original variable untouched.
In second case, you are passing reference to the variable, hence the original value gets modified.
The first function passes the argument by value - in other words, it's copied into the function, and any change you perform on it will be on the local copy.
The second function passes the argument by reference (note the & before it in the function's signature). This means the variable itself is passed, and any modification you perform on it will survive beyond the function's scope.
& is used to pass address of an variable in second function declaration "addSix(&$num) {}"
In second function while calling addSix( $orignum ); updation of value is done on address of "$orignum"
whereas in first function updation is done on "$num"
First function add 5 to your number and second adds 6 to your number
$num+=6 means $num= $num+6
And first function works on Call by value and second function works on Call by reference
Why some programmer set null in function arguments ?
like:
function func ($arg1, arg2 = null)
{
print $arg1.' '.$arg2;
}
So I can call it with this: func('test1') (without wrote $arg2) it print test1 but if call func('test1','test2')... it only print test1.
Also speed of run and debug function is very important for me...also all functions are static...I must bring function under a class or without class is more fast? I am not care about object oriented ...only speed.
There is a nice trick to emulate variables/function calls/etc as default values:
<?php
$myVar = "Using a variable as a default value!";
function myFunction($myArgument=null) {
if($myArgument===null)
$myArgument = $GLOBALS["myVar"];
echo $myArgument;
}
// Outputs "Hello World!":
myFunction("Hello World!");
// Outputs "Using a variable as a default value!":
myFunction();
// Outputs the same again:
myFunction(null);
// Outputs "Changing the variable affects the function!":
$myVar = "Changing the variable affects the function!";
myFunction();
?>
In general, you define the default value as null (or whatever constant you like), and then check for that value at the start of the function, computing the actual default if needed, before using the argument for actual work.
Building upon this, it's also easy to provide fallback behaviors when the argument given is not valid: simply put a default that is known to be invalid in the prototype, and then check for general validity instead of a specific value: if the argument is not valid (either not given, so the default is used, or an invalid value was given), the function computes a (valid) default to use.
Incorrect usage of default function arguments
<?php
function makeyogurt($type = "acidophilus", $flavour)
{
return "Making a bowl of $type $flavour.\n";
}
echo makeyogurt("raspberry"); // won't work as expected
?>
The above example will output:
Warning: Missing argument 2 in call to makeyogurt() in
/usr/local/etc/httpd/htdocs/phptest/functest.html on line 41
Making a bowl of raspberry .
Now, compare the above with this:
Correct usage of default function arguments
<?php
function makeyogurt($flavour, $type = "acidophilus")
{
return "Making a bowl of $type $flavour.\n";
}
echo makeyogurt("raspberry"); // works as expected
?>
The above example will output:
Making a bowl of acidophilus raspberry.
Note: As of PHP 5, arguments that are passed by reference may have a default value.
Here are few links I am adding for you to learn function arguments in depth -
http://www.w3schools.com/php/php_functions.asp
http://php.net/manual/en/functions.arguments.php
First of all, I can't recall the name of this process, but it looks something like this:
function test($alter = FALSE){
//do stuff
return $alter;
}
Making $alter = FALSE right in the function declaration
What is that called? How does this work? What happens in the following circumstances?
$result = test();
$result = test(TRUE);
FALSE is defined as the default value if no other value is passed.
In the case of you examples the results (in order) would be:
FALSE
TRUE
FALSE defined in method header is the default value (if nothing is added to parameter while calling) - test() otherwise it behaves like a normal parameter.. so if you call test(TRUE) value will be TRUE
Nothing to add except: The term that you probably might be remembering is "function overloading" but this isn't a real embodiment of this (it's just PHP's "default parameter" is perhaps similar)
"<?php
echo"welcome";
function a($b=false){
echo"<br /> b: ".$b;
}
a(true);
a();
a("some text");
a(false);
?>
result :
welcome
b: 1
b:
b: some text
b:
"
it seems that if its false/null/empty it does not print anything..
and what ever you pass to that method string/boolean it does print as long as not null/empty.
function test($param=null) {
if ($param===null)
.....
}
Since $param is set to null at function header, why even bother test if $param===null? If there a case $param wouldn't be null?
Since $param is set to null at function header, why even bother test
if $param===null? if there a case $param wouldn't be null?
That is optional argument because you define default value of null to it.
As for why bother checking it, you want to make sure a parameter was indeed specified which is NOT null.
Let's assume you wanted to echo it:
function test($param=null) {
echo $param;
}
When you call the function, nothing would happen and you don't want to do that, right. For that reason, you want to make sure that value of argument is NOT null so that you could manipulate it however you like.
Tests:
function test($param=null) {
echo $param;
}
test(); // no output
test('hello there'); // output: hello there
That is an optional/default argument.
If you call that function, then you can call it one of two ways:
test($value);
or
test();
In the first case, $param holds the value of $value. In the second case, $param is always null.
$param will only be null if no value is passed to the function. This is an example of optional parameters.
You could call the function by passing a value
test(10); //$param inside the method will be 10;
test(); //$param will be null
$param=null is a default variable only, overwritten when the function is called with a variable.
if you call the function using
$helloworld = test('notnull');
then $param will equal 'notnull' in the function.
A case $param wouldn't be null:
test("ok");
In this case, $param = "ok".
Example:
function example($x = "")
{
Do something
}
Isn't $x empty by default already? Why set it explicitly?
Isn't $x empty by default already?
If no default is specified, $x is not an empty string by default, but an undefined variable. There is a difference between "" and NULL or undefined.
However, setting the default allows you to omit the parameter when calling the function, without it throwing a warning.
<?php
function test1($x = "DEFAULT") {
echo $x;
}
function test2($x) {
echo $x;
}
// Call each without the parameter $x:
test1();
// DEFAULT
test2();
// Output:
PHP Warning: Missing argument 1 for test2(), called in /home/mjb/test.php on line 10 and defined in /home/user/test.php on line 5
PHP Notice: Undefined variable: x in /home/user/test.php on line 6
The main reason is that setting a default on the declaration makes the argument optional:
$a = example();
$b = example(5);
One reason is so when you reuse the function you dont have to explicitly set the variable. This happens a lot when a default is set to true or false. That way a function can seem to be overloaded like you can do in other oop languages. If that variable didn't contain a default value, you'd always have to set that value or your function would throw an error, however, by setting the variable to a default value you wouldn't have to necessarily set the value of the variable. Hope that helps some :)
Once of the reasons I use default values is so that I do not have to declare the variable when calling function ie:
function something(debug=false){
doing something here;
if ($debug === true){
echo 'SOMETHING';
}else{
return true;
}
}
This way you can debug something bu simply adding the variable to the function call but if you dont' add it the functions assumes it is false. This is valuable in my $_GET security function that I am using to encrypt my $_GET strings when I turn on debug the $_GET is decoded and dumped as an array inside a
<pre>print_r($_GET);</pre>
so that I can see what the values are in the $_GET but the string is still encrypted in the address bar.
Hope that helps