Page 88 - Open Soource Technologies 304.indd
P. 88

Unit 6: Building Blocks of PHP



            }                                                                                     Notes
            The output of this example is

            The players are:

            #0 = John
            #1 = Barbara

            #2 = Bill

            #3 = Nancy
            Here’s  a more  complicated  example  that iterates over  an array of  people and  marks  which
            person is considered old and which one is considered young:

            $people = array(1 => array(“name” => “John”, “age” => 28),
            →array(“name” => “Barbara”, “age” => 67));

            foreach ($people as and $person) {
            if ($person[“age”] >= 35) {

            $person[“age group”] = “Old”;

            } else {
            $person[“age group”] = “Young”;

            }

            }
            print_r($people);

            Again, this code makes use of the print_r() function.

            The output of the previous code is the following:
            Array

            (
            [1] => Array

            (

            [name] => John
            [age] => 28

            [age group] => Young
            )

            [2] => Array

            (




                                             LOVELY PROFESSIONAL UNIVERSITY                                    83
   83   84   85   86   87   88   89   90   91   92   93