Below is the example I put together to understand my confusion. Now my question is, when I make the function into a local variable, does it start to run instantly or does it wait for the local variable to be called.
//Here is the function
function myFunction(){
return 'Hello Stackoverflow';
}
//Does the functio run at this point
$stackoverflow = myFunction();
//Or does the function run here?
echo $stackoverflow;
You aren't making "the function into a local variable."
In your example the function runs, the string return value is assigned to $stackoverflow, then you echo the string.
I think you are trying to do this.
//Here is the function
function myFunction(){
return 'Hello Stackoverflow';
}
//Doesn't run yet
$stackoverflow = 'myFunction';
//This runs now
echo $stackoverflow();
You are not actually assigning a function to the variable, but the return value of the function.
And yes, the function is executed when you call it, i.e. when you assign it to the variable.
After that of course you have a variable with a value and you can do whatever you want with it.
It will run at the time of assignment:
$stackoverflow = myFunction();
You could assign the function to the variable if you want it called when referencing the variable and not when it's assigned:
$foo = function () {
return 'Hello Stackoverflow';
};
echo $foo();
The function run when it is assigned to a value, i.e, at $stackoverflow = myFunction();
Related
this function does not return the result, what could I be doing wrong?
<?php
function iisset($name){
return ${$name}; // "${$name}" does not work
}
$hola = 1;
echo iisset("hello");
Note: it works correctly if it is not in a function
You have a few issues going on here. The first is an issue of scope. Nothing outside the function iisset($name) is normally visible to it. Therefore any variable defined outside iisset($name) can't been seen.
So the first step would be to make it global
by adding global ${$name}; to the first line after the function declaration. The second would be that there is no variable named "hello" outside the function. If you are trying to access the $hola then I would recommend the following:
<?php
function iisset($name){
global ${$name};
return ${$name}; // "${$name}" does not work
}
$hola = 1;
echo iisset("hola");
how to access the variable X in Function Func1 not in global Scope
$X='hi';
function Func1(){
global $X;
echo $X;
}
function Func2(){
$X='hello';
Func1(); // I want to echo "hello" not "hi"
}
(First of all, good job trying to avoid using a global. They are almost never the right answer.)
Variables in PHP functions are locally scoped - they don't inherit anything from where they're called. Func1 has no idea about any variables or anything else that happens in Func2, it only knows about itself.
If you want a variable available in a function, then you need to pass it in as an argument:
function Func1($X){
echo $X;
}
function Func2(){
$X='hello';
Func1($X);
}
It would be worth reading http://php.net/manual/en/language.variables.scope.php to get a basic grounding in scope in PHP.
Give this a try
$x = 'hi';
function func1(){
echo func2();
}
function func2(){
return $x = 'hello';
}
The x variable will get overridden and the function will return the variable data.
Next, the func2 will be called in func1 and the returned value will be printed on the screen.
Just tried to minimize the number of lines.
i have below a function called test thats being called, and just echos "test" keeping it simple, for this question.
test();
function test() {
echo "do something";
}
However i want to try and make the function dynamic if thats the right choice of words.
I have a list of records in a database, and based on each record, i may want a different function to process that record, the idea then is to have a field in the database which would be function_name, i then use that field to some how call that function.
Like this
test();
$function_name = "test";
function $function_name() {
echo "do something here";
}
the only way i can think to get around this is to use switch/case but that is going to cause me other issues down the line.
The function has to be defined with a specific name but you can call it using a variable that contains its name like so :-
<?php
function name() {
echo "name";
}
$func_name = 'name';
// its always a good idea to check that function
// actually exists before calling it.
if (function_exists($func_name)) {
$func_name();
}
Use closures for this:
<?php
$funcName = 'test';
$$funcName = function ()
{
echo 'Do something';
};
$test(); // 'Do something'
$$funcName(); // 'Do something'
Main File;
$opid=$_GET['opid'];
include("etc.php");
etc.php;
function getTierOne() { ... }
I can use $opid variable before or after function but i can't use it in function, it returns undefined.
What should i do to use it with a function in an included file?
$getTierOne = function() use ($opid) {
var_dump($opid);
};
Its because the function only has local scope. It can only see variables defined within the function itself. Any variable defined outside the function can only be imported into the function or used globally.
There are several ways to do this, one of which is the global keyword:
$someVariable = 'someValue';
function getText(){
global $someVariable;
echo $someVariable;
return;
}
getText();
However, I'd advise against this approach. What would happen if you changed $someVariable to another name? You'd have to go to each function you've imported it into and change it as well. Not very dynamic.
The other approach would be this:
$someVariable = 'someValue';
function getText($paramater1){
return $parameter1;
}
echo getText($someVariable);
This is more logical, and organised. Passing the variable as an argument to the function is way better than using the global keyword within each function.
Alternatively, POST, REQUEST, SESSION and COOKIE variables are all superglobals. This means they can be used within functions without having to implicitly import them:
// Assume the value of $_POST['someText'] is someValue
function getText(){
$someText = $_POST['someText'];
return $someText;
}
echo getText(); // Outputs someValue
function getTierOne()
{
global $opid;
//...
}
<?php
class MyClass
{
static function test()
{
echo "Victor";
}
static function result()
{
echo "My name is ".self::test();
}
}
MyClass::result();
?>
I'm confused why self::test() is executed before the rest of the command or the other way around. Thanks in advance for the comments.
Because to get string that needs to be echoed out needs to be "prepared". so before output it needs to know what's the return value of it. it executes first and it's result is included in string. Actually, self::test(); does not return value, but echoes out some text.