Modifying PHP code of an Include, outside of the include? - php

I want to set a PHP variable: $custom, that will be used whenever it is defined.
But I want the output of this variable to be dependant of another variable: $v index, which is used inside while, which gets defined only during the while statement.
I don't know if it's possible to do, right now I have the following code, and for the way I defined $custom[3], it doesn't work.
Only $custom[1] and $custom[2] varibables will work, but they take into account only constant values or variable whose values were already set, so this isn't helpful.
Code:
<?php
$product[1]["price"]=10;
$product[2]["price"]=50;
$product[3]["price"]=70;
$custom[1] = 'Static HTML Block';
$custom[2] = 'Past Variable: '. $product[2]["price"] .'';
$custom[3] = 'Future Variable: '. $product[$v]["price"] .''; // I want this kind of definitoin
?>
<HTML>
// the following part will be used within an include, and shouldn't be modified ///
<?php
$v = 1;
while ($z <= 5) {
?>
<?= $custom[$v] ? $custom[$v] : '$' . $product[$v]["price"] ?>
<?php
$v = $v + 1;
$z = $z + 1;
}
?>
So basically, I want that on the third run (when v=3), that Future Variable: 70 will be the output.
The Rationale:
I want to use the latter code as a constant Include, that will serve as a template for all files. but on occasion, a certain file may require special changes that also require PHP code modification, so I will want to perform them within such specific file, that will affect the original include.
Edit 2:
More Simple example:
<?php
$product[1]["price"]=10;
$product[2]["price"]=50;
$custom[1] = 'I dont modify the PHP code';
$custom[2] = 'I DO mofiy the latter PHP code: '. $product[$v]["price"] .'';
?>
<HTML>
// the following part will be used within an include, and shouldn't be modified ///
<?php $v = 1; while ($v <= 5) { ?>
<?= $custom[$v] ?>
<p>
<?php $v = $v + 1; } ?>
// Expected Output:
//I dont modify the PHP code
//I DO mofiy the latter PHP code: 50

It's a little difficult to tell what you are trying to do, but I think you need to decouple your data from your presentation:
$product[1]['price']=10;
$product[2]['price']=50;
$product[3]['price']=70;
$custom[1] = 'Static HTML Block';
$custom[2] = 'Past Variable: %s';
$custom[3] = 'Future Variable: %s';
$v = 1;
while($z <= 5) {
$price = $product[ $v ]['price'];
printf($custom[ $v ], $price);
$v++;
$z++;
}
In this case, $custom stores the format, but doesn't actually contain the data. You call printf to output the desired format with whatever data you want to pass.

