Variable from required document not found [duplicate] - php

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.
MySQLiconfig.php:
<?php
$MySQLi = new mysqli('localhost','root','123','Database') or die('ERROR');
Other document:
<?php
require 'MySQLiconfig.php';
function DoAQuery($Query){
$MySQLi->query($Query);
}
The error is:
Undefined variable: MySQLi

You are wrong with variable scope. In the function you need to use like global $MySQLi or you can pass as function parameter.

Related

How can I check if a method is defined? [duplicate]

This question already has answers here:
Check if method exists in the same class
(4 answers)
Closed 3 years ago.
I try to set the value of my variable to "", in the case that getLove is not defined:
`$money = $dollar->getCash()->getLove ?? "";`
But I get still the error message:
Attempted to call an undefined method named "getLove" of class
"Proxies__CG__\App\Entity\Happiness".
Using method_exists, note that this does not take "__call()" in consideration.

laravel: cant push object property to array [duplicate]

This question already has answers here:
PHP variables in anonymous functions
(2 answers)
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 stuck in a silly silly sample of code but i can't find solution. I have this code:
Route::get('/example', function(){
$arr=[];
Collection::all()->each(function($collection){
$arr[]= $collection->id;
});
dd($arr);
});
and it returns to me an empty array all the time despite the fact that Collection:all() has objects inside. Can someone help me?
In the each section you define function which make it with new scope for the variable.
Try the use of PHP function as:
Route::get('/example', function(){
$arr=[];
Collection::all()->each(function($collection) use (&$arr){
$arr[]= $collection->id;
});
dd($arr);
});

Echo variable in more files [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 5 years ago.
In first.php i have $name = "Josh". How to echo $name in secound.php. Both are in same folder.
In first.php
$name = "Josh";
In secound.php
echo $name;
Notice: Undefined variable: name in D:\XAMPP\htdocs\Folder\secound.php on line 2
Edit
include 'first.php';
echo $name;
To access the data in the second file, you'll need to include it.
At the top of your secound.php script, add this code:
include 'first.php';
You could also use require, like this:
require 'first.php';
The difference between the two can be found in the PHP manual:
require is identical to include except upon failure it will also
produce a fatal E_COMPILE_ERROR level error. In other words, it will
halt the script whereas include only emits a warning (E_WARNING) which
allows the script to continue.

Ambigous PHP error message [duplicate]

This question already has answers here:
Only variables should be passed by reference
(12 answers)
Closed 7 years ago.
Having this:
...
private $responseBuffer = array();
...
and within this line:
$lm = end(array_values($this->responseBuffer));
I get
Error: Only variables should be passed by reference (2048)
as both end and array_values are built in and do not have a call-by-referenceI'm puzzled, any one an idea?
(purpose: get the latest value out of $responseBuffer)
end function receives the arguement by reference, do like this:
$var = array_values($this->responseBuffer);
$lm = end($var);

How to pass variable in PHP using include? [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 8 years ago.
I have one page where i need to send some results to another PHP file.
include("test.php");
but i want to pass variable (var) also so that which can be processed by test.php using $_GET['var'] command. How is it possible in PHP?
$var1="hello";
include ("test.php");
the test file contains below code
echo $var1." world";

Categories