echo or print, what really is the best option?

Asked

Viewed 10,236 times

11

I know the differences of echo or print, but in a real PHP project which one is best suited to use? Or simply whatever?

5 answers

9

It depends on the need, so much echo how much print sane language constructs the difference between them is that print always returns 1(true) already the echo returns nothing.

print

echo print('ola mundo');

The exit will be ola mundo1. The print can still be used with suits while the echo nay.

$p = ($idade <10) ? print ('menor') : print('maior'); //valido

$p = ($idade <10) ? echo ('menor') : echo ('maior'); //invalido,
Parse error: syntax error, unexpected T_ECHO 
  • The advantage I see in that is in expressions like <?php isset($value) and print($value) ?>, that with the echo there’s nothing you can do.

8


One is practically nickname for the other.

The print also exists in C, a language that PHP relied heavily on, so some books or initial PHP courses, or even teachers more accustomed to C, can use print. But in general like other PHP programmers use echo, then it tends to be interesting to use echo outside college.

Although the print display examples such as print("Alô Mundo"), is not necessary for them. I believe the only difference useful that the print could have in relation to the echo is that he returns 1, but if you want to let other people maintain your code, you don’t really need to explore it.

Behold echo documentation in php.net and the print php.net documentation.

  • Thank you for the reply Emerson.

8

Are virtually identical.

The print has the detail of returning a value, 1 in case it has run. So it can be used

$printou = print('foo');

The echo allows concatenating variables/strings:

echo 'Olá!', ' mundo', '!'; // dá "Olá mundo!"
echo 'Olá!'.' mundo'.'!'; // dá "Olá mundo!"

The echo has still the advantage of having a shortcut when writing mixed in html:

<?=$minhaVariavel?>
  • Thanks for commenting Sergio. But does this form of shortcut <?=$var? > have no future risks? Of course as long as the server is configured due to <?php ...

  • 1

    @phpricardo, can have a good image (in English) about it: http://i.stack.Imgur.com/Bgkl8.png , of this post.

  • 1

    But concatenating variables and strings print also does

3

The best option is echo because it will save a byte in the file size of your source code, each time it is preferred instead of print (for echo has 4 characters and print has 5 characters).

The best option will be print only if you need to use the expression in a ternary or receive a return value.

  • 2

    Besides, echo refers us to alien Echo Echo ben 10, while print doesn’t remind us of any alien!

  • 3

    Why is it worth saving bytes in php font?

  • 2

    Why waste bytes in php font?

2

You better use the echo.

Mainly because echo accepts multiple parameters; already the print nay.

Which, in this case, would create a considerable difference in the time of printing the instruction below

echo  'Olá ', 'Meu nome é Wallace',
      ' e Eu tenho ', 30  - 6 , ' anos de idade', PHP_EOL;
      //Olá Meu nome é Wallace e Eu tenho 24 anos de idade

echo  'Olá ' . 'Meu nome é Wallace' .
      ' e Eu tenho ' . 30  -  6 . ' anos de idade'; // -6 anos de idade

As for the print, the only advantage I see in using it instead of the echo would simplify a conditional expression to print a value.

Examples:

//Com `print`:

<?php isset($value) && print($value) ?>

//Com `echo` PHP 5.3 ou anteriores 

<?php echo isset($value) ? $value : null; ?>

//No PHP 5.4+ já podemos fazer isso sem precisar habilitar o short_tags

<?= isset($value) ? $value : null; ?>

But that’s a very small difference!

  • I was going to answer about the commas, but I saw your answer, so +1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.