0

I'm wondering how I can create external variables (for lack of better terms) dynamically. What I mean by external variable is what holds the internal variable.

External variable

$external_variable

Internal variable

$external_variable = 'internal_variable';

Now let's say I want to create a different suffix for the external variable dynamically, is it possible?

Example using a forloop

for ($a=1; $a <= 3; $a++) {

$external_variable_$a

}

result

$external_variable_1
$external_variable_2
$external_variable_3

Does it make any sense to do this like this?

EDIT

I'm giving more details with real code. I'm actually trying to change $pw_monday so that every time the forloop iterates the $pw suffix becomes the next weekday in $_SESSION. Or maybe I'm too tired to figure out another way.

//get all the dates of previous week
$pw_monday = date("Y-n-j",strtotime($previousweek  .' last Monday' ));
$pw_tuesday = date("Y-n-j",strtotime($pw_monday  .' +1 day' ));
$pw_wednesday = date("Y-n-j",strtotime($pw_tuesday  .' +1 day' ));
$pw_thursday = date("Y-n-j",strtotime($pw_wednesday .' +1 day' ));
$pw_friday = date("Y-n-j",strtotime($pw_thursday .' +1 day' ));                                                                 
$pw_saturday = date("Y-n-j",strtotime($pw_friday .' +1 day' ));
$pw_sunday = date("Y-n-j",strtotime($pw_saturday .' +1 day' ));  

//put all dates in session
for ($rn=1; $rn <= 7; $rn++) {
//request days
$requestNs ='request_'.$rn;
$_SESSION[$requestNs] =  $pw_monday;                                                                                                                                               
}

final result

$_SESSION[request_1] =  $pw_monday;  
$_SESSION[request_2] =  $pw_tuesday;  
//...get the drift
6
  • I've read this three times and I have no idea why you would do this. I think this is an XY Problem. What are you trying to do? Commented Sep 24, 2017 at 1:52
  • You could do it by creating an array like $myArr = ['var1' => 1, 'var2' => 2], and then doing extract($myArr). Should you ? - most likely no. As @Machavity has noted, what you really want to do is definitely achievable with a better solution. Commented Sep 24, 2017 at 1:56
  • @hlfrmn: I have updated my code with more details so that it is more understandable. Commented Sep 24, 2017 at 2:00
  • @Machavity: I just updated my code so that you can understand. Thanks for suggesting. Commented Sep 24, 2017 at 2:01
  • @BalloonFight so you want to have $_SESSION['request_1'] == $monday_date, $_SESSION['request_2'] == $tuesday_date etc., where the date variables are like 2014-3-10 ? Commented Sep 24, 2017 at 2:07

1 Answer 1

1
$pw_monday = date('Y-n-j', strtotime($previousweek . ' last Monday'));
for ($day = 0; $day < 7; $day++) {
    $nth_day = date('Y-n-j', strtotime("$pw_monday +$day day"))
    $_SESSION["request_$day"] =  $nth_day;                                                                                                                                               
}

Something like this will do the trick.

Although, most likely you don't want to add these dates to the session, but then you would need to share with us why you want to store them there.

Honestly, I wasn't able to come up with a reason to do this, so we might be in yet another iteration of the XY Problem.

Sign up to request clarification or add additional context in comments.

3 Comments

Why not? I'm actually using this to append dates to a calendar. It works like a charm.
@BalloonFight I just see no reason to persist 7 dates that can easily be calculated at any time. I would maybe create a helper function if needed, but of course I can't make a solid decision without actually knowing the whole design.
It's kinda hard without the whole design in mind that's what I was going to say. It makes perfect sense with a database. The dates are changing all the time. Thanks by the way. Cheers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.