1 of 32

ISTE - 240 Week 8

Intro to PHP

2 of 32

HTML Document Request Workflow

Firsts here is a reminder of how we have been doing things (web and mobile 1, and up to now in web and mobile 2)

3 of 32

4 of 32

5 of 32

6 of 32

7 of 32

PHP Document Request Workflow

Firsts here is a reminder of how we have been doing things (web and mobile 1, and up to now in web and mobile 2)

8 of 32

9 of 32

10 of 32

11 of 32

12 of 32

13 of 32

14 of 32

15 of 32

Hello World PHP Script

Question: What is the problem here?

16 of 32

Hello World PHP Script

Answer: It wasn’t uploaded to a server!

17 of 32

Hello World PHP Script

Now that we moved it to a server that supports PHP it should work

18 of 32

  • When the page is read by the server and PHP directives are found (lines 7-9)���
  • Apache reads the PHP, and in this case puts out the string “My first PHP script” in this exact place in the code.

Hello World PHP Script

19 of 32

  • when I ‘view source’ on the rendered page from Figure 2 – I will see the following:

��

  • Note you don’t see any of the <?php or ?> or echo. In fact, when the browser gets the page and renders it, there will be NO EVIDENCE of any PHP ever. The only way the client knows it might have had PHP in it is by the extension index.php.

Hello World PHP Script

20 of 32

  • I can have more than one PHP directive first one is on lines 1-5, second on 9, third on 12-14)
  • The directives will be parsed in PHP by Apache as they are read in line by line.
    • First directive (1-5) is processed before the second, etc.
  • PHP is very picky about its line termination (semicolon - ;).
  • ALL variables start with the $ (dollar sign)!
    • Your choice what naming convention you want to use with PHP (either is fine with me)
      • $f_name
      • $fName
    • Environmental variables as well ($_GET, $_POST, $_SERVER, etc) – but they are coming up next…
  • The dot or period (see line 9) is for concatenation. Leave it off and you’ll get an error!
  • If there is no output from the directive (lines 1-5), then the final output on the client won’t even show up!
      • Note that lines 1-5 on Figure 6 completely disappear – because there is no output from them!

PHP Parsing and Variables

21 of 32

  • $_SERVER
    • Server and execution environment information
  • $_GET
    • HTTP GET variables (if they were sent)
  • $_POST
    • HTTP POST variables (if they were sent)
  • $_FILES
    • HTTP File Upload variables (if any were sent)
  • $_REQUEST
    • HTTP Request variables (inclusive of all $_GET, $_POST and $_COOKIE variables)
    • As you mature as a coder, you will realize you should NOT USE this. Beyond it being lazy, it can be a security issue.
  • $_SESSION
    • Session variables
    • Way to keep variables from one page to another (AWESOME!). More on this in a couple of weeks.
  • $_ENV
    • Environment variables
  • $_COOKIE
    • HTTP Cookies (if there are any)

Predefined ‘environmental’ Variables

22 of 32

Predefined ‘environmental’ Variables

Notice the use of var_dump? This can often times be used for debugging to print out the structure of an array or object.

23 of 32

Predefined ‘environmental’ Variables

Line 14 – there is an echo of a hr tag

Line 15 – there is an echo of the $_SERVER variable

Line 16, I ended the PHP directive (started on line 12)

Line 17 – a simple <hr/> tag

Line 18 – restarted a new PHP directive

Line 19 – echo out the variable dump of $_SERVER

24 of 32

Predefined ‘environmental’ Variables

Line 19 – echo out the variable dump of $_SERVER

  • the $_SERVER array has 79 members (or 79 different name/value pairs – in Java that is known as a ‘Hash’ – in PHP it is known as an ‘Associative Array’). If you look down the list, you would see: [“SERVER_PORT”]=> string(3) “443” – that tells us that at the name location of “SERVER_PORT”, it is holding a string (size of 3) the value of is 443
  • If I just wanted to echo out that port – I could have written:

echo “My port is: “ . $_SERVER[“SERVER_PORT”];

Make sense? I can access any of the 79 by its name. This approach for accessing information from an associative array will also be how we get data from databases – so it is good to start seeing it now!

25 of 32

GET & POST Environmental Variables

  • Let’s go get one more of these predefined variables – the $_GET. If we remember our ‘get’ and ‘post’ from forms, the ‘get’ is added to the URL bar and the ‘post’ is sent as a separate file.
  • we should test to see if there is anything in the $_GET in the first place…

Line 22 to 26 is a simple if statement – but 22 is looking for something specific. It is looking to the URL to see if there is a GET with the name of ‘test’ that was included. The isset() method is a predefined PHP method (color coding is your friend) that checks if it is there at all.

26 of 32

GET & POST Environmental Variables

VERSUS

COMPARE:

1

2

Notice the URL argument we have here? We will go over this in more depth in later weeks.

27 of 32

Other PHP examples

Some Handy Built in PHP functions: phpinfo()

28 of 32

<!DOCTYPE html>�<html>�<body>��<?php�$t = date("H");��if ($t <  "20") {�     echo "Have a good day!";�} else {�     echo "Have a good night!";�}�?>�  �</body>�</html>

Other PHP examples

Some Handy Built in PHP functions: Date()

29 of 32

Other PHP examples

Some Handy Built in PHP functions: Arrays and explode / implode

30 of 32

Other PHP examples

Some Handy Built in PHP functions: Arrays and explode / implode

Source: https://www.c-sharpcorner.com/UploadFile/051e29/implode-and-explode-function-in-php/

31 of 32

Other PHP examples

Some Handy Built in PHP functions: Arrays and explode / implode

Source: https://www.c-sharpcorner.com/UploadFile/051e29/implode-and-explode-function-in-php/

32 of 32

Additional Time Left Over?

Time for Questions and More Demos