echo or print, what really is the best option?

11

I know of the differences of echo or print, but in a real project in PHP which one is the most suitable to use? Or just so much?

    
asked by anonymous 09.02.2014 / 15:37

5 answers

7

One is practically nickname for the other.

print also exists in C, a language that PHP relied heavily on, so some early PHP books or courses, or even teachers more accustomed to C, could use print . But in general as other non-beginner PHP programmers use echo , then it tends to be interesting to use echo out of college .

Although print displays examples as print("Alô Mundo") , it is not necessary for them. I think the only useful difference that print could have with echo is that it returns 1 , but if you want to let other people maintain your code, you have no real need to explore this.

See echo documentation in php.net and documentation in print php.net .

    
09.02.2014 / 15:41
8

They are almost identical.

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

$printou = print('foo');

The echo allows you to concatenate variables / strings:

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

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

<?=$minhaVariavel?>
    
09.02.2014 / 15:43
8

Depends on the need, both echo and print are language constructs the difference among them is print always returns 1 (true) echo does not return anything.

print

echo print('ola mundo');

The output will be ola mundo1 . The print can still be used with ternaries while echo does not.

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

$p = ($idade <10) ? echo ('menor') : echo ('maior'); //invalido,
Parse error: syntax error, unexpected T_ECHO 
    
09.02.2014 / 15:46
3

The best option is echo because it will save one byte in the file size of your source code, whenever preferred in place of print (because 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.

    
09.02.2014 / 16:13
2

It's best to use echo .

Mainly, because echo accepts multiple parameters; already print not.

That, in the case, would generate a considerable difference in the time of printing the statement 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

Regarding print, the only benefit I see in using it instead of echo would be to 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 this is a very small difference!

    
05.01.2015 / 21:26