You are on page 1of 3

Chapter 2 PHP Fundamentals

<body>
$content
</body>
</html>";

echo $html;
</code>

These are the basic elements needed for a HTML 5 webpage with PHP
included. You are declaring that you want errors to be turned on at the
top, as you have done before. The next lines set two variables, one for the
title and one for the content. The rest of the file sets the variable $html
to the entirety of HTML that you want displayed on the page. Within this
code you see the $title and $content variables placed where you want to
display them on the page. Go ahead and open your browser to this page to
see how it looks. This can get a little redundant if you have many pages that
follow the same look and feel presented with the HTML. Therefore, you
will use this page as a template that you can call and just switch out the
values you want displayed. Open up main2.php in your editor.

<code>
<?php
//error_reporting( E_ALL );
//ini_set( "display_errors",1);
$title = "Beginning PHP 8 & MySQL";
$content = "Here is the main content for this page";
$html =include_once "inc/template2.php";
</code>

Here you are introducing the include_once function. By calling


include_once, you tell PHP that you want to load a specific file into this
area. By separating out the design aspect (HTML) from the PHP, you can
view your code better and reuse the HTML elements in other PHP files.

21
Chapter 2 PHP Fundamentals

Building on this example, let’s take it a step further and include multiple
PHP files in your template. In main2.php, change template.php to
template2.php and refresh the page.
In your case, you first include header.php, located within the inc/
directory. When you use this method of including PHP snippets around
the HTML in your file, you are essentially creating a template. This
template (main.php), if used in another file, will still include a header,
contents, and footer. Let’s create a file called second.php by copying the
main.php file and naming it second.php.
Now that you have come to an understanding with your templates on
how to separate them and use them to your advantage, let’s take a look
at your variables. So far you have been using variables for trivial content,
colors, and item names. What if you want to store someone’s name and
address? It’s perfectly correct to do something like this:

<code>
$firstName = "gunnard";
$lastName = "engebreth";
</code>

This works fine until you want to start passing this information around
between functions in your program. Basically, envision passing your friend
a handful of Skittles vs. a bag of Skittles. Your friend still gets the Skittles in
the end, but one way is clean, optimized, and all Skittles are guaranteed to
reach your friend. This brings us to objects.

Objects
In PHP, an object is a specific set of data as defined in a class. In the code
example above, you would say that the information would belong to a class
of User. You would define that class as such:

<code>

22
Chapter 2 PHP Fundamentals

<?php
   class UserClass
      /* User variables */
      var $firstName;
      var $LastName;

      /* Member functions */


      function setFirstName($firstName){
         $this->firstName = $firstName;
      }

      function getFirstName(){
         echo $this->firstName;
      }

      function setLastName($lastName){
         $this->lastName = $lastName;
      }

      function getLastName(){
         echo $this->lastName;
      }
   }
  </code>

To create a new user you can call


<code>
$user = new UserClass();
$user->setFirstName('gunnard');
$user->setLastName('engebreth');
var_dump($user);
</code>

Let’s take a look at this code.

23

You might also like