...

View Full Version : syntax question



tripwater
06-07-2007, 07:46 PM
I found this code in a forum scrip and I was wondering what the various parts are doing and what it means.



for($j = 0; $j < count($row2); $j++)
{
$row_color = ( !($j % 2) ) ? $theme['td_color1'] : $theme['td_color2'];
$row_class = ( !($j % 2) ) ? $theme['td_class1'] : $theme['td_class2'];
$template->assign_block_vars('faq_block.faq_row', array(
'ROW_COLOR' => '#' . $row_color,
'ROW_CLASS' => $row_class,
'FAQ_QUESTION' => $row2[$j]["q"],
'FAQ_ANSWER' => $row2[$j]["a"],

'U_FAQ_ID' => $row2[$j]["q_id"])
);
$template->assign_block_vars('faq_block_link.faq_row_link', array(
'ROW_COLOR' => '#' . $row_color,
'ROW_CLASS' => $row_class,
'FAQ_LINK' => $row2[$j]["q"],

'U_FAQ_LINK' => '#' . $row2[$j]["q_id"])
);
}



So inside the for loop there are things like
$row_color = ( !($j % 2) ) ? $theme['td_color1'] : $theme['td_color2'];

I am not sure what the '?' and the : are doing in this statement.

Also the use of the symbols -> and => in this

$template->assign_block_vars('faq_block.faq_row', array(
'ROW_COLOR' => '#' . $row_color

If someone could kindly give a simple explanation as to what these symbols are doing in this context or point me somewhere, I would greatly appreciate it.

Up till now I have only use $_POST. $_SESSION and local vars. So these methods are kind of abstract to me right now. thank you for any help with this.

PappaJohn
06-07-2007, 08:32 PM
$row_color = ( !($j % 2) ) ? $theme['td_color1'] : $theme['td_color2'];

If the statement in the parentheses is true, $row_color is set to $theme['td_color1'], otherwise it is set to $theme['td_color2'].



'U_FAQ_ID' => $row2[$j]["q_id"]

U_FAQ_ID is an array element, the "=>" is the assignment operator assigning the value $row2[$j]["q_id"] to that array element

tripwater
06-07-2007, 08:44 PM
Thank you so much for the reply. That helped out a lot.

So when assigning the array element why use => instead of =

And also what is the -> used in that example?

kbluhm
06-07-2007, 08:46 PM
So when assigning the array element why use => instead of =
Short answer: because that's how the language was written. :)

=> is the definition of a key/value pair:


<?php

$var = array
(
'one' => 'once',
'two' => 'twice',
);

echo $var['two']; // twice

?>



And also what is the -> used in that example?

-> signifies that the method (aka function) or property (aka variable) belongs to the class of which the object is an instance, and is used to access that method or property:


<?php

/**
* The class
*/
class NameClass
{
private $name = 'John';

public function getName()
{
return $this->name;
}

public function setName( $name = '' )
{
$this->name = ( string ) $name;
}
}


$nameObj = new NameClass;
echo $nameObj->getName(); // John
$nameObj->setName( 'Jim' );
echo $nameObj->getName(); // Jim

?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum