Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: syntax question

  1. #1
    Regular Coder
    Join Date
    May 2005
    Posts
    262
    Thanks
    4
    Thanked 0 Times in 0 Posts

    syntax question

    I found this code in a forum scrip and I was wondering what the various parts are doing and what it means.

    Code:
    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
    Code:
    $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.

  • #2
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    PHP Code:
    $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'].

    PHP Code:
    '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

  • #3
    Regular Coder
    Join Date
    May 2005
    Posts
    262
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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?
    Last edited by tripwater; 06-07-2007 at 08:56 PM.

  • #4
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Quote Originally Posted by tripwater View Post
    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 Code:
    <?php

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

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

    ?>
    Quote Originally Posted by tripwater View Post
    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 Code:
    <?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

    ?>
    Last edited by kbluhm; 06-08-2007 at 02:33 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •