Store 'null' string in .env file Laravel 5 - laravel

I need to store 'null' as a string in the .env file.
Now I have VAR_NAME=null in the file and if I try to read the value with
env('VAR_NAME') it doesn´t return nothing.
Is there any way way to store the value as a string?

You can store a blank string in your .env file by leaving the space after = blank, such as `
VAR_NAME=
But it won't return null, it will return an empty string:
var_dump(env("VAR_NAME");
//string(0) ""
If you want it to return NULL, then use
VAR_NAME=NULL
Now, if you want the string "NULL", you would have to have some custom logic to convert it at runtime, for example:
$test = env("VAR_NAME") ? env("VAR_NAME"):"NULL";
var_dump($test);
// string(4) "NULL"

If you put VAR_NAME=null in .env file and try to print it as {{ env('VAR_NAME') }} it wouldn't show anything. But the value is still null.
You can see it by using {{ var_dump(env('VAR_NAME'))}}
But if you want to return null as a string, I don't think it's possible. You would probably need to specify any other string and consider it as null.
If you just wanted to store it in a global variable, you can save it in the config/app.php file and use it as {{Config::get('app.var_name')}} This would return null.
//config/app.php
'var_name' => 'null',
Now access it via:
config('app.var_name')
or
Config::get('app.var_name')

The anser to my question seems to be, no, you can't.
The solution was to use the second parameter of env('VAR_NAME','default').
The problem is that env() doesn't return the default value because VAR_NAME exists and have a value.
I comented VAR_NAME in the env file just in case I need to use it later.

You can use php's getenv function and pass a default value. So if the value is not existing then it will return the default value for eg:
getenv('VAR_NAME',null)
This will return the value if VAR_NAME is existing in the .env file else it will return you the default value which is null

L8 try
env('VAR_NAME') ?: "null";
Cover cases:
if VAR_NAME not exists in .env you will get 'null'
if VAR_NAME= in .env you will get 'null'

Related

Laravel - Variable check after form submit

is it possible when submitting a form that redirects me with a variable to check if that variable is null? Because when i have if($variable === null) in my code and i dont pass the mentioned variable when loading the page it will result in error that $variable is not defined
you need to define the variable before put it in if condition
not in the if
update:
use this in return : return view('name_page')->with('variable',$variable);
the exception told you that variable doesn't exist on memory so you to check if it exists not in it's value for that you have change your if condition from
if($variable === null) to be if(isset($variable))

how can set or change global variable value at runtime in laravel

I used global Variable and shared it in all views but when I try to change its value, it doesn't work . also I tried use config file but when I change its value its doesn't work .
please help me;
thanks
my config file name is langauge and its content is: <?php return [ 'lng' => 'english', ];
I want to change lng to 'arabic' or 'spanich'
I tied this statement but it doesn't change
config()->set('config.langauge.lng', 'arabic');
To set configuration values at runtime, pass an array to the config helper:
your code should be like:
config(['langauge.lng' => 'arabic']);

change a autoloaded variable in codeigniter framework

I need to change a variable which is saved over the autoloader function.
Inside of a existing class (named "app") I can check if the variable is set.
$this->options[$name];
Now I want to change the value of these autoloaded variable, inside my app.php class in this way
---
echo"old value: ".$this->options[$name].")";
$this->options[$name] = $value;
echo "new value:".$this->options[$name];
return true;
...
for this, i get the correct new value.
The problem is, that it seems that this new value is not updated for the rest of the script!? If i access this variable later, i get the old value!?
What i do wrong?
The only "variables" that persist across different pages/loads/refreshes .etc. are those that are stored in a session variable, cookie or database.
To reiterate; this:
echo"old value: ".$this->options[$name].")";
$this->options[$name] = $value;
echo "new value:".$this->options[$name];
return true;
only affects the current instance at run-time (you can look at this as a page view). It will not persist. Same goes for config value changes in CodeIgniter.
Your only method is using some sort of file/database based storage.

Populate a property according to another one with SpEL in application.properties

Here is the application.properties file:
myVar=${SOME_VAR:#{null}}
result=myVar is #{myVar != null && myVar.length() > 0 ? '' : 'not'} populated
What I am trying to get is if the environment variable SOME_VAR is set (and not blank), the property result should be myVar is populated, otherwise myVar is not populated.
The code I put above doesn't work (the line to set result), and I have also tried different combinations of #{} and ${}, including wrapping myVar, but no success so far.
What is the correct way to do? Thanks.
You wont be able to refer the myVar field directly if your member variable are private. So you should put your condition directly on the property value.
please check below expression as per your requirement.
#Value("myVar is #{ '${SOME_VAR}' != null && '${SOME_VAR}'.trim().length() > 0 ? '' : 'not'} populated")
private String result;
The #{ } is an expression language feature, while ${ } is a simple property placeholder syntax.
I ended up doing
result=myVar is #{'${SOME_VAR:#{null}}' != '#{null}' && '${SOME_VAR:#{null}}'.trim().length() > 0 ? '' : 'not'} populated

Code Igniter Passing Variable to Controller Using URL Error

I am creatting a Code Igniter project in which I want to pass a variable through the URL like a get statement, like this:
url: /site/cake/1
controller function: cake($var)
but when the variable is left blank, I receive an error, how can I get code igniter, to ignore this?
In your controller, do this:
function cake($var = null) {
// your other code here
}
When $var isn't present in the URL, it will be set to null and you'll receive no error.
To explain why Colin's answer works:
The issue you had, was that there was no default value for that controller function. In php, creating a default value for a function parameter is done by assigning it a value in the function definition ($var = false). Now when the cake() function is called with no parameter, it will set $var to false by default.

Resources