It's not clear to me what you're trying to accomplish, but if I had to guess, you want something known as templates (e.g. Twig), and your question might possibly be a XY problem. If you supply more context maybe someone might be able to help you further.
As to the matter at hand, you cannot define $custom[3] here since $v is not defined yet:
$custom[3] = 'Future Variable: '. $product[$v]["price"] .''; // I want this kind of definitoin
The closest you can get is to use eval (which is rarely recommended), or define a closure, even if it's awkward. Instead of a variable, you define a function:
$custom[3] = function($productData) {
return 'Future Variable: '. $productData["price"] .'';
}
The above you can do in an included file.
Then in the loop you check whether the custom object is a function, and if so, you call it and assign the return value to the reply.
The code below stays the same whatever the include - actually, it even works with the data you have now. Of course, it has to be a little more complicated.
while ($z <= 5) {
// If the $v-th item is customised
if (array_key_exists($v, $custom)) {
// Verify what it is
if (is_callable($custom[$v])) {
// if it's a function, call it.
$reply = $custom[$v]($product);
} else {
$reply = $custom[$v];
}
} else {
// Not customised, just print the price.
$reply = '$' . $product[$v]["price"];
}
print $reply;
Another more "templateish" solution is to define strings and then process them using e.g. preg_replace_callback in order to generate the desired HTML. In this case, too, you need to pass to the templater a fixed variable, such as $product[$v]. You need some kind of 'language' to indicate where you want elements of this fixed variables to be instantiated. Usually, square brackets or curly brackets, often doubled, are used:
$custom[3] = 'Future Variable: {{price}}';
At resolution time, preg_replace_callback may be made to replace {{price}} with the value in $product[$v]['price'], and so on (e.g. {{weight}}, {{description}}...).
(You can also do this server side and populate the HTML page via AJAX using client-side libraries such as Handlebars).

The quick and nasty way to make your own template is to create an array of keys => values and pass it to the where loops as your $custom variable.
While you're in the where(){} loop, use the php function extract to convert the array into $key = 'value' pairs. This is very similar to list without having having to define each variable manually.
http://php.net/manual/en/function.extract.php
Eg.
$data = [
'foo' => 'bar',
];
extract($data);
print $foo; // output: bar
Then after you extract, include your file. Make sure that you wrap this in output buffering mode ob_start() and put it in a function so your local variables can't be overriden.
This is how you make homebrew templating languages.

Related

PHP: create an array of functions with a for loop

I have some code that needs a list of functions.
I want to create automatically part of this list.
Scope rules in PHP prevent my following approach to the problem from working:
$list=[];
$i=0;
for(;$i<10; $i++) {
$list []= function() {
// code that --depends-- on $i, like:
return $i;
};
}
// example of use of the 7th function in the list:
echo $list[6]();
Outputs (on my test machine I have PHP 8.1.2)
Warning: Undefined variable $i in [...]/test.php on line [...]
Because when the 7th anonymous function in $list is called by the last line, its body refers to $i, which is local to the function body and does not refer to the loop variable $i. If $i were declared global it would not issue a warning but would not do what I want either.
Notes:
In some posts here, OO programming is mentioned. But I do not know enough about OO programming and PHP to see how.
create_function is no more available in PHP 8
I know there are simpler code for outputting 6, like echo 6; thanks.
<?php
$list=[];
for($i=0;$i<10; $i++) {
$list []= function() use ($i) { // <--
return $i;
};
}
echo $list[6]();
You are missing the capture phrase in PHP: https://www.php.net/manual/en/functions.anonymous.php
Closures may also inherit variables from the parent scope. Any such variables must be passed to the use language construct.

php array syntax ${ is confusing me

I create a $values array and then extract the elements into local scope.
$values['status'.$i] = $newStatus[$i];
extract($values);
When I render an html page. I'm using the following
<?php if(${'status'.$i} == 'OUT'){ ?>
but am confused by what the ${ is doing and why $status.$i won't resolve
$status.$i means
take value of $status variable and concatenate it with value of $i variable.
${'status'.$i} means
take value of $i variable, append id to 'status' string and take value of a variable 'status'.$i
Example:
With $i equals '2' and $status equals 'someStatus':
$status.$i evaluated to 'someStatus' . '2', which is 'someStatus2'
${'status'.$i} evaluated to ${'status'.'2'} which is $status2. And if $status2 is defined variable - you will get some value.
I wanted to add to the accepted answer with a suggested alternate way of achieving your goal.
Re-iterating the accepted answer...
Let's assume the following,
$status1 = 'A status';
$status = 'foo';
$i = 1;
$var_name = 'status1';
and then,
echo $status1; // A status
echo $status.$i; // foo1
echo ${'status'.$i}; // A status
echo ${"status$i"}; // A status
echo ${$var_name}; // A status
The string inside the curly brackets is resolved first, effectively resulting in ${'status1'} which is the same as $status1. This is a variable variable.
Read about variable variables - http://php.net/manual/en/language.variables.variable.php
An alternative solution
Multidimensional arrays are probably an easier way to manage your data.
For example, instead of somthing like
$values['status'.$i] = $newStatus[$i];
how about
$values['status'][$i] = $newStatus[$i];
Now we can use the data like,
extract($values);
if($status[$i] == 'OUT'){
// do stuff
}
An alternative solution PLUS
You may even find that you can prepare your status array differently. I'm assuming you're using some sort of loop? If so, these are both equivalent,
for ($i=0; $i<count($newStatus); $i++){
$values['status'][$i] = $newStatus[$i];
}
and,
$values['status'] = $newStatus;
:)

Add two $row together in one php echo

I'm not even sure if what I am trying to do is possible, I have a simple php echo line as below..
<?php echo $T1R[0]['Site']; ?>
This works well but I want to make the "1" in the $T1R to be fluid, is it possible to do something like ..
<?php echo $T + '$row_ColNumC['ColNaumNo']' + R[0]['Site']; ?>
Where the 1 is replaced with the content of ColNaumNo i.e. the returned result might be..
<?php echo $T32R[0]['Site']; ?>
It is possible in PHP. The concept is called "variable variables".
The idea is simple: you generate the variable name you want to use and store it in another variable:
$name = 'T'.$row_ColNumC['ColNaumNo'].'R';
Pay attention to the string concatenation operator. PHP uses a dot (.) for this, not the plus sign (+).
If the value of $row_ColNumc['ColNaumNo'] is 32 then the value stored in variable $name is 'T32R';
You can then prepend the variable $name with an extra $ to use it as the name of another variable (indirection). The code echo($$name); prints the content of variable $T32R (if any).
If the variable $T32R stores an array then the syntax $$name[0] is ambiguous and the parser needs a hint to interpret it. It is well explained in the documentation page (of the variable variables):
In order to use variable variables with arrays, you have to resolve an ambiguity problem. That is, if you write $$a[1] then the parser needs to know if you meant to use $a[1] as a variable, or if you wanted $$a as the variable and then the [1] index from that variable. The syntax for resolving this ambiguity is: ${$a[1]} for the first case and ${$a}[1] for the second.
You can do like this
$T1R[0]['Site'] = "test";
$c = 1;
$a = "T".$c."R";
$b = $$a;
echo "<pre>";
print_r($b[0]['Site']);
Or more simpler like this
$T1R[0]['Site'] = "test";
$c = 1;
$a = "T".$c."R";
echo "<pre>";
print_r(${$a}[0]['Site']);

Using array value with index as Variable Variable

The title may be a little confusing. This is my problem:
I know you can hold a variable name in another variable and then read the content of the first variable. This is what I mean:
$variable = "hello"
$variableholder = 'variable'
echo $$variableholder;
That would print: "hello". Now, I've got a problem with this:
$somearray = array("name"=>"hello");
$variableholder = "somearray['name']"; //or $variableholder = 'somearray[\'name\']';
echo $$variableholder;
That gives me a PHP error (it says $somearray['name'] is an undefined variable). Can you tell me if this is possible and I'm doing something wrong; or this if this is plain impossible, can you give me another solution to do something similar?
Thanks in advance.
For the moment, I could only think of something like this:
<?php
// literal are simple
$literal = "Hello";
$vv = "literal";
echo $$vv . "\n";
// prints "Hello"
// for containers it's not so simple anymore
$container = array("Hello" => "World");
$vv = "container";
$reniatnoc = $$vv;
echo $reniatnoc["Hello"] . "\n";
// prints "World"
?>
The problem here is that (quoting from php: access array value on the fly):
the Grammar of the PHP language only allows subscript notation on the end of variable expressions and not expressions in general, which is how it works in most other languages.
Would PHP allow the subscript notation anywhere, one could write this more dense as
echo $$vv["Hello"]
Side note: I guess using variable variables isn't that sane to use in production.
How about this? (NOTE: variable variables are as bad as goto)
$variablename = 'array';
$key = 'index';
echo $$variablename[$key];

Is there a way to get the name of a variable? PHP - Reflection

I know this is not exactly reflection, but kind of.
I want to make a debug function that gets a variable and prints a var_dump and the variable name.
Of course, when the programmer writes a call to the function, they already know the variable's name, so they could write something like:
debug( $myvar, 'myvar' );
But I want it to be quick and easy to write, just the function name, the variable, and voilĂ  !
debug( $myvar ); // quicker and easier :)
You can do it by converting the variable to a key/value set before passing it to the function.
function varName($theVar) {
$variableName = key($theVar);
$variableValue = $theVar[$variableName];
echo ('The name of the variable used in the function call was '.$variableName.'<br />');
echo ('The value of the variable used in the function call was '.$variableValue.'<br />');
}
$myVar = 'abc';
varName(compact('myVar'));
Though I don't recommend creating a reference to a nameless variable, function varName(&$theVar) works too.
Since compact() takes the variable name as a string rather than the actual variable, iterating over a list of variable names should be easy.
As to why you would want to do this -- don't ask me but it seems like a lot of people ask the question so here's my solution.
I know I'm answering a 4 year old question but what the hell...
compact() might help you is your friend here!
I made a similar function to quickly dump out info on a few chosen variables into a log for debugging errors and it goes something like this:
function vlog() {
$args = func_get_args();
foreach ($args as $arg) {
global ${$arg};
}
return json_encode(compact($args));
}
I found JSON to be the cleanest and most readable form for these dumps for my logs but you could also use something like print_r() or var_export().
This is how I use it:
$foo = 'Elvis';
$bar = 42;
$obj = new SomeFancyObject();
log('Something went wrong! vars='.vlog('foo', 'bar', 'obj'));
And this would print out like this to the logs:
Something went wrong! vars={"foo":"Elvis","bar":42,"obj":{"nestedProperty1":1, "nestedProperty2":"etc."}}
Word of warning though: This will only work for variables declared in the global scope (so not inside functions or classes. In there you need to evoke compact() directly so it has access to that scope, but that's not really that big of a deal since this vlog() is basically just a shortcut for json_encode(compact('foo', 'bar', 'obj')), saving me 16 keystrokes each time I need it.
Nope, not possible. Sorry.
Not elegantly... BUT YOU COULD FAKE IT!
1) Drink enough to convince yourself this is a good idea (it'll take a lot)
2) Replace all your variables with variable variables:
$a = 10
//becomes
$a = '0a';
$$a = 10;
3) Reference $$a in all your code.
4) When you need to print the variable, print $a and strip out the leading 0.
Addendum: Only do this if you are
Never showing this code to anyone
Never need to change or maintain this code
Are crazy
Not doing this for a job
Look, just never do this, it is a joke
I know this is very very late, but i did it in a different way.
It might honestly be a bit bad for performance, but since it's for debugging it shouldn't be a problem.
I read the file where the function is called, on the line it was called and I cut out the variable name.
function dump($str){
// Get the caller with debug backtrace
$bt = debug_backtrace();
$caller = array_shift($bt);
// Put the file where the function was called in an array, split by lines
$readFileStr = file($caller['file']);
// Read the specific line where the function was called
$lineStr = $readFileStr[$caller['line'] -1];
// Get the variable name (including $) by taking the string between '(' and ')'
$regularOutput = preg_match('/\((.*?)\)/', $lineStr, $output);
$variableName = $output[1];
// echo the var name and in which file and line it was called
echo "var: " . $variableName . " dumped in file: " . $caller['file'] . ' on line: ' . $caller['line'] . '<br>';
// dump the given variable
echo '<pre>' . var_export($str, true) . '</pre>';
}
i've had the same thought before, but if you really think about it, you'll see why this is impossible... presumably your debug function will defined like this: function debug($someVar) { } and there's no way for it to know the original variable was called $myvar.
The absolute best you could do would be to look at something like get_defined_vars() or $_GLOBALS (if it were a global for some reason) and loop through that to find something which matches the value of your variable. This is a very hacky and not very reliable method though. Your original method is the most efficient way.
No, the closer you will get is with get_defined_vars().
EDIT: I was wrong, after reading the user comments on get_defined_vars() it's possible with a little hack:
function ev($variable){
foreach($GLOBALS as $key => $value){
if($variable===$value){
echo '<p>$'.$key.' - '.$value.'</p>';
}
}
}
$lol = 123;
ev($lol); // $lol - 123
Only works for unique variable contents though.
Bit late to the game here, but Mach 13 has an interesting solution: How to get a variable name as a string in PHP
You could use eval:
function debug($variablename)
{
echo ($variablename . ":<br/>");
eval("global $". $variablename . ";");
eval("var_dump($" . $variablename . ");");
}
Usage: debug("myvar") not debug($myvar)
This is late post but I think it is possible now using compact method
so the code would be
$a=1;
$b=2;
$c=3
var_dump(compact('a','b','c'));
the output would be
array (size=3)
'a' => int 1
'b' => int 2
'c' => int 3
where variable name a, b and c are the key
Hope this helps
I believe Alix and nickf are suggesting this:
function debug($variablename)
{
echo ($variablename . ":<br/>");
global $$variablename; // enable scope
var_dump($$variablename);
}
I have tested it and it seems to work just as well as Wagger's code (Thanks Wagger: I have tried so many times to write this and the global variable declaration was my stumbling block)

Categories