PHP: variable not working inside of function?
Because it's not defined in the function.
There are a few ways to go about this:
1) Use what Alex said by telling the function it is a global variable:
echo $path; // working
function createList($retval) {
global $path;
echo $path; // working
}
2) Define it as a constant:
define(PATH, "/my/test/path"); // You can put this in an include file as well.
echo PATH; // working
function createList($retval) {
echo PATH; // working
}
3) Pass it into the function if it's specific to that function:
echo $path; // working
function createList($retval, $path) {
echo $path; // working
}
Based on how the function really works, one of those will do ya.
PHP global variables not working inside a function
Looks like homework, still:
<?php
$bool = 1;
boo();
function boo() {
global $bool;
if ($bool == 1) {
$bool = 2;
echo 'Hello World';
}
}
?>
Or
<?php
$bool = 1;
boo(&$bool);
function boo(&$bool) {
if ($bool == 1) {
$bool = 2;
echo 'Hello World';
}
}
?>
PHP variable not working when referenced with '$'
Omit the leading $ from $project_key
in the following line:
to_js('$project_key');
It should be:
to_js('project_key');
The $
in a variable is not part of the variables name, so you don't need to include it when referencing it in a variable variable.
PHP use variable inside function
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.
Php get function variable outside not working
since the variables '$var','$var2' and '$var3' are local variable for the function variables so it has no existence unless the function is called or executed.
So there are two methods to fix your issue
Method one make the variable global.
<?php
// Declaring the global veriable
$var=0;
$var2=0;
$var3=0;
function variables()
{ // method 2 to declare global
GLOBAL $var = rand(1111,9999);
GLOBAL $var2 = rand(11111,99999);
GLOBAL $var3 = rand(111111,999999);
}
but still, if u want to get the rand
value from the function u need to call the function at least once
// calling the function
variables();
echo variables([$var]);
echo variables([$var2]);
echo variables([$var3]);
?>
hope you got the solution if not just comment I will help u out with this for sure
Why php variable scope not working in function
If variables are defined outside the function, you need to specify the global
keyword. Such as:
<?php
$a='a';$b='b';
function test(){
global $a, $b;
echo $a.$b;
}
test(); // error
?>
But your second example is the recommended way of handling it, typically.
How to fix? PHP variable not shows values inside function
What you are looking for is the "USE" keyword (for anonymous functions, as you are using in your second example). This allows additional variables from the parent scope to be passed into the closure. Its usage can be a little tricky, see the official PHP anonymous function documentation for more information (specifically, see example #3).
$line_second = $imageMeta[2];
$text_line2 = function (TextToImage $handler) use ($line_second) {
$handler->add($line_second)
->position(250, 150)
->font(20, __DIR__ . '/Roboto-Black.ttf')
->color(0, 0, 0);
};
Variable not working inside If statement
First, You can define outside the if condition.
$SubmissionID = "";
$CourseID = "";
if (isset($_POST['modifybtn'])) { //Now we have our variables all set
$SubmissionID = $_POST['modifybtn'];
$CourseID = $_POST['courseid'];
}
//We do some irrelevant stuff here.... and then
echo $CourseID; //Works
if (isset($_POST['savebtn'])) {
echo $CourseID; //Now it will Working
saveData($AppID, $CourseID);
header("Location: applicantCase.php");
}
if (isset($_POST['nextbtn'])) {
saveData($AppID, $CourseID); // Now it will work
header("Location: ApplicantApplyEducation.php");
}
global variable not working inside function
Your variables $country_table
and $cities_table
won't be defined because you didn't bring in the global $wpdb before you tried to use it in those vars. Try this instead:
global $wpdb;
$country_table = $wpdb->prefix . "trackbyid_country";
$cities_table = $wpdb->prefix . "trackbyid_cities";
function database_creation(){
global $country_table, $cities_table;
...
}
Related Topics
How to Add a New Method to a PHP Object on the Fly
What Factors Make PHP Unicode-Incompatible
How Long Can a Tld Possibly Be
Laravel 5.4 Redirection to Custom Url After Login
Merging Two Multidimensional Arrays on Specific Key
How to Pass Data Between Pages in PHP
Simple PHP Contact Form with Firebase Hosting
Warning: MySQLi_Query() Expects Parameter 1 to Be MySQLi, String Given In
How to Handle $_Post Data in MVC
How to Include Authorization Header in Curl Post Http Request in PHP
PHP - Empty $_Post and $_Files - When Uploading Larger Files