title: How to use a foreach loop to echo first name, last name, and email address objects in an array in PHP and along with HTML
body: <p>I am new to PHP and have been struggling with echoing out a first name, last name, and email address as objects inside of a foreach loop. I have used a Profile.php class to define those and the get and set methods, and then I have a User.php class to set up a Username and Profile object and its get and set methods. I don't use the username object at all for my answer, according to people I have tried getting help from. I believe that the problem is within the foreach loop in index.php, but I have worked for hours and cannot wrap my head around it yet. 
  <a href="https://i.stack.imgur.com/i5KEf.png" rel="nofollow noreferrer">Here is the UML for the program.</a></p>
  
  <p><a href="https://i.stack.imgur.com/ztVzq.png" rel="nofollow noreferrer">Here is what the program should ouput.</a></p>
  
  <p>And here is the code itself:</p>
  
  <p>index.php:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
  
          &lt;?php
  
          require_once('User.php');
          require_once('Profile.php');
  
          $professors = array("Andrew Besmer", "Gerry Derksen");
  
          $andrewUser = new User();
          $andrew = new Profile();
  
          $andrewUser-&gt;setProfile($andrew);
  
          $andrew-&gt;setFirstName("Andrew");
          $andrew-&gt;setLastName("Besmer");
          $andrew-&gt;setEmailAddress("besmera@fakeemail.com");
  
          $gerryUser = new User();
          $gerry = new Profile();
  
          $gerryUser-&gt;setProfile($gerry);
  
          $gerry-&gt;setFirstName("Gerry");
          $gerry-&gt;setLastName("Derksen");
          $gerry-&gt;setEmailAddress("derkseng@fakeemail.com");
  
          foreach($professors as $profile) {
              echo "&lt;h2&gt;" . $profile . "&lt;/h2&gt;";
              echo "&lt;ul&gt;";
                  echo "&lt;li&gt;&lt;b&gt;First:&lt;/b&gt;" . $profile-&gt;getFirstName() . "&lt;/li&gt;";
                  echo "&lt;li&gt;&lt;b&gt;Last:&lt;/b&gt;" . $profile-&gt;getLastName() . "&lt;/li&gt;";
                  echo "&lt;li&gt;&lt;b&gt;Email:&lt;/b&gt;" . $profile-&gt;getEmailAddress() . "&lt;/li&gt;";
              echo "&lt;/ul&gt;";
          }
          echo "&lt;br&gt;";
          ?&gt;
  
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Profile.php:</p>
  
  <pre><code>&lt;?php
  class Profile { 
      protected $firstName = "";
      protected $lastName = "";
      protected $emailAddress = "";
  
      public function __construct(){
  
      }
  
      public function getFirstName(){
          return $this-&gt;firstName;
      }
  
      public function setFirstName($firstName){
          $this-&gt;firstName = $firstName;
      }
  
      public function getLastName() {
          return $this-&gt;lastName;
      }
  
      public function setLastName($lastName) {
          $this-&gt;lastName = $lastName;
      }
  
      public function getEmailAddress() {
          return $this-&gt;emailAddress;
      }
  
      public function setEmailAddress($emailAddress) {
          $this-&gt;emailAddress = $emailAddress;
      }
  }
  ?&gt;
  </code></pre>
  
  <p>User.php:</p>
  
  <p>
  
  <pre><code>class User {
  
  protected $username = "";
  protected $professors = array();
  
  public function __construct(){
  
  }
  
  public function getUsername(){
      return $this-&gt;username;
  }
  
  public function setUsername($username) {
      $this-&gt;username = $username;
  }
  
  public function getProfile(){
      return $this-&gt;profile;
  }
  
  public function setProfile($profile){
      $this-&gt;profile = $profile;
    }
  }
  </code></pre>
  
  <p>Thanks for the help and your time.</p>