Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it common to have just a return statement in a php file

Tags:

php

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.

like image 917
fs_tigre Avatar asked May 11 '16 22:05

fs_tigre


People also ask

Can I use return in 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.

Which of the following is true about return statements in PHP?

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.

Does return end a function PHP?

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.

What is return $this in PHP?

$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.


1 Answers

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']);
like image 21
scrubmx Avatar answered Oct 18 '22 19:10

scrubmx