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?
Related
How do I convert another create_function. The one below
return create_function('$f,$e=null', "return ($parsed_tpl);");
to
$e=null;
return function($f,$e) { return ($parsed_tpl); };
or
return function($f,$e=null;) { return ($parsed_tpl); };
But neither of them are working.
I have tried everything above.
The key part to notice on the original code is this:
"return ($parsed_tpl);"
Note the double-quotes, meaning the variable will be expanded in the string before passing it to create_function. Then the expanded form will form the body of the function.
In other words, the actual code of the created function will be completely different every time - it won't just return the string, it will execute it as PHP code.
To achieve the same effect in current PHP, you need to use the eval function:
return eval("return ($parsed_tpl);");
You also have two mistakes in your attempt:
$e=null isn't an assignment, it's a default value for the parameter; it doesn't need a semicolon
You have to "capture" the $parsed_tpl variable with the use keyword to use it inside the function
The format is this:
$my_anonymous_function =
function($some_param, $next_param='default value', $another='another default')
use ($something_captured, $something_else) {
/* body of function using those 5 variables*/
};
I tried substr() method in PHP 5.6 to get some part of String,
first,
<?php
echo substr("Avicienna", 0,3);
and save it to a file.
second one,
<?php
class Test{
public function index(){
$name = "Hasan";
var_dump(subtr($name,0,3));
}
}
$test = new Test();
$test->index();
and save it to another file.
the first one without class, return correct string parts, while, the second one return PHP 500 error :
Call to undefined method Coba::subtr() in /var/www/html/koper/coba.php on line 5
is there any limitation to call substr() or others php function inside a class ?
Typo error subtr() . Its substr().
<?php
function dosomething(){
echo "do\n";
}
$temp="test".dosomething();
echo $temp;
?>
expected result:testdo
but actual result is:
do
test%
I know how to change the code to get the expected result.
But what i doubt is why the code prints result like this.
Can someone explain it?Thanks!
dosomething is echoing to the screen. Since this runs first "do\n" is printed.
dosomething also doesn't return anything so the second echo is equivalent to echo "test";
In order to use the result of the call you should return it:
function dosomething(){
return "do\n";
}
Which will behave as you expect.
To clarify. In order to work out what $temp is the function must be run first which prints out "do\n" first.
Use return.
function dosomething(){
return "do\n"; }
Use a return statement instead of echo
function dosomething(){
return "do\n";
}
I'm not sure why people are getting down voted. Their answers are right.
http://codepad.org/nagGXY99
Try this:
Use return in the calling function. Doing that you will get the string where the function is being called. So function is being replaced by string and 2 strings will then con cat.
-
Thanks
i got some trouble to understand scope in OOP. What i want is that $foo->test_item() prints "teststring"...Now it just fails with:
Warning: Missing argument 1 for testing::test_item()
Thanks a lot!
<?php
class testing {
public $vari = "teststring";
function test_item($vari){ //$this->vari doesn't work either
print $vari;
}
}
$foo = new testing();
$foo->test_item();
?>
test_item() should be:
function test_item() {
print $this->vari;
}
There is no need to pass $vari as a parameter.
Well, you've declared a method which expects an argument, which is missing. You should do:
$foo->test_item("Something");
As for the $this->, that goes inside of the class methods.
function test_item(){
print $this->vari;
}
function parameters can not be as "$this->var",
change your class like
class testing {
public $vari = "teststring";
function test_item(){ //$this->vari doesn't work either
print $this->vari;
}
}
$foo = new testing();
$foo->test_item();
And read this Object-Oriented PHP for Beginners
What's happening there is that $foo->test_item() is expecting something passed as an argument, so for example
$foo->test_item("Hello");
Would be correct in this case. This would print Hello
But, you may be wondering why it doesn't print teststring. This is because by calling
print $vari;
you are only printing the variable that has been passed to $foo->test_item()
However, if instead you do
function test_item(){ //notice I've removed the argument passed to test_item here...
print $this->vari;
}
You will instead be printing the value of the class property $vari. Use $this->... to call functions or variables within the scope of the class. If you try it without $this-> then PHP will look for that variable within the function's local scope
I'm using a template engine that inserts code in my site where I want it.
I wrote a function to test for something which is quite easy:
myfunction() { return '($this->data["a"]["b"] ? true : false)'; }
The problem is, $this->data is private, and I can't access it everywhere, so I have to use getData(); which causes my problem.
$this->getData()['a']['b']
does not work, and assigning the value first doesn't either because it will be used directly in an if() block.
Any ideas?
Since PHP 5.4 it's possible to do exactly that:
getSomeArray()[2]
Reference: https://secure.php.net/manual/en/language.types.array.php#example-62
On PHP 5.3 or earlier, you'll need to use a temporary variable.
You cannot use something like this :
$this->getData()['a']['b']
ie, array-access syntax is not possible directly on a function-call.
Youy have to use some temporary variable, like this :
$tmp = $this->getData();
$tmp['a']['b'] // use $tmp, now
In your case, this probably means using something like this :
function myfunction() {
$tmp = $this->getData();
return ($tmp['a']['b'] ? true : false);
}
You have to :
first, call your getData() method, and store its return value in a temporary varibale
then, use that temporary variable for your test
You don't have much choice about that, actually...
Ok... apparently there really isn't a better way, so I'm going to answer myself with a not so beautiful solution:
I created the function:
arrayGet($array, $index) { return $array[$index]; }
And used it like this:
myfunction() { return '(arrayGet(arrayGet($this, "a"), "b") ? true : false)' }
This is not pretty but works.
$this->data is always accessible, if it is protected. $object->data is not accessible from everywhere, so if you're returning $this in your code, and it is evaluated as such, it should be ok.
Btw, there is a bug in your code: The quotes need to be escaped.
myfunction() { return '($this->data[\'a\'][\'b\'] ? true : false)'; }
It is possible from PHP version 5.4.
If you don't want a temporary variable for that and your PHP version is less, than 5.4, than you still can use a few built in functions to get the first or the last element:
$x = 'first?last';
$first = array_shift(explode('?', $x));
$last = end(explode('?', $x));
$last2 = array_pop(explode('?', $x));
Edit:
!!! Please note, that in later versions( 5.4+ ) PHP will throw a notice, because end only expects variables as parameter.