This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Reference - What does this symbol mean in PHP?
Can anyone explain this experssion
&variablename in PHP.
I have seen this around at many places but i am not able to figure out what does this statement do.
Thanks in advance
J
PHP reference. References in PHP are a means to access the same variable content by different names. There are three operations performed using references: assigning by reference, passing by reference, and returning by reference.
PHP reference
for example:
$example1 = 'something';
$example2 =& $example1;
echo("example 1: $example1 | example 2: $example2\n"); //example 1: something | example 2: something
$example1 = 'nothing'; //change example 1 to nothing
echo("example 1: $example1 | example 2: $example2"); //example 1: nothing | example 2: nothing
You can pass variable to function by reference, so that function could modify its arguments. The syntax is as follows:
<?php
function foo(&$var)
{
$var++;
}
$a=5;
foo($a);
// $a is 6 here
?>
taken from http://www.phpbuilder.com/manual/language.references.pass.php
Related
This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 7 years ago.
I want to know about meaning of $$val; what is the actual meaning is?i tried to find meaning of this in google but not understand properly. Please help me in this situations.
For example: suppose i have one variable which has $$value;
meaning of $$value?
You didn't put the language, but I'll assume you mean PHP
That's a variable variable.
That means you ware asking for the value of the variable whose name.is the first variable.
Here's an example, since that's quite confusing:
$foo = "Hi";
$bar = "world";
$world = "Hello!";
echo $$bar; // "Hello!"
php fiddle: http://ideone.com/Ve4YOO
Reference: https://secure.php.net/manual/en/language.variables.variable.php
This question already has answers here:
Does function definition order matter?
(7 answers)
Closed 1 year ago.
The following code runs in PHP
<?php
$foo = "Chocolate milkshake";
go($foo);
function go($param) {
echo $param;
}
?>
// Output: chocolate milkshake
See this Demo http://codepad.viper-7.com/ToApZa
This code runs without errors and prints specified output, why?
I thought this "function hoisting" only occurred in JavaScript
It doesn't matter where you declare your functions in PHP in most cases, as you've just proved :)
Take a look at this page for more details. The key point:
Functions need not be defined before they are referenced, except when a function is conditionally defined as shown in the two examples below.
This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 8 years ago.
Can you please explain to me the differences between two functions:
function &a(){
return something;
}
and
function b(){
return something;
}
Thanks!
The first returns a reference to something, the second a copy of something.
In first case, when the caller modify the returned value, something will be modified as a global variable do.
In the second case, modifying a copy as no effect to the source.
An ampersand before a function name means the function will return a reference to a variable instead of the value.
According to this LINK
Returning by reference is useful when you want to use a function to find to which
variable a reference should be bound. Do not use return-by-reference to increase
performance. The engine will automatically optimize this on its own. Only return
references when you have a valid technical reason to do so.
This question already has answers here:
How does the "&" operator work in a PHP function?
(4 answers)
Closed 9 years ago.
I've been looking everywhere for a satisfactory answer to this. Still not wrapping my head around the usefulness of the & character when used as a reference. Why would I want to use it? I learn best by example.
Here is an example taken from php.net with slight modification:
<?php
function foo(&$var)
{
$showVar = $var++;
echo $showVar;
}
$a=5;
foo($a);
?>
How is the above different from:
<?php
function foo($var) // & was removed here.
{
$showVar = $var++;
echo $showVar;
}
$a=5;
foo($a);
?>
I got the same exact result (the value of 5) when printing $var++, but according to the documentation there, it should be 6.
What is the advantage?
In any case, I'd appreciate a very lucid and even dumbed down explanation of that the benefits of using & is when referencing something.
Best to explain with code:
function foo(&$var)
{
$var++;
}
$a=5;
foo($a);
echo $a; // 6
In this example above the value of $a can changed insed the function. Meaning if you pass by reference the function has access to a variable in the calling context. This is different to:
function foo($var)
{
$var++;
}
$a=5;
foo($a);
echo $a; // 5
Where the param passed to the function is just a copy of the value of $a
The difference is that if you don't pass it by reference, the change only happens locally within the function.
In your first scenario, if you use echo $a after calling the function, you'll see the changes made to that variable within the function. In the second scenario, you wont.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Parse error on explode('-','foo-bar')[0] (for instance)
In PHP there are functions that return an array, for example:
$a = parse_url("https://stackoverflow.com/q/9461027/87015");
echo $a["host"]; // stackoverflow.com
My question is how to combine the above two statements into a single statement. This (is something that works in JavaScript but) does not work:
echo parse_url("https://stackoverflow.com/q/9461027/87015")["host"];
Note: function array dereferencing is available since PHP 5.4; the above code works as-is.
You can do a little trick instead, write a simple function
function readArr($array, $index) {
return $array[$index];
}
Then use it like this
echo readArr(parse_url("http://stackoverflow.com/questions/9458303/how-can-i-change-the-color-white-from-a-uiimage-to-transparent"),"host");
Honestly, the best way of writing your above code is:
$a = parse_url("http://stackoverflow.com/q/9461027/87015");
echo $a["scheme"];
echo $a["host"];
Isn't that what I originally posted?
Yes. Depending on context you may want a better name than $a (perhaps $url), but that is the best way to write it. Adding a function is not an attractive option because when you revisit your code or when someone reads your code, they have to find the obscure function and figure out why on earth it exists. Leave it in the native language in this case.
Alternate code:
You can, however, combine the echo statements:
$a = parse_url("http://stackoverflow.com/q/9461027/87015");
echo $a['scheme'], $a['host'];