Class 14: PHP

Tasks to complete for this class

  1. Read the content and watch the video lectures here.
  2. Finish PHP Tutorial (opens in a new tab). You should try all the examples, finish all the exercises, and take the PHP Quiz (opens in a new tab)
  3. (Recommended) Watch the following video from Pacific Lynda (opens in a new tab)

PHP Overview

PHP stands for PHP, HyperText Processor, it an open-source programming language that was built specifically to be used as a server-side language. We need to be able to execute commands on the server to be able to provide a dynamic web experience that changes based off of information that is provided by users. Because of this, PHP has lots of cool features meant to leverage server resources. However, the syntax can seem a little strange at times and can also be difficult to debug as it prioritizes uptime and accessibility. PHP often gets a bad rap as not being as cool as more modern frameworks, but it has continued to be a formidable part of the backend experience for many web apps.

Please watch this introductory video (opens in a new tab) to go over the first parts of PHP, how it fits in with what we know about HTML, CSS and web development, and how PHP and other server-side languages have risen and fallen in popularity.

Introduction of PHP continued

The following video (opens in a new tab) continues to discuss PHP as a language, how it was opitimized for server side rendering, and how PHP code is executed on the server before being sent back to the browser. We show an initial version of a PHP script and how PHP will interpret what to do when it is embedded into an HTML page, which allows for an easier point of entry for folks doing server side development.

Important: PHP's documentation (opens in a new tab) is also excellent and worth visiting. For example, I would highly recommend reading PHP's article on what it is and what it can do (opens in a new tab).

Also, here is the page to the explode reference (opens in a new tab) mentioned in the lecture. They not only provide explanations as you expect, but also examples and user contributed notes to help you get a better understanding of php's functions and abilities. It is quite comprehensive and your first source of reference, even more so than w3schools once you start actually coding in PHP!

PHP Basics and embeds

After getting more of the overview, in this video (opens in a new tab) we start running more of the PHP code via webstorm and discuss ideas about equality and php's tags.

Equality

  • Use === over == to check for equality.
    • === checks both equality and ensures both sides have the same type.
    • == has weird behavior because it tries to auto convert types to match. Avoid it.

PHP Tags

  • <?php is the way to start telling the server to start processing information as php.
    • It can be turned off and you can resume interpreting client-side code by typing ?>
  • <?= is a php shorthand tag that automatically has an echo associated with it.

Therefore <?php echo 5?> and <?= 5 ?> are equivalent.

Lastly, while the ?> is normally used to finish any php code, it can be safely omitted if you reach the end of a file.

This is unlike </html> which is needed to be compliant for HTML.

PHP Expressions and Statements

The next video (opens in a new tab) goes over some of the basic portions of PHP, including:

  • how to print and what to know about line breaks
  • php data types
  • arithmetic in PHP and its slight differences
  • how boolean operations use 1 for true and the empty string ("") for false
  • how all variables in PHP are preceded with a dollar sign ($) 🤑🤮
  • how to use the unset and isset function and how null values are represented
  • additional math functions and how we can leverage PHP storm to help us figure out what fucntions are available.

PHP Arrays, Strings, Objects and Loops

The last video for today (opens in a new tab) goes over the aforementioned arrays, strings, objects and loops.

PHP Arrays

Keep in mind that arrays are not fixed like in C++/Java. They are dynamic, which means, no ArrayIndexOutofBoundsExceptions 🥳! They also though, allow different types in the same array. 😦 There are also a few different ways of working with arrays, so it's best to read PHP's official array introduction (opens in a new tab) As well as to look at the multitude of array functions in PHP (opens in a new tab), which includes:

PHP Strings

We can think that Strings in PHP as an array of characters, similar to how we think of strings in Java/C++. You can also use either double-quotes (") or single-quotes (') to declare string values. The difference will be in what characters you can use inside. I will be sticking to double quotes to keep with our previous languages. To concatenate strings though, use ..

I'm also goint to link to the excellent Strings explanation for PHP (opens in a new tab). Strings also has a ton of string functions (opens in a new tab) for you to peruse, including:

PHP Files

Files end up being super important for PHP, as you'll be on a server, there may be files that you want to access!

Use the file function to get an array of strings that contain all of the lines of a file. Here's the reference to the file function (opens in a new tab) as well as a function to check if the file exists (file_exists) (opens in a new tab) as well as a host of other file related functions for PHP (opens in a new tab)

PHP Control Structures

Most of the control structures (opens in a new tab) that you are used to are present in PHP, including:

There is also an alternative syntax for PHP (opens in a new tab) that can be used as shorthand for certain control structures.