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']();
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 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.
I'm writing my own debug functions and I need some help to fix the code below.
I'm trying to print a variable and its name, the file where the variable and the function was declared and the line of the function call. The first part I did, the variable, the variable name, the file and the line is printed correctly.
At the code, a($variable) works good.
The problem is I'd like this function accepts a string too, out of a variable. But PHP returns with a fatal error (PHP Fatal error: Only variables can be passed by reference in ...). At the code, a('text out').
So, how can I fix this code to accept a variable or a string correctly?
code (edited):
function a(&$var){
$backtrace = debug_backtrace();
$call = array_shift($backtrace);
$line = $call['line'];
$file = $call['file'];
echo name($var)."<br>".$var."<br>".$line."<br>".$file;
}
$variable='text in';
a($variable);
a('text out');
I need pass the variable by reference to use this function below (the function get the variable name correctly, works with arrays too):
function name(&$var, $scope=false, $prefix='unique', $suffix='value'){
if($scope) $vals = $scope;
else $vals = $GLOBALS;
$old = $var;
$var = $new = $prefix.rand().$suffix;
$vname = FALSE;
foreach($vals as $key => $val) {
if($val === $new) $vname = $key;
}
$var = $old;
return $vname;
}
The way your code is currently implementing pass by reference is perfect by design, but also by design cannot be changed to have two a() methods - one accepting a variable by reference and the other as a string-literal.
If the desire to pass a string literal instead of assigning it to a variable first is really needed, I would suggest creating a second convenience method named a_str() that actually accepts a string-literal instead of a variable by reference. This method's sole-purpose would be to relay the variable(s) to the original a() method - thereby declaring a variable to pass by reference.
function a_str($var) {
a($var);
}
The only thing to remember is, use a($variable); when passing by reference and a_str('some text'); when not.
Here is the same convenience-method for your name() function:
function name_str($var, $scope=false, $prefix='unique', $suffix='value'){
return name($var, $scope, $prefix, $suffix);
}
The only way to do what you are asking without writing an additional function like #newfurniturey suggests is plain and simply opening and parsing the file where your function was called as text (e.g. with fopen), using the data from debug_backtrace. This will be expensive in terms of performance, but it might be ok if used only for debugging purposes; and using this method you will no longer need a reference in your function, which means you can freely accept a literal as the parameter.
I have the following structure:
$par4 = json_decode($source_code)->$par1->$par2->$par3;
$par5 = $par4[0]->attributes->attribute[1]->value;
where par1, par2 and par3 are strings. How do I chain the par4 and par5 on one line.
This does not work because of the array / object nesting I guess:
json_decode($source_code)->$par1->$par2->$par3[0]->attributes->attribute[1]->value;
Here's the error:
Undefined property: stdClass::$o
What about
$par5 = current(json_decode($source_code)->$par1->$par2->$par3)->attributes->attribute[1]->value;
This works if you are always need in the first (0th) value of the array.
You can also create a function that returns the nth-value:
function third_value($arr) { return $arr[2]; }
$par5 = third_value(json_decode($source_code)->$par1->$par2->$par3)->attributes->attribute[1]->value;
I'm not sure, what you really need but try using {} to highlight what you need
{json_decode($source_code)->$par1->$par2->$par3}[0] // I think this is right
json_decode($source_code)->$par1->$par2->${par3[0]}
json_decode($source_code)->$par1->$par2->{$par3[0]}
I'm having trouble with the following code. What it should do is echo cats.php followed by example.php but it's not echoing the example.php. Any ideas why this might be happening?
$bookLocations = array(
'example.php',
'cats.php',
'dogs.php',
'fires.php',
'monkeys.php',
'birds.php',
);
echo $bookLocations[1];
function findfile($filenumber)
{
echo $bookLocations["$filenumber"];
}
findfile(0);
Try changing,
echo $bookLocations["$filenumber"];
to:
echo $bookLocations[$filenumber];
Edit* To expand on Thomas's correct answer, instead of using global variables, you could change your method to:
function findfile($filenumber, $bookLocations)
{
echo $bookLocations[$filenumber];
}
i believe you may also need to declare the global variable in your function.
global $bookLocations;
Ok, there are two issues.
Variable Scope
Your function doesn't know the array $bookLocations, you need to pass it to your function like so:
function findfile($filenumber, $bookLocations)
Array key
You don't want to wrap your array key in quotes:
wrong: $bookLocations["$filenumber"];
right: $bookLocations[$filenumber];
The quotes in "$filenumber" turn your key into a string, when the keys to your array are all numbers. You are trying to access $bookLocations["1"] when in fact you want to access $bookLocations[1] -- that is to say, 1 is not the same as "1". Therefore, like others have said, you need to get rid of the quotation marks around the key (and check your variable scope too).
function findfile($filenumber)
{
global $bookLocations;
echo $bookLocations[$filenumber];
}
Good-style developers usually avoid global variables. Instead, pass the array to the function as the parameter:
function findfile($files, $filenum)
{
echo $files[$filenum];
}
$bookLocations is out of scope for your function. If you echo $filenumber you will see that it's in scope because you passed it in by value. However, there is no reference to $bookoLocations.
You should pass in $bookLocations
declaration: function findfile($filenumber, $bookLocations){
call: findfile(1, $bookLocations);
You could also to declare $bookLocations as global, but globals should be avoided if possible.