0

Filename is: wn200.php

The script in the file is:

<?php 
include ('projectstatus.php');
$id_title = explode(",",$wn200_id_title);
$proj_id = $id_title[0];
$title = $id_title[1]; 
?> 

I want that "wn200" in the script should be automatically picked up from the filename. For that I am doing this:

$file_name = explode(".",basename($_SERVER['PHP_SELF']));
$file_name = $file_name[0];

So I have wn200 in the $file_name but how can I insert it in:

$id_title = explode(",",$wn200_id_title);

so that I don't have to write wn200 in the script.

After reading the comment about variable variable I tried this:

$file_name = explode(".",basename($_SERVER['PHP_SELF']));
$$n = $file_name[0];
include ('projectstatus.php'); // this file has $wn200_id_title = "wn200,Test"
$id_title = explode(",",$$n_id_title); // $$n part works but how do I connect it to _id_title so that the script reads it as $wn200_id_title = "wn200,Test"
$proj_id = $id_title[0]; // this shows "wn200"
$title = $id_title[1]; // this does not show "Test"

So I need to know how to connect $$n to _id_title so that it appears like $wn200_id_title in order to use its value in the script.

SpeckDrip
  • 31
  • 6
  • 2
    Have a look at [variable variables](https://www.php.net/manual/en/language.variables.variable.php). – El_Vanja Feb 06 '21 at 14:12
  • I had a look at variable variables and edited the post with what I tried, pls have a look. thanks. – SpeckDrip Feb 07 '21 at 10:08
  • You seem to have misunderstood the idea. When you create `$$n = $file_name[0];`, you should be getting an "Undefined variable $n" notice. If you don't see it, make sure you turn on [error reporting](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display). You need to first declare a normal variable that contains a string with the name to be used dynamically. Then you use that variable with `$$` to access a variable with that name. – El_Vanja Feb 07 '21 at 10:27
  • I do see the errors. But if I declare a normal variable then I have to start by declaring it with values from $wn200_id_title, whereas I want to avoid manually typing in wn200. To put it in another way. Is it possible to insert the values of $wn200_id_title in a variable without having to type "wn200" in $wn200_id_title? – SpeckDrip Feb 07 '21 at 10:44
  • `$file_name[0].'_id_title'` - you create the dynamic name as a string then use it afterwards. – El_Vanja Feb 07 '21 at 10:54
  • when you close _id_title in quotes it becomes a string and the original value of that variable is lost. e.g. I get undefined offset in this case. Instead of getting the title as "Test" I am getting "_id_title". – SpeckDrip Feb 07 '21 at 12:36
  • 1
    `$dynamicName = $file_name[0].'_id_title'` then you later use it `$$dynamicName`. If it's not working, the error is elsewhere. – El_Vanja Feb 07 '21 at 20:51
  • Perfect! Thank you very much. – SpeckDrip Feb 07 '21 at 21:58

0 Answers0