CS代写

This is a single, concatenated file, suitable for printing or saving as a PDF for offline viewing. Please note that some animations or images may not work.

Copyright By PowCoder代写 加微信 powcoder

Module 4 Study Guide and Deliverables Readings: Murach’s PHP and MySQL: Chapters 1-4 & 16-18
Assignments:
Assignment 4 Due: Thursday, February 10 at 6:00 AM ET

Term Project:
Term Project Milestone: Project Selections Due Thursday, February 10 at 6:00 AM ET

Live Classrooms: Thursday, February 3, 6:00–8:00 PM ET
Facilitator live office: TBD

Introduction to PHP and MySQL

Learning Objectives

By reading the lectures and textbook, participating in the discussions, and completing the assignments, you will be able to do the following:

Differentiate the use cases of PHP.
Write PHP scripts using variables, functions, conditionals, and loops.
Identify various PHP data types and structures.
Create MySQL databases and tables.
Perform CRUD (Create, Read, Update, and Delete) tasks with PHP and MySQL.
Upload files to a web server.

Introduction

PHP is a server-side scripting language that is used to create dynamic websites. In the vast majority of cases, PHP is used with a database. In this lecture, you will learn how to create PHP scripts that interact with MySQL databases. We won’t cover every feature and attribute of the PHP programming language in this course, but we will cover the most common ones and even some selected areas of advanced functionality.

NOTE:The lectures found in this module and in subsequent ones will assume that you have some level of programming knowledge. You don’t need to be an expert, but you should understand the basic programming concepts such as variables, data types, functions, conditionals, loops, etc.

PHP has syntax very similar to C, JavaScript, and other languages derived from C.

There is a lot of information to learn, but it’s going to be fun!

Why Use PHP?

As outlined in the previous lecture, PHP is a server-side scripting language. It is an interpreted language and therefor does not need to be compiled before execution. PHP is open-source and free software. It is cross platform, meaning you can develop, deploy, and use it on Windows, Mac, and Linux machines.

PHP is powerful, scalable, and very robust. Many large websites such as Facebook, Yahoo, and Wikipedia use PHP for their server-side development language. It was developed with web development in mind.

PHP is fully object oriented since version five. It has great documentation and has a large and very active developer community. You will certainly need to refer to the PHP documentation numerous times as you work through examples and assignments in this course. It is suggested that you bookmark the site and even keep a browser tab open with that page so it is readily accessible to you as you are working on your PHP projects.

To learn more about the capabilities of PHP, be sure to view this article What Can PHP Do?

History of PHP

PHP came about as a programming/scripting language when needed a set of tools to manage his personal home page in 1994. The set of tools he created at the time were extremely tiny compared to the features that PHP offers today. He coined the scripts he created as “Personal Home Page Tools” or PHP.

In 1995, version 2 was created and released to the public. Over the years much work has been put into PHP, which really started to accelerate in 1998 with the release of version 3. and really took over much of the development of PHP and recoined it as PHP: Hypertext Preprocessor (a recursive acronym, geek humor).

Version 4 was released in 2000 in which Zend rewrote the core of PHP that resulted in a more modular Zend Engine 1.

Version 5 was released in 2004. This version introduced Zend Engine 2, which offered better performance, and improved OOP support. This is the latest major and stable release and the version we will be using in this course.

Version 6 was a failed initiative and development has stopped.

Version 7 is currently being developed now.

PHP Requirements

There are a few components you will need in order to start writing PHP code and to then run/parse it.

Plain Text Editor or Integrated Development Environment (IDE)

This will serve as your development environment for writing, testing, and debugging your PHP code.

Some examples include:

TextEdit (included with OS)
Sublime Text
Eclipse or

Notepad (included with OS)
Sublime Text
E Text Editor
Eclipse or

There are other viable choices as well. Just be sure to select a plain text editor or IDE and stay away from word processing applications such as Microsoft Word. It is highly recommended that you choose a solution that includes the following features:

Line numbers
Code coloring/syntax highlighting
Ability to navigate between multiple files within the editor
Strong search/replace features
Automatic auto-pairing (brackets, parenthesis, quotes)

Web Browser(s)

You’ll need the latest versions of one or more (preferably 2+) web browsers such as:

Google Chrome
Mozilla Firefox

Microsoft Internet Explorer

Web Server running PHP/MySQL

 A web server will be needed to host your files and to work with the PHP engine in order to parse and return your PHP code as HTML. While there are various types of web servers available, we recommend that you use the Apache web server for this course. You will want to ensure that a later version of PHP is installed such as PHP 5.4 or greater. The latest version at the time of this writing is version 5.6.11 and it is recommended that you use PHP 5.6.x or greater.

Important: Even though PHP 5.4+ has a built in web server, Apache should still be used.

In addition to the web server and PHP, you’ll want to ensure that the server also has access to a database server. For the first part of this course, we will be using MySQL.

There are a few different ways to setup or obtain access to a web server meeting the requirements above.

Install it all on your own on your personal computer or a virtual machine (not recommended).

