Page 1 of 1

PHP 4/5 make a difference to this code?

Posted: Wed Jul 12, 2006 4:25 am
by mattcooper
Is there a difference in how this method will run in PHP4 as opposed to PHP5? (Ignoring the fact that the "static" keyword is used, that is) Any comments on this code very welcome...

Code: Select all

static function connect($dbName)
{
  if ($this->db == null)
  {
    $this->db = new $this->mysql_connect($dbname);
  }
}
Also, what is the function of this piece of code? I can't quite get my head around the "double []":

Code: Select all

for ($x = 0; $x < Database::get_total_rows(); $x++)
	{
	foreach ($columns as $colname)
		{
		$data[$x][$colname] == Database::get($colname);
		}
		$oStats->NextRecord();
	}
Cheers :)

Posted: Wed Jul 12, 2006 4:52 am
by Jenk
For your first question, I don't know.. I wouldn't have thought using "new $this" was possible regardless of version as your are instantiating an object, from an object and not a class, meh.

Second question: That is a multidimensional array. In layman's terms, it's an array of arrays.

e.g.:

Code: Select all

<?php

$array = array(array('foobar'));

echo $array[0][0]; //outputs (string) 'foobar'

?>
Multi-dimensional arrays are very handy in varying situations. A particular instance I have seen and used such an array, is in the use of storing Database Table Rows..

Code: Select all

<?php

$rows = array();

while ($row = mysql_fetch_assoc($result)) {
    $rows[] = $rows;
}

?>
$rows will now contain every row returned by the query (that has been omitted in the example.)

Therefore, if I wanted to echo, for example, the column labelled "user" from the 5th row, I can use the following:

Code: Select all

echo $rows[4]['user'];
remembering of course that the $rows array is a zero-indexed array (first indice is 0.)

HTH :)