Andrew Schultz
Andrew Schultz

Reputation: 4243

Short echo Tag PHP Variables and Methods

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?

Upvotes: 2

Views: 912

Answers (3)

Damian Dziaduch
Damian Dziaduch

Reputation: 2127

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 :)

Upvotes: 2

Sasha Kos
Sasha Kos

Reputation: 2608

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

<?php
class MyClass {
    public function getValue() {
        return 'Hello';
    }
}

$my_object = new MyClass();
?>

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

and see that it outputs "Hello"

Upvotes: 5

vietanhyt
vietanhyt

Reputation: 668

In normal cases, it should work.

See and test:

<?php 
    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.

Upvotes: 5

Related Questions