Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Short echo Tag PHP Variables and Methods

Tags:

php

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?

like image 287
Andrew Schultz Avatar asked Mar 11 '26 16:03

Andrew Schultz


1 Answers

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.

like image 64
vietanhyt Avatar answered Mar 13 '26 12:03

vietanhyt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!