Sorry for such a lame title, but I just had no idea what to put there, hope you understand it. Plus, I have no idea if similar question has been asked before, because I don't know the proper keywords for it - therefore couldn't google it too.
Basicly... When looking at preg_match_all();, they got this matches parameter that will create new array defined in function, and give us the ability to access it after function execution.
And the question is.. How can I implement such a feature in my own function? But that it could create single variable and/or array.
Thanks in advance!
preg_match_all() accepts a reference to an array, which in its own scope is called $matches. As seen in the function prototype:
array &$matches
If you call the function and pass in a variable, if it does not already exist in the calling scope it will be created. So in your user-defined function, you accept a parameter by reference using &, then work with it inside your function. Create your outer-scope variable by simply declaring it in your function call, like you the way you call preg_match_all() with $matches.
An example:
function foo(&$bar) {
$bar = 'baz';
}
// Declare a variable and pass it to foo()
foo($variable);
echo $variable; // baz
I think you are referring to function parameters passed by reference, are you not?
function putValInVar(&$myVar, $myVal){
$myVar = $myVal;
}
$myVar = 1;
putValInVar($myVar, 2);
echo $myVar; // outputs '2', but will output '1' if we remove the '&' //
By default function arguments in PHP are passed by value. This means that new variables are created at each function call and those variables will exist only inside the function, not affecting anything outside it.
To specify that an argument should be used by reference the syntax is to append an & before declaring it in the function header. This will instruct PHP to use the passed variable inside the function rather than creating a copy of it.
Exception: Objects are always passed by reference. (Well... Not really, but it's complicated. See the comment thread for more info.)
I think what you are asking for is passing-by-reference. What preg_match_all basically does to "create" an array variable outside its scope is:
function preg_match_all($foo, $bar, & $new_var) {
$new_var = array(1,2,3);
}
The crucial point here is & in the function definition. This allows you to overwrite variables in the outer scope when passed.
Stylistically this should be used with care. Try to return arrays or results instead of doing it via reference passing.
Like this:
$myvariable = runfunction();
function runfunction() {
//do some code assign result to variable (ie $result)
return $result;
}
Or
global $result;
function runfunction() {
global $result;
$result = 'something';
}
Related
Is it considered wrong to pass a variable to a function where the variable passed has the same name as the variable in the function itself? I never see anyone do this, but wondered if there was a reason why (other than readability). Example:
$variable = "value";
function myFunction($variable) {
return $variable;
}
No there is not problem with doing this except some naming confusion. Because,
$variable = "value"; // this declaration belong to a global scope
// but $variable passing through as argument, makes it scope within that function declaration only
function myFunction($variable) {
return $variable;
}
You might have expect that if you call the function like myFunction() i.e. without any parameter, it will return you "value", but absolutely not, cause $variable passed as argument is not the global one.
But if you try to use the global $variable within the myFunction, then it will cause some conflict, that is why no one do this.
Aside from making variables a bit confusing to track, there is nothing wrong with this. They exist in different scopes.
It is up to you but vice versa, if you want to pass intentionally a variable into a function.. then you have to use this:
$variable = "value";
function myFunction($variable) {
global $variable;
return $variable;
}
echo myFunction('another value');
One more point. When creating functions you may set a default value for your params to avoid errors:
function myFunction($variable="default value") {
return $variable;
}
echo myFunction();
There is nothing wrong with it at all, and I would have thought it's quite common.
However, you should always be choosing the best name for a variable, or a parameter, in each situation, not applying one rule to your whole code.
For instance, in a database lookup function, it might be obvious what $id refers to, so it would be a sensible parameter name; but where you are calling it, you will likely have lots of IDs of various sorts, so a more descriptive variable name should be used. On the other hand, each time you build an SQL string, it's probably the only such variable in scope, so giving it a consistent name keeps your code tidy and easy to read.
A common observation is that all code is read more often than it is written (yes, even your personal website that you wouldn't dare share with the world). Variable names should be chosen with that in mind: what makes this code easy to read, understand, and maintain?
You can use global and add default value in param variable.
$variable = "value";
function myFunction($variable = '') {
global $variable;
return $variable;
}
im new to PHP and i have come across two ways that people declare their functions parameters.
function fun(array &$array)
{
return $stuff
}
OR
function fun($array)
{
return $stuff
}
From what i read in the documentation the & passes the reference to the variable. but i thought that declaring the type array was unnecessary.
So, when i pass reference to a variable in a function is declaring its variable type optional?
Meaning is function fun(array &$array) AND function fun(&$array) equivalent/allowed?
To answer the question, Its optional because not all values are allowed. You can specify non scalar values (arrays, objects etc) but not ints strings floats etc
Your last line they are the same, with the exception that a catchable fatal error is thrown if its called like this
fun("1")
fun(false)
the way without the type hint would allow the function to be run and the functions logic would have to test that its argument is correct
Simply you declare your funcion as "no referenced parameters", without "&". In the moment you call the function pass the variable as reference.
function fun(&$array)
{
//function body
}
fun($girlfriend_array);
You can use settype: http://php.net//manual/es/function.settype.php for ensure a variable is type you want.
edited: change "&" location.
This is just out of curiosity, but I was wondering if there was a way to query all the variables defined inside the scope of a function (exclusively within this scope, and from within that function) and put them in an associative array. Something like an extended get_defined_vars function.
The reason is that it would be nice to be able to save the 'state' of an execution at any point in the program, for instance to debug, log, handle exceptions, or even pass the entire scope of a function to another one. If I'm not mistaken, I think get_object_vars allows doing this with objects.
From the comments of PHP.net
// The very top of your php script
$vars = get_defined_vars();
// Now do your stuff
$foo = 'foo';
$bar = 'bar';
// Get all the variables defined in current scope
$vars = array_diff(get_defined_vars(),$vars);
echo '<pre>';
print_r($vars);
echo '</pre>';
PHP doesn't really have scopes, they were added later, that's why you need $this and global to access a global variable. Objects are implemented basically as arrays because they came later, that's why equality tests on two objects work member by member.
You can try var_dump of course.
Ignore the var_dump bit, you've found get_defined_vars and get_object_vars they are as close as you will get I'm sorry to say.
Another difficulty in telling you about what's in a function is how it does variables, remember PHP is a dynamic language if we have:
$x = "hello";
$$x = "world"
we now have a variable called $hello with value "world", if the $x variable came from some complicated nondeterministic code, it'd be impossible to tell without running. So you can only inspect the current variables, when you're in it, and given how scopes are implemented, get_defined_vars is as close as you will get sorry.
Is it possible to pass a variable to function without passing the variables that come before it in the function definition?
For example, if my function looks like this:
function doThis( $v1, $v2, $v3 )
{
echo $v1.$v2.$v3;
}
Can I then call the function with only some of those variables, like doThis("v1",null,"v3") or some other way?
you can use predefined arguments:
function doThis( $v1, $v2 = "hello", $v3 = 1 ) {}
call to this function only with the first argument, the 2nd and 3rd will be "hello",1 by defualt:
doThis(10);
+1 on #Alon post
You can also use an array ..
$array['var1'] = null;
$array['var2'] = "foo";
$array['var3'] = "bar";
doThis($array);
function doThis($theArray){
var_dump($theArray);
}
This way, you can use empty values and N amout of variables.
Yes, you can do doThis("v1",null,"v3"), if you prepare your function to handle that null argument.
For example your example will work just fine.
Or, if it makes sense you can change the order of the arguments so you can set defaults.
You can specify default values in the function definition like so:
function doThis($a, $b=null, $c=null) {
echo $a . $b . $c;
}
doThis("hello", " world"); // yields: hello world
In this function, $a is a required parameter. The other two values are optional.
To appease the commenter below, you can of course still call this function like so:
doThis("hello", null, "world");
and of course, you do not have to provide defaults (but it is a good idea to do so)
The answer is: rework your workflow.
I know you don't like that answer, but honestly, if you are passing more than 3 parameters or if sometimes you don't want to pass part of the beginning parameters, you probably want to rethink what you're doing. Function and method calls should be simple. You are probably doing too much in your function.
General tips:
Use classes if appropriate. This way you can avoid passing some information around because it's stored in the class and your methods have access to it.
Keep methods cohesive. That means your methods and functions should do one thing and do it well.
Put variables you want to pass only sometimes at the back. This allows you to simply not pass them if they have $var=null after it. You then check to see if $var===null before doing something with it.
Much cleaner way is passing an array of variables, as demonstrated by #jflaflamme.
Another way is allowing an arbitrary number of variables like this:
function foo()
{
$arguments = func_get_args();
if(sizeof($arguments))
{
echo implode('', $arguments);
}
}
foo(1, 2, 3, $some_var);
You can also loop trough the array you obtain via func_get_args, check them for their type (if it's a null, array, object - you name it) and then handle them in a way you deem appropriate.
When I reading the code of CodeIgniter,I found some functions written as follows:
function &get_instance()
{
global $CI, $OBJ;
if (is_object($CI))
{
return $CI;
}
return $OBJ->load;
}
I can understand variable refrence,but I can hardly get this through.Is it necessary to use this function style?And any benefit?
Thanks.
In PHP's syntax, this means that the function returns a reference instead of a value. For example:
<?php
$foo = 'foo';
function & get_foo_ref ()
{
global $foo;
return $foo;
}
// Get the reference to variable $foo stored into $bar
$bar = & get_foo_ref();
$bar = 'bar';
echo $foo; // Outputs 'bar', since $bar references to $foo.
?>
In the above example, removing the & from the function decleration would make the $foo variable still contain 'foo', since only the value, not the reference was returned from the function.
This was used more often in PHP4, because it did not pass objects by their reference and cloned them instead. Because of this, object variables had to be passed by reference to avoid unwanted cloning. This is no longer the case in PHP5 and references should not be used for this purpose.
However, functions that return references are not completely useless either (or bad practice, when not used for replacing object references). For example, personally I've used them when creating a script that passes a "path" to an function, which returns reference to variable in that path allowing me to set value to it and read the value. Due to recursive nature of the function, returning the reference was needed.
It is the same concept as variable reference, in fact this "style" is required when you want to return a REFERENCE to a variable and not the value itself.
Explained in the manual here: http://www.php.net/manual/en/functions.returning-values.php
no, there's no benefit. It was necessary for php4, but in php5 it's a bad practice.