Professional Documents
Culture Documents
Chapter 2 PHP Fundamentals
Chapter 2 PHP Fundamentals
<code>
<?php
$title = "main3 php file";
require "UserClass.php";
$user = new UserClass;
$user->setFirstName = 'gunnard';
$user->setLastName = 'engebreth';
var_dump($user);
error_reporting( E_ALL );
ini_set( "display_errors",1);
$html =include_once "inc/template2.php";
</code>
24
Chapter 2 PHP Fundamentals
You have already reviewed what the first two lines do, so set up and set
the variable $user as a part of the class UserClass. The next two lines set
the first name and last name. If you run this script on the command line or
browser, you can verify this through the var_dump() function. Figure 2-2
shows what you should see.
25
Chapter 2 PHP Fundamentals
GET
POST
PUT
DELETE
PATCH
The PATCH method is used to apply partial
modifications to a resource.
You will be focusing on GET and POST but rest assured, you will be using
the others as you build your REST API.
Point your browser to http://localhost:8000/chapter2/main4.
php?pants=123. Notice in the URL you have main4.php?pants=123. When
the page loads, it should look like Figure 2-3.
26
Chapter 2 PHP Fundamentals
At the top is var_dump() and you can see that the info you have in
the URL is now available to you in PHP as the variable $userVars. This is
available to you through the HTTP verb GET and in PHP you use the global
variable $_GET. GET specifically allows for the transfer of data through the
URL. You can send multiple values as well. Change the URL to include
<code>
http://localhost:8000/chapter2/main4.php?pants=123&dog=poodle&f
ood=spaghetti
</code>
Refresh the page and you will now see that Pants, Dog, and Food have
values set to them. The other method of transmitting data from the user to
your code is using POST.
The POST verb behaves in nearly the same way but does not use the
URL, thereby keeping the data you are transmitting a bit more secure. In
order to see the POST functionality, open main5.php and take a look.
<code>
<?php
$userVars = $_POST;
$title = "main5 php file";
require "UserClass.php";
$user = new UserClass;
$user->setFirstName = 'gunnard';
$user->setLastName = 'engebreth';
27