Array and fetch_assoc symantics

I have seen a tutorial where someone uses the syntax $data = []; to declare an array instead of $data = (); My notes and web references refer to the round braces instead of square braces.

In the same tutorial, when attempting to display data from a database in tabular format, instead of using while($rows = $results->mysqli_fetch_assoc()), the person opts to use while($rows = $results->fetch_assoc()). I was under the impression that the standard query should be mysqli_fetch_assoc() as standard.

Another example:

while($rows = $results->fetch_assoc())
{ $comps[] = $rows; }

Can anyone shed some light on these differences? The person’s code still seems to run, but I am confused as to what is the more correct option in terms of both the array declaration and fetch query.

The new way to declare arrays is,

$array = [];

The old,

$array = array();

Where you found that this,

$array = (); <--- Should throw a syntax error

was acceptable, I don’t know as it should give an error.

mysqli_fetch_assoc() is the procedural way to call fetch_assoc()

You know, if you would just RTFM for arrays instead of depending on “notes and web references” you would have had your answer. I don’t know why people look everywhere but the manual. SMH.:thinking:

http://php.net/manual/en/language.types.array.php

As of PHP 5.4 you can also use the short array syntax, which replaces array() with [] .

Yeah, thanks. Apologies that I am not as experienced and clever as you are/plugged into the Matrix, became an expert PHP programmer within seconds and DARED to ask a question. BEGINNERS forum so LEARNING. And the SYNTAX FIRST specified is Array() in the MANUAL you are harping on.

‘Specifying with array() ¶’ to quote the exact heading. So if they were to be technically correct they should have specified the new method in the heading. So maybe the person who WTFM should do it right … So much for specifying that there is no such thing as a ‘dumb question’ in the forum description. Scratch that …

It’s not about being experienced or clever. Looking at the manual is the most basic thing anyone can do which you clearly didn’t. We don’t mind helping at all, but people need to put in a minimal effort. It took you more effort to find this site, register and post a question then it would have took to read the four short paragraphs in the manual about arrays which clearly tells you about the short array syntax.

This should help you out in the future. http://www.catb.org/esr/faqs/smart-questions.html

Sponsor our Newsletter | Privacy Policy | Terms of Service