I have a function in php which looks like this:
function blabla(){
// some code
if(/*function dothis() calls me*/){
// do this
}
return something;
}
And I have two functions who call blabla(). When the first function calls blabla(), I want that the code outside the if-statement executes. And when the function dothis() calls blabla(), then the code inside the if-statement should be called. How do I build my if-statement?
Pass a boolean as an argument to blabla. In dothis you can call blabla(true) and in the other function you can call blabla(false). Check the argument in the if statement and then the code inside will only be executed when blabla is called from dothis.
Hey you can try like this:-
function blabla($val = 0){
// some code
if($val){
// do this
}
return something;
}
now if i call blabla function from two ways:-
blabla(0); - In this case if statement will not execute.
blabla(1); - In this case if statement will execute.
Related
I tried to google this and i think it can't be done but i have to ask anyway.
If i have while loop?
while ($smth > 1)
{
func();
}
Is there a way i could exit the while loop immediately with that function inside it?
Maybe that function returns something that stop loop?
I tried
func(){
return break;}
but this doesn't work.
It's not possible the way you've done it -- you can't return a break. But you can return a value that could signal to exit the loop.
function func() {
return true
}
And then
while($smth > 1) {
if(func()) break;
}
This will run the function, but it will also check it's return value to verify it does not need to exit the loop. If the function returned true, the if statement is satisfied and so a break will occur. If the function doesn't return explicitly, the if will not be satisfied.
You sort of can get out of that while loop from inside that function by throwing an exception. Your teammates will not like you anymore, however.
This question already has answers here:
How to call a function from a string stored in a variable?
(18 answers)
Closed 1 year ago.
I found question from here. But I need to call function name with argument.
I need to be able to call a function, but the function name is stored in a variable, is this possible? e.g:
function foo ($argument)
{
//code here
}
function bar ($argument)
{
//code here
}
$functionName = "foo";
$functionName($argument);//Call here foo function with argument
// i need to call the function based on what is $functionName
Anyhelp would be appreciate.
Wow one doesn't expect such a question from a user with 4 golds. Your code already works
<?php
function foo ($argument)
{
echo $argument;
}
function bar ($argument)
{
//code here
}
$functionName = "foo";
$argument="Joke";
$functionName($argument); // works already, might as well have tried :)
?>
Output
Joke
Fiddle
Now on to a bit of theory, such functions are called Variable Functions
PHP supports the concept of variable functions. This means that if a variable name has parentheses appended to it, PHP will look for a function with the same name as whatever the variable evaluates to, and will attempt to execute it. Among other things, this can be used to implement callbacks, function tables, and so forth.
If you want to call a function dynamically with argument then you can try like this :
function foo ($argument)
{
//code here
}
call_user_func('foo', "argument"); // php library funtion
Hope it helps you.
you can use the php function call_user_func.
function foo($argument)
{
echo $argument;
}
$functionName = "foo";
$argument = "bar";
call_user_func($functionName, $argument);
if you are in a class, you can use call_user_func_array:
//pass as first parameter an array with the object, in this case the class itself ($this) and the function name
call_user_func_array(array($this, $functionName), array($argument1, $argument2));
I am passing an array to a function and expecting the function to store values in it. Here's my code
The Function -
function GetDetailsById ($iStudentId, $aDetailsId)
{
/* SQL */
while ($row = mysql_fetch_array($result))
{
array_push($aDetailsId, $row[0]);
}
}
Usage -
$aDetailsId = array();
$oDetailsTable->GetDetailsById("1", $aDetailsId)
When I try to do
print_r($aDetailsId)
the array shows nothing. Am I doing it the right way?
Your array needs to be passed by reference to the function ; which means the function should be defined this way :
function GetDetailsById ($iStudentId, & $aDetailsId)
{
// ...
}
For more informations, see Making arguments be passed by reference
Or you could have your function return its result -- which might be better idea (looking at the code that calls the function, you immediately know what it does) :
function GetDetailsById ($iStudentId)
{
$result = array();
// TODO here, fill $result with your data
return $result;
}
And call the function :
$aDetailsId = $oDetailsTable->GetDetailsById("1");
That's because parameters are passed by value by default, meaning only the value of the variable is passed into the function, not the variable itself. Whatever you do to the value inside the function does not affect the original outside the function.
Two options:
return the modified value from the function.
Pass the parameter by reference:
function GetDetailsById ($iStudentId, &$aDetailsId) ...
first count/check your resutl is contain any resultset. and try using '&' in parameter of array
function GetDetailsById ($iStudentId, &$aDetailsId)
Please change function declaration to,
function GetDetailsById ($iStudentId, &$aDetailsId)
There is one more mistake in array_push call. Change it to,
array_push($aDetailsId, $row[0]);
I am new to PHP. Well the text i am referring to say quotes
Functions can be defined anywhere within your program.
the above statement holds fine for code block 1 but not for code block 2. KINDLY EXPLAIN?
CODE Block 1:
<?php
test();
function test()
{
echo "Hello Inside the function";
}
?>
CODE Block 2:
<?php
$no=1;
switch ($no)
{
case "1":
test();
function test()
{
echo "Hello test";
}
}
?>
In theory, yes, functions can be defined "anywhere". In practice, there's a trick to it. The trick is as follows: when PHP reads and compiles the source of your script, it looks for function definitions, and if function definition is in global context (not inside if, switch, etc.) it will be defined immediately. However, if it is inside such construct, or inside another function, etc. it will be defined only when control passes the line on which function() statement resides.
Thus, code block 1 works - because the function is in global context, so PHP will define it before any code is run. But in code block 2, the function is in the context of switch, so it will be defined only when control passes line 7. But since you try to call it on line 6, it is not defined yet! So PHP errors out.
The advice here is never define your functions inside conditionals etc. unless you mean it to be conditional definitions - and then take care not to call them before they are defined.
You can declare function in switch statement, but it's not so good. Your have error because you call function and just then declare it. At first you should declare function and then use it.
<?php
$no=1;
switch ($no)
{
case "1":
function test()
{
echo "Hello test";
}
test();
}
?>
You cannot declare a function in a switch statement.
However what you can do is the following:
<?php
$no=1;
switch ($no)
{
case "1":
test();
break;
}
function test()
{
echo "Hello test";
}
?>
Just remove the function from the switch.
The function only gets executed when called so it doesn't matter.
EDIT
What propably is meant by that quote (Functions can be defined anywhere within your program.) is:
You can declare functions before or even after you call them in your script.
A couple of problems. you need to use
case 1:
for switches, otherwise it will be looking for a string equivalent to "1". "1" != 1 (the first is a string, the second an integer)
While your text did say functions could be defined anywhere, they didn't actually mean anywhere. You cannot define a function inside of a block of code, so you'l have to define the function outside of the switch:
<?php
$no = 1;
switch ($no) {
case 1:
test();
break;
}
function test()
{
echo "I'm inside the test function!";
}
?>
Otherwise things just get crazy.
can I use a function that is outside the current function?
eg.
function one($test){
return 1;
}
function two($id){
one($id);
}
Seems like i cant, how should I do it then to use the function that are outside? Thanks
The function is in the same file.. /
Is your function inside of a class? In that case you have to use $this->function() instead of function().
That's perfectly valid. Check out the running code here.
Your code looks valid to me : you are declaring two functions, called one and two ; and two is calling one.
Then, you can call any of those functions, to execute it.
For example, if you execute the following portion of code :
function one($test){
var_dump(__FUNCTION__);
return 1;
}
function two($id){
var_dump(__FUNCTION__);
one($id);
}
two('plop');
Note that I called two, in the last line of this example.
You'll get this kind of output :
string 'two' (length=3)
string 'one' (length=3)
Which shows that both functions were executed.
That works fine. However, one ignores its parameter. Then, two ignores the return value from one.
This should work fine
Example:
<?php
function test ($asd)
{
return $asd;
}
function run ()
{
return test('dd');
}
echo run();
?>
Maybe you have an issue elsewhere?