Preface

About

This section briefly introduces the coverage of this book, the technical skills you'll need to get started, and the hardware and software requirements required to complete all of the included activities and exercises.

About the Book

You already know you want to learn PHP 7, and a smarter way to learn PHP development is to learn by doing. The PHP Workshop focuses on building up your practical skills so that you can develop cutting-edge, high-performance web applications. It’s ideal if you’re looking to work with an existing application, or even develop your own side project with a PHP framework like Laravel. You'll learn from real examples that lead to real results.

Throughout The PHP Workshop, you'll take an engaging step-by-step approach to understanding PHP development. You won't have to sit through any unnecessary theory. If you're short on time you can jump into a single exercise each day or spend an entire weekend learning about third-party libraries. It's your choice. Learning on your terms, you'll build up and reinforce key skills in a way that feels rewarding.

Every physical copy of The PHP Workshop unlocks access to the interactive edition. With videos detailing all exercises and activities, you'll always have a guided solution. You can also benchmark yourself against assessments, track progress, and receive content updates. You'll even earn a secure credential that you can share and verify online upon completion. It's a premium learning experience that's included with your printed copy. To redeem, follow the instructions located at the start of your PHP guide.

Fast-paced and direct, The PHP Workshop is the ideal companion for PHP beginners. You'll build and iterate on your code like a software developer, learning along the way. This process means that you'll find that your new skills stick, embedded as best practice. A solid foundation for the years ahead.

About the Chapters

Chapter 1, Introducing PHP, introduces you to the PHP language, enabling you to set up your first development environment and write your first PHP scripts.

Chapter 2, Types and Operators, introduces the different types used in PHP programming.

Chapter 3, Control Statements, defines different branching and looping techniques and scenarios for the use of different control structures and conditions with operators.

Chapter 4, Functions, looks at functions and the difference between built-in functions and custom functions, as well as exploring the callback function.

Chapter 5, Object-Oriented Programming, explains everything you need to know to have a solid foundational knowledge of object-oriented programming for PHP. You will learn about interfaces, classes, namespaces, class instantiation, class field scopes, methods, magic methods, abstraction, inheritance, object composition, autoloading, and more.

Chapter 6, Using HTTP, explores HTTP requests, which are vital to understand and use in practical web applications. You will become familiar with request types and URL components, find out about common vulnerabilities on the World Wide Web and learn how to protect your application against attacks that exploit those vulnerabilities.

Chapter 7, Data Persistence, describes the utilization of databases, including coverage of their configuration and read and write operations.

Chapter 8, Error Handling, explains error levels and exceptions in PHP, including when they trigger, how they can be triggered, and also—very importantly—how to handle them when they occur.

Chapter 9, Composer, explains how to use the Composer dependency management tool and how to autoload dependencies into PHP scripts.

Chapter 10, Web Services, defines the ways of talking between different platforms by exchanging data.

Conventions

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows:

"The echo construct is one way to print to the screen.".

Words that you see on the screen, for example, in menus or dialog boxes, also appear in the text like this: "Open Insomnia and click on the New Request button."

A block of code is set as follows:

<?php

$language = "PHP";

$version = 7.3;

echo $language;

echo $version;

?>

New terms and important words are shown like this: "Welcome to the world of Hypertext Preprocessor (PHP).".

Long code snippets are truncated and the corresponding names of the code files on GitHub are placed at the top of the truncated code. The permalinks to the entire code are placed below the code snippet. It should look as follows:

Example1.01.php

1 <!DOCTYPE html>

2 <html lang="en">

3 <head>

4     <meta charset="UTF-8">

5     <meta name="viewport" content="width=device-width, initial-scale=1.0">

6     <meta http-equiv="X-UA-Compatible" content="ie=edge">

7    <title>My First PHP Page</title>

8 </head>

Before You Begin

Each great journey begins with a humble step. Our upcoming adventure in the land of PHP is no exception. Before we can do awesome things with data, we need to be prepared with a productive environment. In this section, we shall see how to do that.

Installing PHP 7.3 (Ubuntu)

All the exercises in this book were run with PHP 7.3–the latest stable PHP version—on Linux Ubuntu 18.10. Since PHP is cross-platform, you can use it on Windows version 7+ (Visual Studio 2015 required) and macOS as well.

Ubuntu 18.04 LTS ships with PHP 7.2 by default, so in order to install the latest stable PHP version, you should compile from source or install precompiled packages on your machine. Installing precompiled packages from trusted sources is often preferred since the time for installation is much lower than that for compiling from source code. In your Terminal, run the following (one line at a time, with superuser privileges):

apt-get update

apt-get install -y software-properties-common

LC_ALL=C.UTF-8 add-apt-repository -y ppa:ondrej/php

apt-get update

apt-get install -y php7.3-common php7.3-curl php7.3-mbstring php7.3-mysql

