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);
});
Related
This question already has answers here:
Check if a variable is undefined in PHP
(8 answers)
Closed 4 years ago.
What's the best way of checking if an object property in php is undefined?
You can use is_null
Or
!isset($object)
Example :
I want to check if the input is undefined. So, I can show errors.
if (!isset($_POST['myInput'])) { echo "error"; } else { // do the code }
This question already has answers here:
What is the meaning of three dots (...) in PHP?
(9 answers)
Closed 5 years ago.
I'm learning PHP http://php.net/manual/en/migration70.new-features.php and in the following example, I don't understand ... prepended with the $ints parameter in the function definition.
<?php
// Coercive mode
function sumOfInts(int ...$ints)
{
return array_sum($ints);
}
var_dump(sumOfInts(2, '3', 4.1));
Can anybody please tell me what those dots are for?
Thanks.
that means that when you call that function, you can pass X integers and the function will process them, doesn't matter how many are they. If you call sumOfInts(3,4,6,2,9) or sumOfInts(3,2,9), the function works, no matter how many arguments you pass
This question already has answers here:
What is the syntax for accessing PHP object properties? [closed]
(3 answers)
Closed 3 years ago.
I'm lost by retrieving my PHP var. I've tried multiple things without success.
My var is called $answer and here is his var_dump :
object(stdClass)#3631 (1) { ["token"]=> string(159) "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VySWQiOjEsImlhdCI6MTQ2MTMyMDQzNCwic3ViIjoiQ2hhdEF1dGhlbnRpZmljYXRpb24ifQ.18jpKLj_6Banyncyq6bz9jIFSB3IRDpBCvSgpIGJPrs" }
The most logic is to access by $answer["token"] But it's not working.
How can i get my data ?
This doesn't look like an array, but an object of stdClass. Use the Object Operator to access it:
$answer->token;
You can access it like this: $answer->token
You can access by $answer->token; instead of $answer["token"], as it is an object - not an array
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";
This question already has answers here:
Anonymous recursive PHP functions
(6 answers)
Closed 9 years ago.
i need to recursively call a variable function, how can i do this?
$rec = function($li) use($html,$rec) { // error, rec was not defined yet
if( ... ) $rec( ... );
}
how can i do this?
Use the function variable $rec by reference (&$rec) so you can set it to the function then. This will also define it.
use($html, &$rec)
^
You find this principle outlined in the question Anonymous recursive PHP functions.