wip trying to get the var name directly, first try not working

This commit is contained in:
asus
2024-02-07 18:54:30 +01:00
parent b943682047
commit ae5fa7191a
2 changed files with 26 additions and 4 deletions

View File

@@ -44,9 +44,13 @@ function main_shortcode() {
# https://wiki.php.net/rfc/nameof
# but for now i have to declare both the variable and its name
# (more : https://stackoverflow.com/q/255312/9497573)
# add_var_to_front( array(
# "myvar_1" => $myvar_1,
# "myvar_2" => $myvar_2,
# ));
add_var_to_front( array(
"myvar_1" => $myvar_1,
"myvar_2" => $myvar_2,
$myvar_1,
$myvar_2,
));
}

View File

@@ -28,10 +28,28 @@ function add_script_to_front($script_files_arr) {
}
# https://stackoverflow.com/a/61442261/9497573
function get_varname(&$var) {
$tmp = $var; // store the variable value
$var = '_$_%&33xc$%^*7awdlawidawd__D:DWD2398q_r4'; // give the variable a new unique value
$name = array_search($var, $GLOBALS); // search $GLOBALS for that unique value and return the key(variable)
$var = $tmp; // restore the variable old value
console_log("name : " . $name);
return $name;
}
function add_var_to_front($var_array) {
foreach ($var_array as $key => $var)
# foreach ($var_array as $key => $var)
# {
# $js_var = 'const ' . $key . ' = ';
# $js_var .= json_encode($var);
# $js_var .= ';';
# console_log("in php, js_var: " . $js_var);
# wp_add_inline_script('myscript', $js_var, 'before');
# }
foreach ($var_array as $var)
{
$js_var = 'const ' . $key . ' = ';
$js_var = 'const ' . get_varname($var) . ' = ';
$js_var .= json_encode($var);
$js_var .= ';';
console_log("in php, js_var: " . $js_var);