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
Related
My variables are not working, and I'm not sure why.
I did not define $var as static, so when I reference it as global, or as $this->var, it should be the same variable, right?
Except it isn't.
Some people say 'global' shouldn't be used, instead pass parameters to the function. But what if I need to work with 20 variables in an instance's function?
Do I really pass 20 parameters to it?
Doesn't it become unreadable and unclear?
I'm running PHP 7.2.8. on XAMPP, but that isn't really relevant.
<?php
class Test{
public $var;
public function __construct($param)//1
{
global $var; //5
$this->var = $param; //1
$var = $param * 5; //5
}
public function wtf(){
global $var; //5
$foo = $this->var; //1
echo "var: $var <br>";
echo "this var: $foo <br>";
}
}
$foo = new Test(1);
$foo->wtf();
$value = $foo->var;
echo "Value: $value";
?>
Output:
var: 5
this var: 1
Value: 1
I'm expecting the $var to be the same thing in both cases. Why does it become two?
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();
?>
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
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');
function foo()
{ echo 'function called'; }
Is it possible to do something like this:
onchange($a, foo);
$a = "foo"; // echoes 'function called'
$a = "bar"; // echoes 'function called'
Instead of:
$a = "foo";
foo(); // echoes 'function called'
$a = "bar";
foo(); // echoes 'function called'
Sort of. You can use what's called a setter, a function designed to set a variable.
function setFoo ($value)
{
echo 'in setFoo';
return $value;
}
$a = setFoo('foo'); // echoes 'in setFoo'
echo $a; // echoes 'foo'
$a = setFoo('bar'); // echoes 'in setFoo'
echo $a; // echoes 'bar'
In short: no. You shouldn't use global variables anyway, what means, that foo() should not know anything about $a, except you give it as a parameter