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))
Related
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.
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'
I have the function
index($errorMsg, $successMsg) {....}
It works when I type in the URL.
http://localhost/website/index.php/home/index/1234/5678
But It does not work But when I type in the URL.
http://localhost/website/index.php/home/index//5678
5678 will be $errorMsg.
Is there any hints
Really bad solution for passing success or error parameters via function arguments by get method in CI.
Try use session flash data to pass success or error messages in redirection view.
$this->session->set_flashdata('errorMsg', '1234');
$this->session->set_flashdata('successMsg', '5678');
And show variables:
function index()
{
echo $this->session->flashdata('errorMsg');
echo $this->session->flashdata('successMsg');
}
Use this solution to avoid errors.
Your solution
Declare function like this
index($errorMsg, $successMsg=NULL) {....}
Explanation
index($errorMsg, $successMsg) function required both arguments(variables). If you don't pass it will produce error which is happening in your case.
index($errorMsg, $successMsg=NULL) function required first one and 2nd one is optional.If you don't pass 2nd argument $successMsg value will be null.
Note
/home/index//5678 no need use double slash after index.One will solve your purpose.You need to just check $successMsg.If it is null means you passed only $errorMsg
Let's say my controller function is expecting 2 parameters: page_name, and user_name
The URL would be in the format http://mysite.com/controller_name/function_name/page_name/user_name
Assuming that sometimes I can have a blank user_name, and other times I can have blank page_name, can I pass a blank page_name by loading this URL?
http://mysite.com/controller_name/function_name//user_name
If the controller function is:
function function_name($page_name="default", $user_name=null)
...
Would the $page_name value be "default" for the 2nd URL stated above?
You can't. Server will simply ignore the extra slash.
Since both parameters are optional, you should use request parameters.
http://mysite.com/controller_name/function_name?page_name=p1&user_name=u1
And in your controller, use $this->input->get('page_name') and $this->input->get('user_name') to get the value and check if the values are empty.
I know this is an old question but the solution I came up with is simple. It goes something like this:
$this->input->get('limit') ? $this->input->get('limit') : 20;
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.