– Read more information about setting up a web server.

Use an existing installer that sets up and configures Apache, PHP, and MySQL to all work together. (Recommended).

– XAMPP works on all operating systems, LAMP is for Linux, WAMP is for Windows, and MAMP is for Mac.

Sign up for a hosting account online.

– Be sure that the service offers the requirements listed above. Your instructor will share recommendations with you and it is highly recommended that you don’t signup for any hosting service until you receive instructions from your instructor.

Other Software

Other software might be useful while writing PHP code. If you are hosting your files on a remote web server, you’ll probably want to use a FTP client such as Filezilla to move the files from your local machine to the server. There are other options as well which will be shared by your instructor.

Timely Setup is Critical!

It is very important that you quickly get your programming environment setup in order to write and execute your PHP applications. This course moves very quickly and a huge amount of information is covered in six short weeks. You’ll need the environment setup within the first day or two of this course starting.

Your First PHP Page

Now that you have learned what tools you need in order to write and execute PHP scripts, let’s write our first bit of PHP.

Start off by creating a basic HTML page, ensuring that it uses valid HTML syntax (https://validator.w3.org/). We’ll be using HTML 5 code examples in the course.

Important: Instead of saving this file with a .html or .htm extension, be sure to save it with a .php extension. In this example, I am naming the file: hello.php

We are now going to start adding PHP code to our web page. To do this, we need to add starting and ending tags so the web server knows where the PHP is on the page so it can parse it and return HTML to the web browser. There are multiple ways to indicate the start and end of PHP code. The method we will use in this course looks like this:

You may see other starting and ending tags for PHP that are known as short-open tags. Here are a few examples:

And even …

We won’t be using short-open tags in this course as they are considered to be bad form.

You may also run into some PHP code that was written using ASP style-tags:

One major issue with using ASP style tags is that it requires a special setting to be enabled in the server’s php.ini file. You can’t count on other systems having this enabled so this greatly reduces the portability of your code. It is recommended that you don’t use this style of opening and closing tags in your PHP projects.

Let’s look at the code we will use for our first PHP page, please continue to follow along in your text editor or IDE. Be sure to save the file to your web server’s document root directory using the file manager feature in the hosting control panel or with a FTP client.

You can see that we used a classic introductory programming example for our first PHP page. The echo command is similar to console.log in JavaScript or the print() function in other languages. It simply echoes or prints the statement to standard output, which in this case is the web browser and we’ve wrapped it in a HTML

tag.

TIP: Since the result of parsing PHP is HTML code, you can also insert HTML tags within your PHP code. In the previous example we could have put the opening and closing

tags within the echo statement like this:

You may have also noticed that I switched from double quotes ( ” ) to single quotes ( ‘ ) in that last code sample. We’ll discuss the differences in an upcoming section on Strings. For this example, the result is the same.

Here is how the code should render in your browser:

And this is what it should look like when you view the source for this page via the browser. You’ll notice that you don’t see any PHP code; instead, it just shows HTML:

Congratulations! If you have achieved the same result, you have successfully written your first PHP page!

Here are some important things to keep in mind:

Whitespace doesn’t matter. PHP ignores it.
In most cases, every statement needs to end with a semi-colon.
Web files with PHP code embedded within them must have a .php file extension.

PHP Comments

As with other programming languages, adding comments to your source code has many benefits. It let’s you provide documentation for others and yourself that can be referred to in the future. This helps the next person looking at the source code understand what certain lines of code are intended to do and can also refresh your memory on your approach to solving a problem or implementing a solution.

IMPORTANT!

You will be required to provide comments within your programming assignments for any code that is not self-documenting or explicitly clear.

Here are a few examples of using comments in PHP:

As you will see in the screenshots below, the comments do not render in the browser window or when you view the source via the web browser. This is a bit different from HTML and JavaScript comments where the comments don’t render on the page but do show up when viewing the source code. This is an important distinction to remember.

Variables are a symbolic representation for a value that can be changed overtime. PHP is a loosely typed language, meaning you don’t need to declare what type of data the variable will contain. This allows you to initialize a variable that stores a string and then change it to hold a number type such an integer or float later on. That’s probably not a good idea, but it can be done.

Here are a couple of examples of declaring and initializing variables in PHP:

You can see that during initialization, we assigned values to the variables using the equals sign (=). This is known as the assignment operator in PHP. Whatever is on the right side of the equals sign gets assigned to the variable on the left side of the equals sign.

We will talk more about the data types available in PHP in an upcoming section.

Variable Names

PHP has some pretty specific rules on naming variables:

They must start with: $
The $ can be followed with a letter or underscore
They can contain letters, numbers, underscores, and dashes
They can not contain spaces
For variable names with more than one word, it is recommended that you use camelCase notation or underscores to signify the start of a new word.

They are case-sensitive

There are some names that are reserved by PHP and you can’t use them for your own variable names. These are known as reserved words. You can view all of the reserved words in PHP.

So here are some examples of acceptable variable names:

And here are a couple of examples of unacceptable variable names. If you use an advanced text editor or IDE that supports PHP syntax, you’ll get an error/warning message and/or the syntax coloring will be not as expected.

As the name implies, the value of a constant can’t change while the script is being executed. While constants are case-sensitive, the standard naming convention is to use ALL CAPS and to not prefix the name with a $.

The syntax for initializing a constant is also different from initializing a variable. Let’s take a look at an example:

Data Types

PHP supports the following data types:

A sequence of characters such as: “I like PHP.” They are typically contained in single quotes and double quotes, but there are other ways to represent them as well.
Single quoted strings will display most things literally, meaning as they are written. Variables are not interpreted within single quoted strings.
Double quoted strings do in fact interpret the variables contained within them.

A whole number without a decimal point, like 10. They can also be represented in hex, octal, and binary notation. They can hold positive or negative values.

Floating point number (float or double)
A number with precision or with a decimal point: 3.14. They can be represented in scientific notation as well. As always, floating-point numbers have limited precision. You may get some unexpected results depending on how they are used. These too can be positive or negative in value.

These represent TRUE or FALSE. They can be represented as a literal and are not case-sensitive.

We’ll discuss this in a section dedicated to Arrays

We’ll discuss objects in the next module.

This is a special variable that represents a variable with no value. It is not case sensitive.

This is another special variable that holds a reference to an external source. They are used by special functions. The use of this type of variable will likely be limited in this course.

You can read more about the eight primitive data types available in PHP.

An array is a data structure that can store multiple values in a single variable. These multiple values are referred to as elements and each element is identified by an index or key. You’ll often hear the term “key-value” pair. This refers to the matching sets of the key position and the value contained at that spot in the array. You can think of an array as a type of list.

Note: A data structure is a particular format for storing and organizing data.

There are three different types of arrays in PHP:

Indexed arrays
Sometimes referred to as a numeric array, but this doesn’t mean that it only stores numbers. It means that the indices of the array are a number and values are stored and accessed in a linear manner. They can store anything like numbers, strings, or objects. The default array index starts at zero.

Associative arrays
An associative array uses a string as the index. In this case, the index is known as a named key.

Multidimensional arrays
This is an array that contains other arrays

We are going to share a code example for each type of array here and we will use arrays a bit more when we talk about foreach loops later on in this lecture.

Indexed Array Examples

Here is what is displayed in the browser as a result:

Associative Array Examples

Here is what is displayed in the browser as a result:

Multidimensional Array Examples

Here is what is displayed in the browser as a result:

You can see that in the multi-dimensional array example, it was a bit tricky to keep track of all of the pieces, so the formatting used in the example above makes things a bit more readable. Finally, it was a bit tedious to print out all of the information for all three courses. We’ll learn a more efficient way to do this when we learn about loops later on in this lecture.

Type Juggling

As we discussed in a previous section, PHP is a loosely typed language. We don’t have to specify our data type when declaring our variables, but this doesn’t mean that the variable does not have a data type. The data type of a variable is determined by the contents stored within the variable. One example of this to create a variable and assign a String value to it and then change the value to an integer with a new assignment statement. PHP automatically changes the data type of the variable.

In some cases, we might end up converting the type of a variable without using the assignment operator to give it a new value. This can happen through type casting and type juggling. We’ll talk about type casting in the next section. In this section, we will focus in on type juggling.

Type juggling is PHP’s automatic way of doing type conversion. There are a few cases in which this will happen. Here is one example:

This is what we will see when the code is executed and viewed in the browser:

You can see that we didn’t get an error when we tried to add a String and an integer value together. Instead, PHP converted the String to an integer and returned the result, which was an integer.

Let’s take a look at another example.

This is what we will see when the code is executed and viewed in the browser:

In the previous example, we introduced a special String operator known as the concatenation operator. This is represented as a period (.) in PHP. This returns the concatenation of its right and left operator. In other words, it joins or adds them together.

So we concatenated a String with a float and then concatenated all of that with another String. PHP converted the float to a String, so the entire thing resulted in a String. We used a PHP function named gettype() to return the type of the variable. We’ll learn more about functions later on in this lecture.

A simple way to summarize type juggling is that when PHP does automatic type conversion, it has been juggled. You shouldn’t rely on PHP to do a lot of type juggling as it is considered to be a sloppy practice.

In the next section we will discuss how we can explicitly convert variable types on our own volition.

Type Casting

In the previous section, we demonstrated how PHP can automatically convert variables to a different type when needed. In this section, we will explore how we can do this ourselves with type casting.

There are two ways to do type casting in PHP. Here is an example that shows both methods:

Here is the result:

Let’s discuss what happened here. On line 12 we put (float) in front of $price during the operation. When the statement is evaluated and returns the value, we also echo out the data type, which is a double. A double is the same thing as a float in PHP. The way in which we did this lets us convert it while it is being used, but without changing or mutating the variable type. You see that after we output $price back to the screen and get it’s type.

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com