Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Is it possible to declare multiple arrays in PHP in one line?

$userAnswers  = array();
$questionIDs = array ();
$sqlAnswers = array();
$sqlAnswersQuery = array();
$correctAnswers = array();

Any cleaner ways of doing this?

NOTE: The contents of these arrays are all DIFFERENT. I don't think setting them equal to each other would work.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

There is a way. Wether you like it or not is another question:

$userAnswers = $questionIDs = $sqlAnswers = $sqlAnswersQuery = $correctAnswers = array();

I'm not a fan of this. This isn't easy to read, especially for the-new-guy-who-doesn't-know-this-code. This works for small amounts of variables, but even then with caution:

$hasErrors = $hasWarnings = false;

I think the way you should declare the variables as array depends on how you set the first values, we can't see enough code to answer based on that.

share|improve this answer
    
All of my arrays are going to have different contents, would setting them equal to each other make them have all the same contents? –  Nicholas Roberts Jun 22 at 7:44
1  
Nope, it's not by reference, you just all set them to the last value of the line. But again, your current method is a better plan. Just add a few spaces to line up the equation signs, will be enough. –  Martijn Jun 22 at 8:19
    
Cool, thanks man. –  Nicholas Roberts Jun 22 at 16:24

This is good as it is. It's recommended to have one statement per line, for better readability: code is easier to read when the logic flows from top to bottom, with no distractions sideways. So even if there was a way to do this on one line, you really shouldn't.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.