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 3 years ago.
Could please help me understand what I am doing wrong here.
I am able to get the value of $income, but the issue is in the function ovIncome i get this error $income is undefined.
$income = $_REQUEST['income'];
//Gross Income Overview
function ovIncome() {
//Check if Less Than or More Than
if ($income == '0') {
$wageVal = 'Less than € 30.984,- ';
}
elseif($income == '1') {
$wageVal = 'More than € 30.984,- and same as € 61.200,-';
}
else {
$wageVal = 'More than € 30.984,-';
}
echo "$wageVal";
}
You need to pass variable in your function as a parameter.
$income = $_REQUEST['income'];
function ovIncome($income){//pass variable as parameter
And you will good to go.
Sample example: https://3v4l.org/dGYAj
Note: at the time of calling the function you need to pass that variable too( what I did in my code link in last line)
Related
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 4 years ago.
I have 2 pages
function.php
function getcookie()
{
if (!empty($_COOKIE['remember'])) {
$test="testing function";
}
}
And home.php
include('function.php');
getcookie();
echo"$test";
I get this error - Notice: Undefined variable: test in .... when I call the getcookie function and meanwhile the cookie is set.
When I try it like this, it works.
home.php
if (!empty($_COOKIE['remember'])) {
$test="testing function";
}
echo"$test";
Result - testing functon
if you want to echo from inside a function, you can do it like this as one example. Although there are many ways to do this.
function.php
function getcookie(){
if (!empty($_COOKIE['remember'])) {
echo "testing function"; // Echo this string
}
}
Rest of your code
include('function.php');
getcookie(); // This will output "testing function" from inside your function if your condition is met
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);
}
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 5 years ago.
This is really simple but I can't get my head around it. I am setting a datestamp and would like to be able to use it inside a function like this..
$date_stamp = date("dmy",time());
function myfunction() {
echo $date_stamp;
}
This is not working and $date_stamp is not available inside the function, how can I use this?
This is basic PHP. $date_stamp is out of scope within your function. To be in scope you must pass it as a parameter:
$date_stamp = date("dmy",time());
function myfunction($date) {
echo $date;
}
// call function
myfunction($date_stamp);
See PHP variable scope.
Just as an add-on to John Conde's answer, you can also use a closure like so
<?php
$date_stamp = date("dmy",time());
$myfunction = function() use ($date_stamp) {
echo '$myfunction: date is '. $date_stamp;
};
$myfunction();
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;
}
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'];
}