Short echo Tag PHP Variables and Methods

php echo variable in html
php short tags
php echo html
php tag
php tags in html
php tags
php echo not working
php echo variable in string

I've been using the PHP short echo code but realised that it doesn't print out the return value from a method. That is, the following will work

<?= $my_variable ?>
<?php echo $my_object->get_value(); ?>

But this won't return anything

<?= $my_object->get_value() ?>

Why does calling a method that returns a value not print to the screen with the short hand?

In normal cases, it should work.

See and test:

    class A {
        public function a() {
            return 1;

    $a = new A;


    <?= $a->a() ?>

output: 1

So, I think your problem come with the get_value() method, it seems that the method does not return a printable value.

echo - Manual, echo is not actually a function (it is a language construct), so you are not also has a shortcut syntax, where you can immediately follow the opening tag with an equals sign. Prior to PHP 5.4.0, this short syntax only works with the short_open_tag print are that echo accepts an argument list and doesn't have a return value. Tip: The echo() function is slightly faster than print(). Tip: The echo() function also has a shortcut syntax. Prior to PHP 5.4.0, this syntax only works with the short_open_tag configuration setting enabled.

Looks like not well tested code. You may run this code (may be in sandbox)

class MyClass {
    public function getValue() {
        return 'Hello';

$my_object = new MyClass();

<?= $my_object->getValue() ?>

and see that it outputs "Hello"

Escaping from HTML - Manual, echo 'this code is within short tags, but will only work '. Code within these tags <​%= $variable; %> is a shortcut for this code <% echo $variable; %> Using this method, you can have HTML tags with optional attributes, depending on some  (which is discouraged since it is only available if enabled using the short_open_tag php.ini configuration file directive, or if PHP was configured with the --enable-short-tags option). If a file contains only PHP code, it is preferable to omit the PHP closing tag at the end of the file. This prevents accidental whitespace or new lines being added after the PHP closing tag, which may cause unwanted effects because PHP will start output buffering when there is no intention from the programmer

The <?= is shortcode for <?php echo so it should in the same way. Please show use the code of the get_value() method. Maybe there it does return empty string or null :)

PHP tags - Manual, When PHP parses a file, it looks for opening and closing tags, which are <?php PHP includes a short echo tag <? Same with this method of commenting ? The issues associated with the short open tag were much greater than the benefits from the short echo tag, so you'll find a million and a half recommendations to turn short_open_tag off, which you should. With PHP 5.4, however the short echo tag has been re-enabled separate from the short_open_tag option.

Is it bad practice to use <?= tag in PHP?, With PHP 5.4, however the short echo tag has been re-enabled separate from use it for final output of variables and avoid any function-calls or ternary-logic  Variable names should be as descriptive as possible, but also as short as possible. All variables should start with a lowercase letter, and should be written in camelBack in case of multiple words. Variables referencing objects should in some way associate to the class the variable is an object of. Example:

PHP Output using Echo, Print and Shorthand Statements, PHP has echo and print functions for output and a special shorthand syntax for the echo The echo statement can be used to output text, variables, HTML markup, This PHP code is a special one in PHP, as it uses a different opening tag. about creating my own template, i just wanted to do that echo way {string} not creating a php framework from scratch, and i swear god i saw a php script (not framework) using that way to echo {string} and also working in html files! thanks for your information.

PHP echo() Function, Example. Write the value of the string variable ($str) to the output, including HTML tags: <?php $str = "Hello world!"; echo $str; echo "<br>What a nice day!"; ?​>. No one is arguing that tags should be used in application source files. However PHP's embeddable syntax allows it to be used as a powerful template language, and templates should be as simple and readable as possible. Many have found it easier to use a much slower, add-on templating engine like Smarty,

  • This does actually work. Share your get_value() function with us. Edit: There is no difference between echo and <?=.
  • @evayly you're right, not sure what I did wrong before but it's working now :) It's one of those days when I've spent too long looking at the screen!
  • Thanks for the help, I had a syntax error it's working as expected now :)
  • :D It's nice! So, there is no real problem here!
  • Thanks for the help, I had a syntax error it's working as expected now :)
  • Thanks for the help, I had a syntax error it's working as expected now :)