Can't pass the variable to subject in email laravel 5 - laravel-5

Mail::send('emails.cont',['name'=>$n,'email'=>$email],function($message){
$message->to('abc#gmail.com','efe')->from('cde#gmail.com')->subject($s);
});
There for the subject, I am trying to pass a variable called $s which contains a value which defined there. But, it will underlying in red and saying undefined variable called $s. How to solve that?

can you please try the following code
Mail::send('emails.cont',['name'=>$n,'email'=>$email],function($message) use ( $s ) {
$message->to('abc#gmail.com','efe')->from('cde#gmail.com')->subject($s);
});

Related

dd works but echo does not for an object passed as a parameter to a function in Laravel

I passed an object in a function in laravel.
I tried to assign the value of a property to a variable. It pops an error.
When I tried to figure out what was going on I tried to dd ()the value it worked.
But when I tried to echo the same it does not.
What am I mising.
On using this,
location1 and location2 are two objects of the location class.
The function is here:
function geodistance($location1,$location2){
dd($location1->lat);
$lat1=$location1->lat;
}
it prints
"28.612072"
But when I change the same function to
function geodistance($location1,$location2){
echo($location1->lat);
$lat1=$location1->lat;
}
The output error is:
Trying to get property 'lat' of non-object
Even the function
function geodistance($location1,$location2){
echo $location1->lat;
$lat1=$location1->lat;
}
gives the same output
The aim to remind you is to assign the value to a variable like so.
function geodistance($location1,$location2){
$lat1=$location1->lat;
}
When I echo $location1 from within the function geodistance() gives out and error which makes sense.
function geodistance($location1,$location2){
echo $location1;
$lat1=$location1->lat;
Object of class App\MyClasses\city could not be converted to string
When I dd ($location1 from the funciton like so it gives the right result.
function geodistance($location1,$location2){
dd($location1);
$lat1=$location1->lat;
like so:
city {#13410 ▼
+id: 2245
+info: "{}"
+name: "New Delhi"
+lat: "28.612072"
+lon: "77.22978"
+timezone: null
+weightedrating: null
+country_id: 1
}
It seems i am missing out something very trivial. :(
As pointed out by #salman zafar and #apokryfos, the mistake was that I was passing an array.
When I was doing a dd, after processing the first data the programme was terminated.
The array that was passed was not completely correct. The last data point in my array was not an object of the same class.
So the data ended up being corrupted when using echo. Echo would print one data after another but eventually it would get a wrong input.
The best way to catch these errors (after so much experimenting) is to write the values of the variable in a text file on each iterartion.
What that does is that it catches the error in your text file and u get to know after how many iterations the programme stopped.
Hope this helps others from making such obviously glaring mistakes which are tough to catch (atleast for me since this was the first of such errors)

codeigniter's link does not work and cannot match with the function parameter

I have the function
index($errorMsg, $successMsg) {....}
It works when I type in the URL.
http://localhost/website/index.php/home/index/1234/5678
But It does not work But when I type in the URL.
http://localhost/website/index.php/home/index//5678
5678 will be $errorMsg.
Is there any hints
Really bad solution for passing success or error parameters via function arguments by get method in CI.
Try use session flash data to pass success or error messages in redirection view.
$this->session->set_flashdata('errorMsg', '1234');
$this->session->set_flashdata('successMsg', '5678');
And show variables:
function index()
{
echo $this->session->flashdata('errorMsg');
echo $this->session->flashdata('successMsg');
}
Use this solution to avoid errors.
Your solution
Declare function like this
index($errorMsg, $successMsg=NULL) {....}
Explanation
index($errorMsg, $successMsg) function required both arguments(variables). If you don't pass it will produce error which is happening in your case.
index($errorMsg, $successMsg=NULL) function required first one and 2nd one is optional.If you don't pass 2nd argument $successMsg value will be null.
Note
/home/index//5678 no need use double slash after index.One will solve your purpose.You need to just check $successMsg.If it is null means you passed only $errorMsg

Magento registry variables in controller

I have stored a variable in register by Mage::register('captcha', $var); in helper. And in the controller i tried to retrieve the variable by using Mage::registry('captcha'); But i dont getting any values here. Please help me to solve this.
In your helper file create a function like below :
public function getCaptcha(){
$var = 'myValue123';
Mage::register('varun', $var);
return Mage::registry('varun');
}
In your controller function:
$registryValue = Mage::helper('yourModule')->getCaptcha();
echo registryValue ; //prints myValue123
Hope it helps !!!
It's look like syntax is right.
Please first try to set some static value like $var="test"
Mage::register('captcha', $var);
after that got this value in controller.
Mage::registry('captcha');
if you got this value test then i think you have problem with $var in your helper.
Let me know if you have any problem
'captcha' is already in use, so magento never set your data in registry. Change the name, for example 'captcha1'
Mage::register('captcha1', $var);

No Error for Undefined Variable Used in Class Methods in PHP

I spent more than 10 hours to find out the typo for debugging my PHP program. I expected PHP would produce an error when using an undefined variable. But when it is used as an object in a method, it doesn't. Is there a reason for it?
<?php
$oClass = new MyClass;
// $oCless->tihs('key', 'taht'); //<-- triggers the error: Notice: Undefined variable
$oClass->tihs('key', 'taht');
echo $oClass->arr['key'];
class MyClass {
public $arr = array('key' => 'foo');
function tihs($key, $value) {
$tihs->arr[$key] = $value; //<-- this does not cause an error message.
}
}
?>
Normally if the error reporting level is set to E_ALL | E_STRICT (or E_ALL as of PHP 5.4.0) it should spit out an E_STRICT error. For instance, this code:
error_reporting(E_ALL | E_STRICT);
$tihs->adrr = 453;
Produces:
Strict Standards: Creating default object from empty value in [...]
Interestingly enough, if you specifically create an array instead of an ordinary variable as a property of an object that doesn't exist, e.g.:
error_reporting(E_ALL | E_STRICT);
$tihs->adrr[25] = 453;
No strict standards error is shown! It looks like this could potentially be something PHP folks might want to fix, because I'm not aware this is documented nor I think there's a legitimate reason for this behaviour.
For the record, in both cases regardless of the error a new stdClass is being created on the fly instead, like sberry mentions in his answer.
It is because of PHP trickery...
Under the covers, PHP is actually creating an object called tihs, adding an array to the object called arr and setting key to value.
Here is a print_r($tihs); after the assignment:
stdClass Object
(
[arr] => Array
(
[key] => taht
)
)
You seemed to have misspelt $oClass as $oCless
Agreed, $oCless instead of $oClass would give you an undefined variable error.
Also, "this" is a keyword in most languages and may be in php as well. You should refrain from using it so that it doesn't come out in other languages as a habit. You'll get way more errors if you're using "this" as function and variable names. You wouldn't even get things to compile.

Code Igniter Passing Variable to Controller Using URL Error

I am creatting a Code Igniter project in which I want to pass a variable through the URL like a get statement, like this:
url: /site/cake/1
controller function: cake($var)
but when the variable is left blank, I receive an error, how can I get code igniter, to ignore this?
In your controller, do this:
function cake($var = null) {
// your other code here
}
When $var isn't present in the URL, it will be set to null and you'll receive no error.
To explain why Colin's answer works:
The issue you had, was that there was no default value for that controller function. In php, creating a default value for a function parameter is done by assigning it a value in the function definition ($var = false). Now when the cake() function is called with no parameter, it will set $var to false by default.

Resources