/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Create variables from expressions

Hi all,

I’ve seen several examples here and elsewhere, where you can create a variable from a variable via something like: ${$myvar}; but nothing using an expression.

From other examples I’ve seen on this forum, you can also use square brackets to build variables from variables.

Anyway, based on a loop, I want to create a series of variables as follows:

$MyTemplateShift1Start
$MyTemplateShift2Start
$MyTemplateShift3Start
$MyTemplateShift4Start
etc

Assuming I am looping where I increment $shiftnumcount by one with each loop, will the following work? Note, for my purposes, the looping number stored in $shiftnumcount MUST be set one higher that the loop for each variable – thus the need for an expression.

[CODE]while ($shiftnumcount >= $NumberSelectedByUser) {
$RosterTemplateShift[($shiftnumcount+1).’Start’];
}[/CODE]

Any help is much appreciated!

Cheers,

Chris

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogFeb 14.2010 — There's some inconsistency here as to what you want. Your code snippet implies the setting of an array element such as [b]$RosterTemplateShift['2Start'][/b], which is not the same thing as a scalar variable named [b]$RosterTemplateShift2Start[/b]. Generally, I would recommend going with the array approach as opposed to the "variable variables" approach, possibly using multiple dimensions (array keys) to better model the desired data structure. For instance, it might be that you want something like:
[code=php]
$RosterTemplateShift[1]['start'] = '08:00';
$RosterTemplateShift[1]['end'] = '16:30';
$RosterTemplateShift[2]['start'] = '16:00';
$RosterTemplateShift[2]['end'] = '00:30';
[/code]

But, that's only speculation at this point, not knowing the functional requirements you are trying to model/implement.
Copy linkTweet thisAlerts:
@Chris_JacksauthorFeb 14.2010 — Thanks NogDog!

Ah yes, if it were a larger set, I would look further into an array, but in this case I'm only dealing with a set of 12 numbers and, for readability's sake, and to stick with what I'm familiar with for now, is it possible to create a variable like:

$MyTemplateShift{[I]expression[/I]}Start

where {[I]expression[/I]} is something like $shiftnumcount+1

?

Here's an example I just dug up (but haven't tried - away from my desk right now)...

${'MyTemplateShift'.($shiftnumcount+1).'Start'}

Any good?
Copy linkTweet thisAlerts:
@NogDogFeb 14.2010 — I believe that would work, but I'd still use an array, as I personally find it more readable, plus it opens up the possibility of using the dozens of built-in array functions to work with the data (both now and in future enhancements).
Copy linkTweet thisAlerts:
@Chris_JacksauthorFeb 16.2010 — Thanks NogDog. Incidentally, for anyone else who needs it... this syntax works...

${"StringPartA{$varname}StringPartB"}
×

Success!

Help @Chris_Jacks spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...