Introduction to PHP

From Knowledge Kitchen
Jump to navigation Jump to search


History

  • created in 1994 for tracking visitors.
  • PHP originally stood for "Personal Home Page"
  • PHP currently now stands for "Hypertext Pre-Processor"
    • hypertext is the general term for documents that link to other documents
    • web servers usually just send web pages to clients, without much "thinking"
    • PHP scripts add the ability to "pre-processes" requests to the server before any data is sent to the client
    • in other words, a programmer can control behavior of the web server so that it can do other things instead of, or in addition to, sending data to the client

Language features

  • PHP has all the same control structures as any other high-level language:
    • variables and literals
    • various data types
    • functions
    • if/else statements
    • while loop and for loops
    • etc. See a brief guide to PHP syntax
  • PHP is a "scripting" language, like Javascript
  • PHP is loosely-typed, like Python and Javascript, and unlike strictly-typed languages, such as C, C++, and Java
  • PHP is a server-side language, while Javascript is most commonly a client-side language
    • This means PHP runs on the server, while Javascript runs on the client
    • This is because most servers have PHP interpreters installed on them, while most clients don't
    • This means you can't "run" PHP code on your personal computer... it must be uploaded to a server
  • PHP is generally interpreted
    • However, PHP can be compiled. This is not common, and is usually only done for very large applications
  • PHP comes with a barebones built-in web framework

Client vs. server

The concept of client and server is used to describe the relationship between two computers (and the programs running on those computers)

  • a client computer makes requests
  • a server computer responds to those requests

Web clients and web servers

Communication between computers on the World Wide Web follows the general client/server paradigm:

  • on the web, the term client refers to the web browser (whatever web browser program you use)
  • on the web, the term server is used to mean the web server where web pages/scripts are stored (i6 is our web server)

How web browsers and web servers communicate

Web browser and web servers talk to each other via a "language" known as HTTP (Hypertext Transfer Protocol) :

  • web browser make requests to web servers via this HTTP language.
  • web servers make responses to web browsers also via the HTTP language.

What a typical HTTP conversation in human terms, might look like

If two actors were to play the roles of a web browser and a web server, their conversation might look something like this:

  • Web browser: "Hey, web server! Give me the file named foo.html."
  • Web server: "Hey, web browser! Ok. Here it is."

What a typical HTTP conversation in human terms, might look like if PHP was used

If two actors were to play the roles of a web browser and a web server running a server-side PHP script, their conversation might look something like this:

  • Web browser: "Hey, web server! Give me the file named foo.html."
  • Web server: "Hey, web browser! Wait a second, dude... Let me think... Ok, I'm going to give you something you'll like even better! Here it is."


Client and server communication.png

Why use PHP?

Server-side programming gives web developers the tools to build web pages dynamically.

  • This means web pages can be assembled "on the fly" every time a request from a client is received by the server.
  • This approach can be more efficient and flexible than working only in HTML

Less code redundancy

Let's say a web site has 100 pages with exactly the same header code.

  • Without PHP (or any other server-side programming language), if a developer wants to change the content or design of that header, they have to make those changes to 100 separate code files:
  • With PHP (or any other server-side programming language), a developer can store that header code in just one file, and automatically have that code inserted into all 100 pages without any duplication of code.
  • This is one example of the validity of Bloomberg's Law, which can formulated as follows: "If you are not lazy, you are not a good programmer".

Templated pages

Let's say an e-commerce web site sells 20,000 products and visitors to the web site can view each product on its own page.

  • Without PHP (or any other server-side programming language), the sorry web developers will have to create 20,000 separate product pages.
  • With PHP (or any other server-side programming language), the developers can create a single product page "template" and automatically insert the data for each product into this template

Tracking visitors

Let's say a social media web site that makes hordes of money selling each user's personal information to advertisers asks users to "log in" on the home page. But if the user has previously logged-in, the home page doesn't ask them to log in again, but simply shows them some personalized content.

  • Without PHP (or any other server-side programming language), this is simply not possible.
  • With PHP (or any other server-side programming language), the server can track visitors and "know" whether any visitor has previously logged in, and show them personalized content, while showing logged-out visitors a log in form.

Database integration

Let's say an dog shelter has a website where visitors can search dogs by breed, age, and gender.

  • Without PHP (or any other server-side programming language), the developers would have to make separate web pages for each breed, for every possible age, and for each gender, and every permutation of each of these search attributes. Whenever a new dog arrives at the shelter, the developers would have to update each of these pages so that the dog would appear in all the relevant searches.
  • With PHP (or any other server-side programming language), all the data about the dogs would be stored in a database. The web site would "know" what search attributes the user had selected, and would be able to make queries to the database for dogs matching those attributes. The data about the matching dogs would then be automatically placed into a web [age "template" by PHP. So there would be no need to update code every time a new dog arrived.

What server-side languages cannot do

PHP is a server-side language....

  • PHP cannot create mouseover effects, make windows or modal dialogs pop up in the browser, or do other client-side tasks because those are client-side tasks and PHP is a server-side language.
  • In general, client-side languages are used to present what you see on a web page, while server-side languages provide the rules behind why you are seeing what you


What links here