0

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>';
?>
Cavalier
  • 93
  • 9
  • if empty then use today's date ? Do you have any framework / templating engine running on your site ? – Ken Lee May 06 '23 at 01:08
  • @KenLee No, everything I have is self created with the exceptions of instances where I need help that I derive from here. If it were to be empty, I would think I would have just checked the date() function and if(!empty) then echo it out. I'm not sure why I using this snippet or what It was for. – Cavalier May 06 '23 at 01:11
  • Posts with this technique: [Dynamically create PHP object based on string](https://stackoverflow.com/a/17167406/2943403) and [Code-Golf: one line PHP syntax](https://stackoverflow.com/a/3012044/2943403) and [Accessing an array element when returning from a function](https://stackoverflow.com/q/3642516/2943403) and [Interpreting return value of function directly as an array](https://stackoverflow.com/a/5781374/2943403) and [Explain this fragment of PHP for me: returning array and immediately reference an index](https://stackoverflow.com/q/6398862/2943403) – mickmackusa May 06 '23 at 01:43
  • And [Create new class from appended string](https://stackoverflow.com/a/14913695/2943403) and [How to instantiate from a string class name in PHP?](https://stackoverflow.com/a/23207515/2943403) and [Why can you instantiate a class from a string variable but not a string in PHP?](https://stackoverflow.com/a/32468357/2943403) and [Calling PHP functions within HEREDOC strings](https://stackoverflow.com/a/36202673/2943403) and [PHP static variables in double quotes](https://stackoverflow.com/a/64800522/2943403) – mickmackusa May 06 '23 at 01:49
  • and [Proper way to access a static variable inside a string with a heredoc syntax?](https://stackoverflow.com/a/39778415/2943403) ...but I reckon you grabbed your text from Bishop @ **Calling PHP functions within HEREDOC strings** – mickmackusa May 06 '23 at 01:51
  • As described in @bishop's answer on the dupe target this is a "parser hack" to allow a function to be called where it is normally not allowed (such as inside of a HEREDOC block. You are not confined by such a problem, so this syntax only makes your snippet smell of bad design and will lead to future readers wasting their time trying to understand and maintain the code. If you were silly enough to declare a variable as `${''}` prior to running this code, then that variable would be overwritten by this hack. – mickmackusa May 06 '23 at 02:01

3 Answers3

5

To be completely frank, it is doing a lot of BS for no reason.

$input = 'foo '.${!${''} = date('Y-m-d')}.' bar';

var_dump(
    $input,
    ${''},
    !${''},
    ${!${''}},
    ${false}
);

Output:

string(18) "foo 2023-05-06 bar"
string(10) "2023-05-06"
bool(false)
string(10) "2023-05-06"
string(10) "2023-05-06"

It is abusing a few things:

  1. The results of an assignment expression is the value assigned, eg: $a = 42 == 42.
  2. ${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.

Sammitch
  • 30,782
  • 7
  • 50
  • 77
  • I think I remembered why I found that code. I am/was using the heredoc to store my html. But the date() function would not output the date as functions supposedly do not work with in the heredoc. So I found that code here on stackoverflow in order for the date function to output the date. – Cavalier May 06 '23 at 01:17
  • Don't do this. Just create a variable before the heredoc and then use the variable. If not for the sake of sane and readable code, then for the sake of the fact that one of the language quirks that makes this possible is deprecated and will break in the future. – Sammitch May 06 '23 at 01:27
  • Trust me, I'm not using the heredoc stuff in my code anymore. Too many issues. – Cavalier May 06 '23 at 01:28
  • 1
    @Sam see my collection of links under the question. It should shed light on the intention of the code under different circumstances. If NikiC uses this, then it cannot be totally insane. Probably just got lost in translation (Chinese whispers) when transferred from code to code to code without understanding the usage. – mickmackusa May 06 '23 at 01:51
0

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:

$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>';
maiorano84
  • 11,574
  • 3
  • 35
  • 48
  • I was thinking it was some way that if the date was empty it didn't display the date?? I can't recall what it is for exactly but I believe I got it from this site because this and the PHP manual are the only two places I go to find answers – Cavalier May 06 '23 at 01:07
  • 1
    OHHH!!!! I think I remember what it was for. I am using the heredoc for the html, and the date() does not output the value. The crazy ${!${''} = date('Y-m-d')} is what allows the date function to work within the heredoc – Cavalier May 06 '23 at 01:14
-2

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.

Cavalier
  • 93
  • 9
  • 1
    "I really have no idea exactly what it is doing" ...then you should not have posted this answer. The question is what does it mean. This is Not An Answer -- it should be a comment or question edit. – mickmackusa May 06 '23 at 01:25
  • I posted the answer because I know what it is for... I just don't understand exactly what it is doing, but Sammitch explained what the code is doing, and I'm trying to wrap my head around his explanation. – Cavalier May 06 '23 at 01:27
  • @Cavalier your explanation seems to the only that make sense for that syntax (also if it a 'trick' that abuses the normal rules) – Pippo May 06 '23 at 19:47