Let's say I have an example like this one:
$foo = 'Hello ';
$bar = 1;
$abc =& $foo . $bar;
if (true) {
++$bar;
if (true)
{
++$bar;
}
}
echo $abc;
I am expecting $abc to return Hello 3, but it actually returns Hello only. I'm really confused. Is there something I've gotten wrong with references in PHP?
A reference variable is like an alias to the same object/variable, and it can only reference one variable at a time.
I'm not really sure how to help your situation because I don't know what you're trying to do, but..
$foo = 'Hello ';
$bar = 1;
$abc =& $bar;
++$bar;
++$bar;
echo $foo . $abc;
http://www.php.net/manual/en/language.references.whatdo.php
Related
Is there a way I can pass any variable currently in scope by reference to a lambda function without listing them all in the use(...) statement?
Something like
$foo = 12;
$bar = 'hello';
$run = function() use (&) {
$foo = 13;
$bar = 'bye';
}
// execute $run function
Resulting in $foo equal to 13 and $bar equal to 'bye'.
TL;DR The short answer is no. You need to name the variables
You don't need closure variables for this. It's not even valid to use use with named functions since it won't have a nested lexical scope. Use the global keyword to make the variables "dynamic". You have to name all the variables that are special.
$foo = 12;
$bar = 'hello';
function run() {
global $foo,$bar;
$foo = 13;
$bar = 'bye';
}
run();
print "$foo, $bar\n"; // prints "13, bye"
For a lexical anonymous functions you need to name all variables with the use keyword and use & to get it referenced:
$foo = 12;
$bar = 'hello';
$run = function () use (&$foo,&$bar) {
$foo = 13;
$bar = 'bye';
};
call_user_func($run);
print "$foo, $bar\n"; // prints "13, bye"
You could use get_defined_vars() along with extract():
$foo = 12;
$bar = 'hello';
$vars = get_defined_vars();
foreach (array_keys($vars) as $var) {
$vars[$var] = &$$var; // keep refs of all variables from current scope
}
$run = function() use($vars) {
extract($vars, EXTR_REFS);
$foo = 13;
$bar = 'bye';
};
// execute $run function
$run();
print $foo; // prints 13
print $bar; // prints bye
Hey guys am a newbie in PHP. I have seen some code like:
<?php
class foo {
var $bar = 'I am bar.';
var $arr = array('I am A.', 'I am B.', 'I am C.');
var $r = 'some';
}
$foo = new foo();
$arr = 'arr';
echo $foo->$arr[1];
?>
It returns some. Why it is returning some. echo $foo->$arr[1] means it should output I am B. But instead it outputs some; why?
When you access a property of a class, you don't use $ before the property. If you do, it will evaluate that portion first, to figure out what property to access.
echo $foo->$arr[1];
$arr is 'arr', so when you access it as an array, it will grab the letter at whatever index you specify.
$arr[1] is "r";
$foo->r = 'some';
If you access the object property without the $:
echo $foo->arr[1];
it will output I am B.
As a side note, if you DO want to use variable-variables, and it's an array, you should really use parenthesis.
$foo->$arr[1];
is ambiguous as to whether you mean
($foo->$arr)[1];
or
$foo->($arr[1]);
Try,
<?php
class foo {
public $bar = 'I am bar.';
public $arr = array('I am A.', 'I am B.', 'I am C.');
public $r = 'some';
}
$foo = new foo();
echo $foo->arr[1];
?>
To access object variable have to use $foo->var_name;
I am not sure if this is possible, however, can we build a method that returns or echo the result based on calling the method directly or used in assignment
function foo()
{
return "bar";
}
$abc = foo();
// $abc will have the value "bar"
But if foo() is called directly, it should echo "bar"
foo();
// should echo / print "bar"
adding echo before foo() solves the problem, but how could this be achieved without using echo. Probably adding some line of code to the function foo()
There is a possible way is to echo inside your function .
Just like
<?php function foo()
{
echo "bar";
return "bar";
}
echo $abc = foo();
?>
I was wondering if it's possible to change and initialize variables in a function without passing arguments to the function. Here is what I want to achieve:
$foo = 'Lorem';
$array = array();
foobar($foo);
function foobar(){
if (strlen($foo)== 1)
$bar = 'Ipsum';
else
$array[] = 'error';
}
fubar();
function fubar(){
if (empty($fouten))
echo $bar;
}
$foo is a local (uninitialized) variable inside a function. It is different from the global variable $foo ($GLOBALS['foo']).
You have two ways:
$foo;
$bar;
$array = array();
function foobar(){
global $foo, $array, $bar;
if (strlen($foo)== 1)
$bar = 'Ipsum';
else
$array[] = 'error';
}
or by using the $GLOBAL array …
This is not really good practice though and will become a maintenance nightmare with all those side effects
Functions in php can be given arguments that have default values. The code you posted as written will give you notices for undefined variables. Instead, you could write:
function foobar($foo = null) {
if($foo) { // a value was passed in for $foo
}
else { // foo is null, no value provided
}
}
Using this function, neither of the below lines will produce a notice
foobar();
foobar('test');
I'm kind of slow in the head so can someone tell me why this isn't working:
function foo() {
$bar = 'hello world';
return $bar;
}
foo();
echo $bar;
I just want to return a value from a function and do something with it.
Because $bar does not exist outside of that function. Its scope is gone (function returned), so it is deallocated from memory.
You want echo foo();. This is because $bar is returned.
In your example, the $bar at the bottom lives in the same scope as foo(). $bar's value will be NULL (unless you have defined it above somewhere).
Your foo() function is returning, but you're not doing anything with it. Try this:
function foo() {
$bar = 'hello world';
return $bar;
}
echo foo();
// OR....
$bar = foo();
echo $bar;
You aren't storing the value returned by the foo function
Store the return value of the function in a variable
So
foo() should be replaced by
var $t = foo();
echo $t;
As the others have said, you must set a variable equal to foo() to do stuff with what foo() has returned.
i.e. $bar = foo();
You can do it the way you have it up there by having the variable passed by reference, like so:
function squareFoo(&$num) //The & before the variable name means it will be passed by reference, and is only done in the function declaration, rather than in the call.
{
$num *= $num;
}
$bar = 2;
echo $bar; //2
squareFoo($bar);
echo $bar; //4
squareFoo($bar);
echo $bar; //16
Passing by reference causes the function to use the original variable rather than creating a copy of it, and anything you do to the variable in the function will be done to the original variable.
Or, with your original example:
function foo(&$bar)
{
$bar = 'hello world';
}
$bar = 2;
echo $bar; //2
foo($bar);
echo $bar; //hello world