This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 7 years ago.
I am trying to write a PHP function to write error messages to an array. Not sure what I'm doing wrong, still trying to get to grips with functions.
I can make it work without functions, so I guess its the way I'm writing the function that is wrong.
function writeerrors($arr_key, $arr_val){
$errors[$arr_key] = $arr_val;
return;
}
Then I call it here when I check if the form field is empty. If it is empty I want it to write to the $errors array.
//check if empty
if(empty($fname)){
//write to error array
writeerrors('fname', 'Empty field - error');
//Flag
$errors_detected = true;
}else {
Do something else ..}
This is the form... (ONLY TRYING TO VALIDATE FIRST NAME FIELD FOR NOW):
http://titan.dcs.bbk.ac.uk/~mgreen21/p1_prac/PHP_BBK/P1/hoe9/index.php
You just need to specify the global $errors variable, which you have created outside of your function.
function writeerrors($arr_key, $arr_val){
global $errors;
$errors[$arr_key] = $arr_val;
return;
}
Related
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 3 years ago.
My goal is set value if field is empty.
But get "Trying to get property of non-object" error.
last_day is empty field, how to assign value 1 ?
public function getPointsForExercises(){
$ls = Auth::user()->lessons()->get();
$last_day = Auth::user()->user_settings->last_day;
if (empty($user->user_settings->last_lesson)) {
$user->user_settings->last_lesson = 1;
}
First problem: $user variable is not defined.
What you are trying to achieve can be done using the exists function to make sure the relation exists, and then update the value using the update function, like this:
public function getPointsForExercises() {
$ls = Auth::user()->lessons()->get();
$last_day = Auth::user()->user_settings->last_day;
if (Auth::user()->user_settings()->exists() && empty(Auth::user()->user_settings->last_lesson)) {
Auth::user()->user_settings()->update([
'last_lesson' => 1
]);
}
The code above is not ending the function nor is it using the variables $ls and $last_day as your code.
You did not set the $user object.
firstly you have to make object :
$user = Auth::user()
This question already has answers here:
Passing an optional parameter in PHP Function [duplicate]
(6 answers)
Closed 7 years ago.
Okay - I was able to the pass a string assigned to the variable $myfile into the function. If it could be better, please provide feedback- learning PHP.
<?php
$myfile = $row_rs_recordview['headstone'];
echo "$myfile"; // verify the file name
function readGPSinfoEXIF($myfile)
{
global $myfile;
$exif= exif_read_data("headstone/".$myfile, 0, true); //
if(!$exif || $exif['GPS']['GPSLatitude'] == '') //Determines if the
//geolocation data exists in the EXIF data
{
return false; //no GPS Data found
echo "No GPS DATA in EXIF METADATA";
}
?>
Insights appreciated!
Thanks
you can't access the global variables inside function directly.
//add global before variable declaration like this
function fun(){
global $row_rsUpdate['headstone'];
}
This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 8 years ago.
I'm creating an 11 page website all within one PHP file. On this specific part, I'm trying to take usernames from $_POST['regUser'] and put into a "logins" array defined as "$logins = array();" .
When I var_dump the array, all I get is "NULL". I'm certain I'm using the right post names and such. I realize that this code might not be near enough to diagnose my problem but I really hope it is.
Remember, this is all just a fraction of a larger program if it seems weird. This specific piece is supposed to log the user in and show the "userHome" page if they supply the correct password and add their username to the $logins array. Everything other than that is working fine. (I call the function in another part of the program)
I'm going to post a few lines of my code because I believe the problem lies somewhere within them (the whole thing is probably 400+ lines).
$regUser = $_POST['regUser'];
$regPass = $_POST['regPass'];
$goodUsername = array('user1', 'user2');
$goodUserPass = array('user1', 'user2');
$logins = array();
if (isset($_POST['userLog'])) {
if (in_array($regUser, $goodUsername)) {
$key = array_search($regUser, $goodUsername);
}
if($goodUserPass[$key] == $regPass) {
array_push($logins, $regUser);
echo userHome(); return;
}
else {
echo invalidLogin(); return;
}
}
function adLogins() {
echo phpHeader();
echo "<center><h1>User Logins</h1></center><br><br>";
var_dump($logins);
echo phpFooter();
}
your $logins var is outside of the scope of the adLogins function
var_dump($logins);
outside of the ad logins function or pass it into the function
function adLogins($logins){}
This question already has answers here:
PHP take string and check if that string exists as a variable
(3 answers)
Closed 8 years ago.
I have a field that depends has custom text if a certain condition is met...and if it isn't the field is blank.
I have written a custom function test if a variable is set
function AmISet($fieldName) {
if (isset($fieldName)) {
echo "I am set";
}else{
echo "I am not set";
}
};
but when I attach it the field I get an error that the variable is undefined. But when I do a regular isset($fieldName);
I don't have a problem. Is there any way to get around this and have my function do this instead of the isset()?
I want to add other logic in the function but I only want it to work if the variable is set...but I don't want the undefined error if it is not.
I am new to php and really appreciate any help or direction you can give me.
Thank you for the help!
You need to pass the variable by reference:
function AmISet(&$fieldName) {
if (isset($fieldName)) {
echo "I am set\n";
} else {
echo "I am not set\n";
}
}
Test cases:
$fieldName = 'foo';
AmISet($fieldName); // I am set
unset($fieldName);
AmISet($fieldName); // I am not set
However, this function is not useful as it is, because it will only output a string. You can create a function that accepts a variable and return if it exists (from this post):
function issetor(&$var, $default = false) {
return isset($var) ? $var : $default;
}
Now it can be used like so:
echo issetor($fieldName); // If $fieldName exists, it will be printed
the $fieldName comes from a query that is performed when a checkbox
is checked. If the box is checked the query is made and the variable
is set from the query, if not it doesn't exist and the field is blank.
Filter functions are designed for this kind of tasks:
<input type="foo" value="1">
$foo = filter_input(INPUT_POST, 'foo')==='1';
(There's also a specific FILTER_VALIDATE_BOOLEAN filter you can pass as second argument.)
And now you have a pure PHP boolean that always exists.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Can't use method return value in write context
I sometimes encountered this error Can't use method return value in write context. However, I don't know what does WRITE CONTEXT means in this sentence. Could someone tell me a little general explaination of it.
Thank you.
The code for this error if you want to refer to is on line if(empty($this->loadUser())) However just to clarify, I just want to find out the meaning of "write context":
public function verify()
{
if(empty($this->loadUser()))
$this->addError('username','Incorrect username.');
else
{
$user = $this->loadUser();
$project = $this->loadProject($pid);
$project->associateUserToProject($this->loadUser());
$project->associateUserToRole($this->role, $this->user->id)
}
}
public function loadUser() {
return User::model()->findByAttributes(array('username'=>$this->username));
}
empty () is not a function really.
It is a construct or macros, if you please.
It means you cannot pass an object to it as argument.
Just pure variables.
$is_use = $this->loadUser();
if (empty ($is_use))
{
...
}
empty only takes variables as an arg. empty() only checks variables as anything else will result in a parse error.
http://php.net/manual/en/function.empty.php