2

I've looked at other questions/answers but I'm not fully understanding it. Furthermore, I haven't found one asking my exact question.

I have 7 arrays storing the start and end times for a business. My PHP program checks for the current day and matches it with the start end time for that day. So right now I have 7 if statements similar to this:

if (date(l) == 'Sunday') {
   $start = $SundayAccountingHours['start'];
   $end = $SundayAccountingHours['end'];
 }

What I want to do is clean up the code. So maybe I could have something like

$start = ${$today}.AccountingHours['start'];
$end = ${$today}.AccountingHours['end'];

How can I make this work? Using the above example I'm getting this: Parse error: syntax error, unexpected '[' in C:\xampp\htdocs\Dev.php on line 22 where line 22 is $start is defined. I can't take out the stuff in the brackets because THAT'S the information I need to really get to.

If you can't tell, I'm still novice at PHP so any help will be appreciated.

Thanks!

1
  • 1
    the . in php is not an property operator but instead a concatenation operator. Commented May 22, 2013 at 2:43

3 Answers 3

5
if (date(l) == 'Sunday') {
   $start = $SundayAccountingHours['start'];
   $end = $SundayAccountingHours['end'];
 }

First, put quotes around that 'l'. It's looking for a constant named l, and failing to find that it's treating it like a string. This is very bad practice (and should issue a warning).

Secondly, use a multi-dimensional array. You don't need to try and use the day-of-the-week as part of your variable name. e.g.,

$start = $AccountingHours[$today]['start'];

You might discover variable-variables down the road which allow you do something like what you're trying, but I strongly advise you to steer clear of them. I don't think there's a single practical application for them and they only serve to cause confusion and error (cough).

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

Comments

1

If you MUST use variable variable names (You should really go for multi-dimensional arrays though), try something like this.

$start = ${$today.'AccountingHours'}['start'];

6 Comments

This is a case of solving the apparent problem, not the actual problem.
Completely agreed. Which is why I wrote the first line. :)
I will try to wrap my head around multi-dimensional arrays then. When I echo $start using your code, nothing is showing up. It's like the variable is empty...
Try var_dump($SundayAccountingHours) to see if the variable is being generated at all. This is actually one of the main problems with using variable variables. The code becomes very difficult to debug.
I do agree that multi-dimensional arrays would be the correct answer to this problem. However, I am marking this as the accepted answer because it actually answered the question I asked. I WILL not be using this method in the final version of my program though.
|
0

Array elements can be referenced by variables like so:

$myArray[$myVar]

Arrays can also contain other arrays, like so:

$myArray = array();
$myArray['a'] = array();
$myArray['a']['b'] = 'foo';
print $myArray['a']['b'];  // prints 'foo'

So combine the two concepts and you can create a multi-dimensional array referenced by variables:

$accountingHours = array();
$accountingHours['Sunday'] = array('start' => ..., 'end' => ...);
// ...
$start = $accountingHours[$today]['start'];

Comments

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.