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)
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 3 years ago.
i was trying to send an email and when i want to whrite the email address of the destination (the email is a parameter of the function), i have this error. Can someone help me.
this is my function:
public function email($name, $email2) {
$data = array('name'=> $name);
Mail::send('mail',$data,function($message) {
$message->to($email2, $name)->subject('Deposit Confirmation');
$message->attach(public_path('document.pdf'));
$message->from('xxx#xxx.pt', 'XXX');
});
}
this is my error: "Undefined variable email2"
thank you
When you use anonymous function to pass variables you should use use construction
public function email($name, $email2) {
$data = array('name'=> $name);
Mail::send('mail',$data,function($message) use ($name, $email2) {
$message->to($email2, $name)->subject('Deposit Confirmation');
$message->attach(public_path('document.pdf'));
$message->from('xxx#xxx.pt', 'XXX');
});
}
Here you can find more examples about anonymous functions https://www.php.net/manual/en/functions.anonymous.php
Here simple example from official documentation
$message = "hello";
// Inherit $message
$example = function () use ($message) {
var_dump($message);
};
$example();
// Output: string(5) "hello"
Related
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 4 years ago.
I am having an error Message: Undefined variable: posts. This is what I received after I sent an email using Codeigniter email.
I need to send an html output through CI email.
Controller:
function email() {
$this->load->library('email');
$config = array (
'mailtype' => 'html',
'wordwrap'=> TRUE,
'charset' => 'utf-8',
'priority' => '1'
);
$this->email->initialize($config);
$this->email->from('mywebsite.com', 'My website');
$this->email->to('sender#gmail.com');
$this->email->subject('Test');
$data = $this->data['posts'] = $this->paypal->getRows();
$message = $this->load->view('Receipt', '$data', true);
$this->email->message($message);
$this->email->send();
}
View Receipt.php (this is the view I want to send in an email)
foreach ($posts as $row) {
$row->room_type;
}
See how to pass data from controller to view
$data['posts'] = $this->paypal->getRows();
$message = $this->load->view('Receipt', $data, true);
No need '' in view when you assign array to view.
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 5 years ago.
I just can't figure out why this error is even showing up:
Notice: Undefined variable: authTime in /.../classname.class.php on line 33
class ClassName {
private $authTime = null;
const API_URL = '...';
const CLIENT_ID = '...';
const CLIENT_SECRET = '...';
private static $TOKEN = NULL;
public function __construct() {
$this->_authTime = $authTime; // <----- Line 33
if(!self::$TOKEN OR $this->AuthTime('GET') > 3600 OR !$this->_authTime) {
self::authorise();
}
}
public function getAuthTime() {
return $this->_authTime; // Returns NULL
}
I see $authTime is not defined within the constructor. I think you want to do:
$this->_authTime = $this->authTime;
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 6 years ago.
This is my function:
public function showDataall($result)
{
$q = $this->conn->prepare($result) or die("failed!");
$q->execute();
while ($r = $q->fetch(PDO::FETCH_ASSOC))
{
$data[] = $r;
}
return $data;
}
This function perfectly work in old xampp but new xampp return a Notice:
Undefined variable: data in /opt/lampp/htdocs/live/demo/model/config.php on line 152
Declare variable before using it :
If your query returns no data, your current code will never actually create the $data array, and therefore when you try and return it, this error will happen.
public function showDataall($result)
{
$q = $this->conn->prepare($result) or die("failed!");
$q->execute();
$data = array();
while ($r = $q->fetch(PDO::FETCH_ASSOC))
{
$data[] = $r;
}
return $data;
}
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 7 years ago.
I'm struggling to find the problem with the following piece of PHP code:
The code is as follows
class WorkerThreads extends Thread
{
private $from_list;
public function __construct($x,$host,$users_email,$pass,$inbox)
{
$this->from_list = array(); # holds the unique froms extracted from headers
}
public function run()
{
# Get Froms
if (preg_match('/From\:\ (.+)/i', $headers, $matches, PREG_OFFSET_CAPTURE)) {
$from = trim(str_ireplace("From: ", "", $matches[0][0]));
if (!array_key_exists($from, $this->from_list)) {
$this->from_list[$from] = 1;
echo "<br/>FROM: ".$from."-".$this->from_list[$from]."<br/><br/>";
}
} else {
echo "NO FROM <br/><rb/>";
}
The following error occurs:
Notice: Undefined index: Viva in /var/www/BAMCode/yahoofroms.php on line 198
FROM: Viva-
The offending line 200 is
echo "<br/>FROM: ".$from."-".$this->from_list[$from]."<br/><br/>";
There seems to be a problem with the array
This will work perfectly fine if you don't use multiple threads.
I think many threads are trying to modify and read the same list. Try synchronizing the modification/read of the from_list. We may use a mutex to synchronize read/write to the from_list.
Check the code below:
<?php
class WorkerThreads extends Thread
{
private $from_list;
private $mylock;
public function __construct($x,$host,$users_email,$pass,$inbox)
{
$this->from_list = array(); # holds the unique froms extracted from headers
$this->mylock = Mutex::create();
}
public function __destruct()
{
Mutex::destroy($this->mylock);
$this->mylock = null;
}
public function run()
{
# Get Froms
if (preg_match('/From\:\ (.+)/i', $headers, $matches, PREG_OFFSET_CAPTURE)) {
$from = trim(str_ireplace("From: ", "", $matches[0][0]));
Mutex::lock($this->mylock);
if (!array_key_exists($from, $this->from_list)) {
$this->from_list[$from] = 1;
echo "<br/>FROM: ".$from."-".$this->from_list[$from]."<br/><br/>";
}
Mutex::unlock($this->mylock);
} else {
echo "NO FROM <br/><rb/>";
}
}
}
?>
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 7 years ago.
public static function view($name, array $vars = null){
if(preg_match('/\\\\/', $name)){
$view_data = explode('\\', $name);
if(count($view_data) == 3)
$file = APP_PATH.DS.'views'.DS.$view_data[0].DS.$view_data[1].DS.'view.'.$view_data[2].'.php';
else
$file = APP_PATH.DS.'views'.DS.$view_data[0].DS.'view.'.$view_data[1].'.php';
}
else{
$file = APP_PATH.DS.'views'.DS.'view.'.$name.'.php';
}
if(!is_readable($file)){
throw new Exception('view file application'.DS.'views'.DS.$view_data[0].DS.'view.'.$view_data[1].'.php not found.');
}
else{
if(isset($vars)){
extract($vars);
}
require($file);
}
}
[21-Apr-2015 13:10:30 UTC] PHP Notice: Undefined variable: view_data in /home/realitycards/public_html/test/system/load.class.php on line 28
your variable $view_data only gets defined in the first if statement. It looks like in the if statement below that you are using $view_data even though it hasn't been set.
if(!is_readable($file)){
throw new Exception('view file application'.DS.'views'.DS.$view_data[0].DS.'view.'.$view_data[1].'.php not found.');
}
You either need to set $view_data in your else statement, or in the exception above, use the $file variable that you've already set:
if(!is_readable($file)){
throw new Exception('view file '. $file .' not found.');
}