I am trying to create a Blade directive that will set a class on my table row depending on what gets passed in. The problem is that when I call it, the value of the variable is not being passed in - the literal string that I have between the parenthesis is.
In my view:
#row($inspection->inspection_disposition)
In the directive:
Blade::directive('row', function($data)
{
var_dump($data);
.
.
.
}
What I see in the dump is:
string(37) "($inspection->inspection_disposition)"
Shouldn't I see the value of that variable? That's what I want. What am I missing here?
MORE INFO:
I need to use the value of that variable in the directive, like this:
if($data == "hello")
{
return something
}
elseif($data == "goodbye")
{
return something else
}
This is a simplified example, but hopefully it will help to illustrate that I need to compare the value of the variable inside the directive, then determine what to do. Perhaps I need to use eval() ?
Blade directive should return a string that php will interprete, like this:
Blade::directive('row', function($data)
{
return "<?php var_dump($data); ?>";
}
Here's the secret:
It's not pass by value or pass by reference it's kind of like pass by variable name.
So think of it this way:
When you call:
#row($data)
imagine that you are calling:
$row('$data') /* Don't actually do this */
Because that's how essentially laravel processes it. It's going to take the names of the variables you pass and insert them into the php code that you return to the view.
So like #RDev's answer, place the following (in the boot() function of app\Providers\AppServiceProvider.php):
Blade::directive('row', function($parameter)
{
return "<?php var_dump($parameter); ?>";
}
The $data variable will hold this literal value that you passed it "$data". And it will swap that into your php code like so:
<?php var_dump($data); ?>
And then that php code will get inserted into your view in place of the #row blade directive.
Related
How do I convert another create_function. The one below
return create_function('$f,$e=null', "return ($parsed_tpl);");
to
$e=null;
return function($f,$e) { return ($parsed_tpl); };
or
return function($f,$e=null;) { return ($parsed_tpl); };
But neither of them are working.
I have tried everything above.
The key part to notice on the original code is this:
"return ($parsed_tpl);"
Note the double-quotes, meaning the variable will be expanded in the string before passing it to create_function. Then the expanded form will form the body of the function.
In other words, the actual code of the created function will be completely different every time - it won't just return the string, it will execute it as PHP code.
To achieve the same effect in current PHP, you need to use the eval function:
return eval("return ($parsed_tpl);");
You also have two mistakes in your attempt:
$e=null isn't an assignment, it's a default value for the parameter; it doesn't need a semicolon
You have to "capture" the $parsed_tpl variable with the use keyword to use it inside the function
The format is this:
$my_anonymous_function =
function($some_param, $next_param='default value', $another='another default')
use ($something_captured, $something_else) {
/* body of function using those 5 variables*/
};
I created a function to check for special characters in a string, but I can't seem to get it to echo the response message
Here is my code
<?php
function chk_string($string){
if (preg_match('/[\^£$%&*()}{##~?><>|=_+¬-]/', $string))
{
$chk_str="false";
} else {
$chk_str="true";
}
return $chk_str;
}
$string="this is just a test" ;
chk_string($string) ;
echo $chk_str;
?>
The "echo $chk_str" is not echoing anything.
If you did
$chk_str = chk_string($string);
then you could echo $chk_str;.
The $chk_str you are trying to echo is only visible in your function.
More description:
Your function (chk_string) is in a different scope than your echo.
Your function returns a variable, but that variable is "lost", since you don't assign it to another variable.
Your code currently does something like this line by line:
Your function declaration
$string means "this is just a test"
your functions result as a string, just floating in the code
write out a variable that doesn't exist.
I hope this helped in someway.
You need to store returned value in a particular variable if you want to echo that variable like this,
$chk_str = chk_string($string) ;
echo $chk_str;
Another way you can just directly echo returned value like this,
echo chk_string($string) ;
Your question is about variable scope and it is answered already, but I would recommend you to take a look at variable scope here https://www.php.net/manual/en/language.variables.scope.php.
Basically, every variable has its scope and we can not access a variable outside its scope. In your case, scope of variable $chk_str is inside function chk_string so you can not access it outside of the function. Because you return value of $chk_str in function chk_string so you still can access its value through response of function chk_string, for example:
echo chk_string('a string');
OR
$result = chk_string('a string');
echo $result;
I'm pretty sure this is a very basic question to all of you, but i'm new with php, and i don't really get it...
basically i've created a function in which i need to pass two parameters.
My functions is this:
function displayRoomDetails($customerRooms, $test)
{
foreach ($customerRooms as $room) {
$test.= $room->name;
};
}
it is a very basic function, but will do for this example.
Now, i'm creating templates to display several information, and i have 3 different layout where i need to display the same info but styled differently, so my approach was:
template1 .= '<span>';
if (!$customerRooms == "") {
displayRoomDetails($customerRooms,"template1");
};
template1 .= '</span>';
It should be pretty easy to understand, basically i'm calling the same functions in all the different templates passing as a parameter the template name and trying to append the results to the right template.
The problem i've got is this:
According to this example here ->
http://www.w3schools.com/php/showphp.asp?filename=demo_function3
i should be able to do this exactly like i did, but when i try, when i debug my function, $template doesn't take the passed value as i though it would, but it is still called $test and not $template1...
What am i doing wrong?
Thanks
Try these changes:
function displayRoomDetails($customerRooms, &$test)
And
$template1 .= '<span>';
if ($customerRooms != "") {
displayRoomDetails($customerRooms, $template1);
};
$template1 .= '</span>';
From what I understand you want to append some text to the template1 variable using the displayRoomDetailsFunction
Some things to fix:
template1 should be $template1
You should be passing the $template1 not the "template1" (i.e. the variable itself not its name).
If you want to modify this variable you need to either:
pass it as reference, which you can do by changing the function's declaration to: function displayRoomDetails($customerRooms, &$test)
return new string from function and assign it to the $template1 by adding return $test; just after your foreach block and changing the call to $template1 .= displayRoomDetails($customerRooms,$template1);
Additional note: if $customerRooms is an array, it'd be better to check if it's not empty using count() than !$customerRooms == "", see #andrew's comment for details
Is there any way in PHP to know where a variable was initialized or assigned the value for first time OR where it was last modified?
I think it should be possible to know this because PHP gives some such hint in errors. Like: Can not redeclare abc() (previously declared in /path/to/file.php)
EDIT:
I need this because:
function abc() {
global $page; //this should be int.
if($page == 2) { ... }
}
But when this function is run; I get error Can not convert object into int. This is because some where in my code $page is overriden by any object. I need to find that place where it was modified. Or I'll have to dig through entire code.
You can find out which object is being assigned to your $page variable using the native php function get_class() :
get_class($page);
You would normally want to debug this using a unit test where you would test that the variable $page is numeric.
If you just want to debug on-screen try:
if(is_object($page)){
die(get_class($page));
}
or via an exception:
if(is_object($page)){
throw new Exception('$page must be numeric object given of type : '.get_class($page));
}
This will help you find which object is being assigned to your variable and just point you in the right direction.
If you want to know where a variable was defined then you are out of luck; there is no way to find that out, other than actually looking through your code and finding it.
Your problem seems to be that you are allowing a variable into a function (using global). Because the variable is defined outside the function it can be modified at any time. If you want to always know what the variable contains then you should pass it as an argument instead:
$page = 2;
function abc($page) {
if($page == 2) { ... }
}
$test = abc($page);
If you want to make sure that the value of the variable is a number then you can find that out by simply validating the argument (or the global variable, for that matter):
if (is_int($page) or ctype_digit($page)) {
echo 'The $page variable is a number';
} else {
echo 'The $page variable is NOT a number';
}
I have this code:
return t("Use tokens like: eg. [youtube_video:id]");
Because of the brackets used in my string PHP treat this [youtube_video:id] as an array key and returns notice like: Notice: Use of undefined constant _miscellaneous_filter_tips - assumed '_miscellaneous_filter_tips' w miscellaneous_filter_info_alter()
How can I resolve it?
All the code after a request:
function _miscellaneous_filter_tips() {
return t('Use tokens like: eg. [yamandi:youtube_video:id]');
}
function miscellaneous_filter_info_alter(&$info) {
$info['filter_tokens']['tips callback'] = _miscellaneous_filter_tips;
}
since I don't have the precursor code I can't actually see what the function t() does, however if is seems to think you are calling a variable try using
return t('Use tokens like: eg. [youtube_video:id]');
or if you still want to use variables
return t("Use tokens like: eg. " . '[youtube_video:id]');
Just change the double quote into a single quote:
return t('Use tokens like: eg. [youtube_video:id]');
EDIT
After looking to the updated code, this might be a completely different issue, I think you might wanna try this way of storing function hooks:
function _miscellaneous_filter_tips() {
return t('Use tokens like: eg. [yamandi:youtube_video:id]');
}
function miscellaneous_filter_info_alter(&$info) {
$info['filter_tokens']['tips callback'] = '_miscellaneous_filter_tips';
}
And then when you want to call that function, you can simply use:
$info['filter_tokens']['tips callback']();