Is it common to have just a return statement in a php file? If yes, can someone show me how this is used in other files?
<?php
return ['someVariable' => 'someValue'];
A perfect example is the config files in the Laravel framework, for instance the database.php.
The return keyword ends a function and, optionally, uses the result of an expression as the return value of the function. If return is used outside of a function, it stops PHP code in the file from running.
PHP return statement immediately terminates the execution of a function when it is called from within that function. This function is also used to terminate the execution of an eval() function or script file. If this function is called from a global scope, the function stops the execution of the current script.
If called from within a function, the return statement immediately ends execution of the current function, and returns its argument as the value of the function call. return also ends the execution of an eval() statement or script file.
$this means the current object, the one the method is currently being run on. By returning $this a reference to the object the method is working gets sent back to the calling function.
No it isn't very common to have just a return statement, but it is used sometimes to store the configuration information in a separate config.php
file so that the config can be included elsewhere with php require
.
//config.php
<?php
return [
'app_key' => 'SomeRandomString',
'app_secret' => 'SomeRandomString',
];
// other-file.php
<?php
$config = require 'path/to/config.php';
$facebook = new Facebook($config['app_key'], $config['app_secret']);
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