Skip to content

Instantly share code, notes, and snippets.

@MegaBedder
Created October 2, 2018 02:07
Show Gist options
  • Save MegaBedder/24a03c4449142f5dc86997da6366824c to your computer and use it in GitHub Desktop.
Save MegaBedder/24a03c4449142f5dc86997da6366824c to your computer and use it in GitHub Desktop.
<?php
$v1 = TRUE;
$v2 = 123;
$v3 = "qqq{$v2}zzz";
// After converting each of its expression's values to strings,
// 'echo' concatenates them in order given, and writes the resulting string to STDOUT.
// 'echo' it does not produce a result.
// After converting its expression's value to a string,
// 'print' writes the resulting string to STDOUT.
// 'print' can be used in any context allowing an expression.
// 'print' it always returns the value 1.
// concatenation
echo '>>' . $v1 . '|' . $v2 . "<<\n"; // outputs ">>1|123<<"
print '>>' . $v1 . '|' . $v2 . "<<\n"; // outputs ">>1|123<<"
// multiple expression's
echo '>>' , $v1 , '|' , $v2 , "<<\n"; // outputs ">>1|123<<"
print '>>' , $v1 , '|' , $v2 , "<<\n"; // Parse error: syntax error, unexpected ','
// precedence
echo ('>>' . $v1 . '|' . $v2 . "<<\n"); // outputs ">>1|123<<"
print ('>>' . $v1 . '|' . $v2 . "<<\n"); // outputs ">>1|123<<"
// context in variable
$x = echo "$v3\n"; // Parse error: syntax error
$x = print "$v3\n"; // outputs "qqq123zzz"
var_dump($x); // outputs "int(1)"
// context in expression statement
$a = $b = 0;
$a > $b ? echo "..." : echo "..."; // Parse error: syntax error
$a > $b ? print "..." : print "..."; // outputs "..."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment