php concatenation operator use when operand is a function - php

<?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

Related

Barebones add_filter example doesn't work... what's wrong here?

I've never used my own Wordpress filters before, so I was trying to understand how it's done.
I created a simple example that seems like it should work, but it doesn't. I expect it to echo 'filtered', but it echoes 'unfiltered'. What am I doing wrong here?
function test() {
$stuff = 'unfiltered';
apply_filters('test',$stuff);
echo $stuff;
}
add_filter('test',function() {
return 'filtered';
});
test();
Ahhhhhh. Silly mistake. I needed to assign apply_filter to a variable before echoing it.

Why we use return in the end of function while not returning any value

I am confused about return statement , why we need to use return in end of function , for example
function test($a){blah;
blahh ;
blahhh;
blahhhhh;
return;}
What the use of return here? Function automatically terminates when all the statements executed , I think there is no use of return here , but this picture from http://www.w3resource.com/php/statement/return.php make me confused
So
Can someone please explain the use of return (when we not returning any value).
It depends on what you're trying to achieve.
If you write echo in several places, your code will get confusing. In general, a function that returns a value is also more versatile, since the caller can decide whether to further manipulate that value or immediately print it.
I'd recommend to stick to the convention and use return for a function.
You should check GuardClause.
Example:
function test() {
return 10;
}
$a = test(); // $a stores the value 10
echo $a; // Prints 10
echo $a + 5; // We may want to manipulate the value returned by the function. So, it prints 15.
For further reference, check What is the difference between PHP echo and PHP return in plain English?
In that context: You don't.
return breaks out of the function, but since it is the last statement in that function, you would break out of it anyway without the statement.
return passes its argument back to the caller, but since it doesn't have an argument, there is nothing to pass.
So it does nothing.
Don't assume that every piece of code you stumble across has a purpuse. It might be left over from an earlier version of the code where something else (which gave it meaning) has been removed. It might be written by someone cargo culting. It might be placeholder for future development.
"return" is important when you plan to call this function from other codes, it helps you to:
Know if the function works correctly, or not.
Obtain values from a function.
Make sure other codes are not executed after return.
It might be useless when the code is simple as your sample, let's make it more complex.
function test($a){
if(file_exists($a)){
if(is_file($a)){
return $a." IS A FILE\n";
} else if(is_dir($a)) {
return $a." IS A DIR\n";
} else {
return $a." EXISTS, BUT I DONT KNOW WHAT IT IS\n"
}
} else {
return $a." NOT EXISTS\n";
}
return 0;
}
$filecheck = test("/abc/def.txt");
if($filecheck){
echo $filecheck;
} else {
echo "unknown error";
}
Above shows how to return a value, and do some basic handling.
It is always good to implement return in your functions so you can even specify error code for your functions for reference.
Based on your example, I'll modify slightly like below:
function test($a){
blah;
blahh;
blahhh;
blahhhhh;
return 1;
}
So I know the code is running to last line.
Otherwise the function just finishes silently.
It's useful if you want a function to return a value.
i.e.
Function FavouritePie($who) {
switch($who) {
case 'John':
return 'apple';
case 'Peter':
return 'Rhubarb';
}
}
So, considering the following:
$WhatPie = FavouritePie('John');
echo $WhatPie;
would give
apple
In a really simple form, it's useful if you want a function to return something, i.e. process it and pass something back. Without a return, you'd just be performing a function with a dead end.
Some further reading:
http://php.net/manual/en/function.return.php
http://php.net/manual/en/functions.returning-values.php
To add some further context specific to the answer, if the question boils down to "Do I need to add a return for the sake of it, at the end of a function, then the answer is no. But that doesn't mean the correct answer is always to leave out your return.
it depends what you want to do with $a.
If you echo $a within the function, that function will spit out $a as soon as it is called. If you return $a, assuming you set the calling of test to a variable (i.e. $something = $test('foo')), then you can use it later on.

get return value from a function php using oops

I have code below, where i need to get the return value in a variable outside a class and also its print with respective code.
http://codepad.org/mAlhYBll
and below is raw code.
<?php
class test {
public function kk() {
echo "Whats up :";
return "Hello";
}
}
$obj = new test();
$obj->kk();
$abc = $obj->kk();
?>
Now how can i get value returned from a function added an image below
You need to echo $abc. The program is printing something else since you're echoing What's up within the method, remove that.
Exactly as you've done - although you call "kk()" twice, which is not nneeded, so drop line 13.

PHP: Use function outside current function

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?

Tell How Function was called

I've looked at debug_backtrace but so far it doesn't do what I need it to do.
I need to know whether the function I'm calling was 'called' or 'echo-ed'. Like this:
function hello() {
//blah blah
}
echo hello(); //echo-ed
hello(); //'called'
But the function would do different things if it was 'called' over 'echo-ed'.
How would I do that?
I am pretty sure that this is impossible. The reason this cannot work is that "echo" or any other operator, function or variable assignment uses the return value of the function you've called. So if you've got the following:
echo function1();
What happens is that function1 gets executed, and the return value is passed to echo. Therefor, function1 cannot possibly know that its return value is going to be "echo-ed", because by the time that happens, function1() has already been called and finished executing.
There is no efficient way to deal it
Update:
There is no way to deal it :)
two examples to help you understand.
function hello(){
return "Hello!";
}
echo hello(); // prints Hello!
function hello(){
echo "Hello!";
}
hello(); // prints Hello!

Categories