Accessing a query variable inside a function [duplicate] - php

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'];
}

Related

PHP set empty field in database to value [duplicate]

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()

How to assign a variable outside php function [duplicate]

This question already has answers here:
Are PHP Variables passed by value or by reference?
(16 answers)
How to declare a global variable in php?
(10 answers)
Closed 3 years ago.
I would like to be able to assign the name of a variable outside the function so that the function can assign the chosen variable. It does not seem to work. Anyone have any ideas?
Below is my attempt, however the $admin variable is empty after running the function.
function assign_check($variable, $check) {
if (empty($_POST[$check])) {
$variable = "no";
} else {
$variable = $_POST[$check];
}
}
assign_check('$admin', 'admin');
My question is not about the use of global variables.
You can request a reference in the function body.
function assign_check(&$variable, $check) {
$variable = 'hello';
}
And call passing a variable (reference).
assign_check($admin, 'admin');
$admin value is now 'hello';
Fitting that to your code would result in
function assign_check(&$variable, $check) {
$variable = empty($_POST[$check]) ? "no" : $_POST[$check];
}
assign_check($admin', 'admin');
But returning a proper value would be much cleaner code than using references. Using a ternary operator like presented above would it even simplify without need of a function at all.
A normal way to assign the result of a function to a variable name specified outside the function would be to have the function return the result and assign it directly to the variable when you call the function.
function assign_check($check) {
if (empty($_POST[$check])) {
return "no";
} else {
return $_POST[$check];
}
}
$admin = assign_check('admin');
I would do it this way unless there was a compelling reason to do it otherwise.
For the specific type of thing it looks like this function is intended to do, I would suggest looking at filter_input.

How to use the variable inside some function? [duplicate]

This question already has answers here:
How to pass a variable inside a function?
(1 answer)
How to access array element inside another array [duplicate]
(1 answer)
Closed 5 years ago.
I have an API's Function :
$transaction=$tran[4];
function coinpayments_api_call($cmd, $req = array(),$transaction) {
curl_init($transaction);
}
$transaction variable not passing into function coinpayments_api_call.
function not taking values from out side.
I also make $transaction varible GLOBAL ,but still same problem ,
Please Help
Make your code like this
$transaction=$tran[4];
function coinpayments_api_call($transaction,$cmd, $req = array(),$txnid) {
curl_init($transaction);
}

Why to keep the parameter Null or FALSE? [duplicate]

This question already has answers here:
Using Default Arguments in a Function
(16 answers)
Closed 5 years ago.
when and where I should keep the Parameters Null or FALSE?
for stance here is a function... and what this function will do when i called it or how can i call it?
<?php
function Show_result($id=Null, $name=FALSE){
//whatever i want code goes here
}
?>
In this declaration you set default values so you can call this function without passing parameters.
This could be useful if you don't want to pass the same parameters most of the times but still be able to pass specific ones in other cases.
For example, a function like this :
function log($message, $tag = null) {
if ($tag == null)
$tag = "info";
echo $tag . ": " . $message;
}
This function can be called two ways:
log("text")
or
log("text", "error")
The first call will print
info: text
The second will print
error: text

How to use a PHP function to write to an array [duplicate]

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;
}

Categories