Installing PHP 7.3 (Windows)

Here are the steps to install PHP 7.3 on a Windows system:

  1. Download the latest PHP 7 (non-thread safe version) ZIP file from https://packt.live/2MBLz4R:
    Figure 0.1: Downloading PHP 7

    Figure 0.1: Downloading PHP 7

  2. Extract the contents of the ZIP file into C:PHP7.
  3. Copy the C:PHP7php.ini-development file to C:PHP7php.ini.
  4. Open the newly copied C:PHP7php.ini file in a text editor, such as Notepad++, Atom, or Sublime Text.
  5. Change memory_limit from 128M to 1G (using Composer may require more memory).
  6. Search for extension_dir and uncomment the line (remove the leading semicolon, so the line will look like extension_dir = "ext").
  7. To add C:PHP7 to the Windows 10 system path environment variable, open the Control Panel and click on View advanced system settings:
    Figure 0.2: Checking for advanced system settings

    Figure 0.2: Checking for advanced system settings

  8. Click the Environment Variables… button:
    Figure 0.3: Checking environment variables

    Figure 0.3: Checking environment variables

  9. Click on the Path row under System variables, and then click on Edit…:
    Figure 0.4: Editing the variable

    Figure 0.4: Editing the variable

  10. Click New and add the C:PHP7 row:
    Figure 0.5: Adding a new row

    Figure 0.5: Adding a new row

    Click OK for all opened windows so far and close the Control Panel.

  11. In a Command Prompt (PowerShell or another Terminal), test that the installation is successful by typing php -v:
Figure 0.6: Testing the installation

Figure 0.6: Testing the installation

Installing MySQL 5.7 (Ubuntu)

To install MySQL 5.7 on your system, run the following in your Terminal:

apt-get update

apt-get install -y mysql-server

Accessing MySQL as Root (Using sudo)

To access MySQL as a root user, run the following command in your Terminal:

sudo mysql --user=root

Creating a Test User

To create a test user, run the following command in your Terminal:

create user 'php-user'@'%' identified by 'php-pass';

Granting all Privileges on a Test User

To grant all privileges to a test user, run the following command in your Terminal:

grant all on *.* to 'php-user'@'%';

flush privileges;

In a production environment, you would carefully pick the required-by-the-app privileges only, restricting the range of privileges as much as possible. For more information about privileges on MySQL servers, visit https://packt.live/2N5e6yC.

Installing MySQL Workbench

Open the software manager, search for MySQL Workbench, and click on the Install button.

Installing MySQL 5.7 (Windows)

  1. Access https://packt.live/31BCHAj.
  2. Click on the Looking for previous GA versions? link from the following download box:
    Figure 0.7: MySQL installer

    Figure 0.7: MySQL installer

  3. Pick the latest 5.7 version for Windows and click on the Download button:
    Figure 0.8: Downloading the appropriate version

    Figure 0.8: Downloading the appropriate version

  4. Run the downloaded file in order to install the MySQL Workbench.
  5. Pick Developer Default (includes the MySQL Workbench as well) and click Next:
    Figure 0.9: Selecting the appropriate Setup Type

    Figure 0.9: Selecting the appropriate Setup Type

  6. Click Execute to install the dependencies, and then click Next.
  7. Click on Execute to start the download and install the selected components (click on Try again if the download or installation fails):

    Figure 0.10: Installing the selected components

  8. Click Next and Finish until you come to the MySQL Configuration window Account and Roles prompt; enter a root user password.
  9. Click on the Add User button and enter php_user for the username and php-pass for the password, and click OK:
    Figure 0.11: Entering the credentials

    Figure 0.11: Entering the credentials

  10. Hit Next and Execute until the installation process is complete.

Installing Composer

To install Composer on Ubuntu or Mac, you will need to go to https://packt.live/33PWz4i and run the four PHP commands under the Command-line installation section in the given link. There is an encryption code included in the commands to verify the download for security purposes. The commands, at the time of writing, are included as follows:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

php -r "if (hash_file('sha384', 'composer-setup.php') === '48e3236262b34d30969dca3c37281b3b4bbe3221bda826ac6a9a62d6444 cdb0dcd0615698a5cbe587c3f0fe57a54d8f5') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

php composer-setup.php

php -r "unlink('composer-setup.php');"

On Windows, you can just download the installer file from https://packt.live/31FPC49.

Installing the Insomnia REST Client

Browse to https://packt.live/35PWhfH and download the installer file appropriate for your operating system. Open the installer and complete the installation wizard by selecting the default options.

Installing the Code Bundle

Download the code files from GitHub at https://packt.live/33T57aw and place them in a new folder called C:Code. Refer to these code files for the complete code bundle. Also, if you have Git installed on your machine, you can check out the code as well by using the following command:

git clone [email protected]:PacktWorkshops/The-PHP-Workshop.git

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.253.221