PHP function is little strange

198

I coded like this.

<?php 
function get_argument($arg){
    return $arg;
}
echo print get_argument(33);
?>

saved as php extension file. And execute it in chrome. I expected that it print out the number '33'

But it tells me '331'

little strange to me. So I change the value 34, instead of 33.

It print out '341'

So I tested it one more time.

Insert two more code like this.

echo get_argument(33);
print get_argument(33);

Both of them print out the same number, which is '33'

What is difference? What causes it a little, but big result?

265

Answer

Solution:

Because print has return, see at PHP documentation: http://php.net/print.

340

Answer

Solution:

Because when you writeecho print get_argument(33); , it'll execute the print first, so the print will write 33.

But, the print will return 1, which is then printed by the echo

58

Answer

Solution:

When you type

echo print get_argument(33);

it first prints the result ofget_argument(33) as 33, and then echoes the result ofprint get_argument(33) as true which is 1.

819

Answer

Solution:

That is because you are using echo and print at the same time, chosse only one.

<?php 
  function get_argument($arg){
   return $arg;
  }
   print get_argument(33);
 ?>
258

Answer

Solution:

in this command echo print get_argument(33) : you are 2 output first is : print get_argument(33)

and second is echo ;

echo will return result of print function; 1 is for result of print function.

523

Answer

Solution:

i am not sure why it happens but try to return integer

function get_argument($arg){
    return intval($arg);
}

People are also looking for solutions to the problem: mysql - Remove certain text from php output

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.