views:

37

answers:

2

I have the following inside a function something():

if ($cod == 1000)
{
  $message = 'Some Message';
  return $message;
}

Later, I call this function:

try
{
   $comandoController->someThing();
}

I was expecting to see "Some Message" on the browser. But I don't.

Note: If I echo something like echo "hello" inside the conditional, I can see it. So the condition is the case.

Instead of $comandoController->someThing(); should we do the following:

$result = $comandoController->someThing(); 
echo $result; 
+2  A: 

Works as designed. This

try
{
   $comandoController->someThing();
}

will not output anything to the browser. The return value can be echoed:

echo  $comandoController->someThing();

or stored:

$value =  $comandoController->someThing();

but as it stands, no browser output will take place.

Pekka
Thanks a lot Pekka. :) I have count your post as useful. I will, however, accept Sarfraz answer, because it happens to be the answer were I was doing comments and I believe the comments have important clarifications. Hope you don't mind.
MEM
@Mem no problem
Pekka
+1  A: 

You need to echo that:

echo $comandoController->someThing();

Or use the echo inside your function instead:

if ($cod == 1000)
{
  echo 'Some Message';
}

Now you simply need to do:

$comandoController->someThing();
Sarfraz
If we echo $comandoController->someThing(); will he echo AND execute?
MEM
@MEM: Yes. You are echoing the result of the method call.
Felix Kling
Nice. I thought either it will echo or it will execute. So having $result = $comandoController->someThing(); and then echo $result; is useless. yes?
MEM
@MEM: It isn't that useless because it serves the purpose BUT it is verbose. When shorter way is possible, you should always go for that :)
Sarfraz
@Sarfraz is it better to have the echo inside the function? I prefer to just store the message on that function (that is on a Controller) and let the view to decide, when should that message be displayed. Does this makes sense?
MEM
@MEM: Yes it does make sense, you can go that way too :)
Sarfraz
Yupi! :) Thanks a lot to both of you. I realise this is a very basic question, but, it's basilar. Thanks for taking time for replying. Regards.
MEM
@MEM: Welcome...
Sarfraz
@Felix Kling and all: So, first he executes the method, and later it does the echo of the returned result of that method. It happens to be on the same line, but the order is like this, yes? (how can I talk about Controller and View, and have those questions? Don't ask!) :p
MEM
@MEM: Yes, it is just how code is evaluated and executed. Consider a function call like `a(b(), c())`. The function `a` takes two parameters. In this example, they will be the return values of `b` and `c`. So in order to execute function `a`, functions `b` and `c` have to be executed first. Same for `echo`.
Felix Kling
@Felix Kling: 5 stars. Thanks a lot.
MEM