I had borrowed this piece of code ${!${''} = date('Y-m-d')}
but I cannot for the life of me find anything that tells me what the ${!${''}
means and I do not remember what it does. I have looked in the PHP manual and on here and I find plenty for operators. Just not this.
If someone could explain what this is, breaking each part down, and what it does I would appreciate it. Just trying to find ${!${''}
is vague.
EDIT: This snippet of code was in the value attribute of an input element with type=”date”. And the snippet was assigned a variable
<?php
$input = '<input id="trim_date" class="w3-input w3-border w3-border-black" type="date" name="date_trimmed" value="'.${!${''} = date('Y-m-d')}.'" min="2022-01-01" max="2030-12-31" required>';
?>
3
Answers
I remember what it was used for. But I do not understand exactly what it is doing. In my old code that I am in the process of updating, I was using the heredoc syntax to hold my HTML code. In the input field for the date I was trying to set the value to the current date. But, if I'm not mistaken, functions cannot be used within the heredoc. So the date() function was not outputting the date. I found the answer here on stackoverflow somewhere and the person told me to use
${!${''} = date('Y-m-d')}
. And after that, the date value was showing up in the input element.But as I said, I really have no idea exactly what it is doing but I do know what it was used for.
I’ve never seen this syntax before. It seems unnecessarily difficult to read, but it’s appearing to use Variable Variables to declare a variable from a string.
Executing the code in a sandbox, it doesn’t appear to be doing much of anything of use.
Ultimately, a variable with an empty name is created with the value of the date function.
It would be just as easy to write the markup as:
To be completely frank, tt is doing a lot of BS for no reason.
Output:
It is abusing a few things:
$a = 42 == 42
.${expr}
can be used to define or access variables that do not adhere to PHP’s naming restrictions.It is creating a variable whose name is literally the boolean value
false
that contains the computed date string.I have no idea why anyone would write this code, especially since there is no reason to do this assignment in the first place.