I apologize in advance if this is a needless question.
I've looked around for syntax on php echo and require functions, and multiple pages have different answers. However, each version of the syntax works.
What I want to know is if any of these versions are obsolete, or if one performs slightly better than the other.
<?php echo($variable); ?>
<?php echo $variable ?>
<?php require('file.php'); ?>
<?php require 'file.php'; ?>
I see them with parentheses, without parentheses, with single quotation marks, without.. etc.
Is there a definitive syntax for each one?
Thank you.
From the PHP docs on echo
echois not actually a function (it is a language construct), so you are not required to use parentheses with it. echo (unlike some other language constructs) does not behave like a function, so it cannot always be used in the context of a function. Additionally, if you want to pass more than one parameter to echo, the parameters must not be enclosed within parentheses.
Both echo and require are language constructs, the same applies to require, include, require_once and include_path.
The parentheses do make a difference. All you need to do is read the fine manual
echo
Additionally, if you want to pass more than one parameter to echo, the parameters must not be enclosed within parentheses
include (applies to require as well)
Because include is a special language construct, parentheses are not needed around its argument. Take care when comparing return value.
See Example #4 for how it can make a difference.
I would recommend not using parentheses with either construct due to the limitations they impose (echo) and the effect on return values (include)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With