PHP 类和方法中的 return 与 echo:代码解析
在该代码中,函数what_color()中的return\ $this->color;语句会返回\$this->color的值,而echo\ $this->color;语句则只是将\$this->color的值输出到屏幕上。\n\n在调用what_color()方法时,echo\ $a->what_color();语句会输出\$this->color的值,因为\$a->what_color()返回了\$this->color的值,所以可以通过echo语句将其输出。\n\n而echo\ $this->color;语句在return\ $this->color;之后,所以它不会被执行到。当函数遇到return语句时,它会立即结束执行并返回指定的值。所以在这种情况下,echo语句不会被执行。
原文地址: https://www.cveoy.top/t/topic/qhoH 著作权归作者所有。请勿转载和采集!