PHP 4/5 make a difference to this code?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

PHP 4/5 make a difference to this code?

Post 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 :)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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 :)
Post Reply