Skip to content

Final Exam Study Guide

Notes

  • Not everything listed here may be on the exam
  • You will be able to use notes and your Virtual Machine to test and verify code
  • You will be given two hours of class time to answer the questions and will turn in the results on BlackBoard

Examples and things to consider

  • Be able to explain these functions and how the affect the variables in the script.

e2sample1.php

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
    $friend = "Sam";
    $friend2 = who();
    echo 'My friends are: ', $friend, ' ', $friend2, '<br />';

    function who() 
    {
        $friend = "Joe";
        return $friend;
    }

    $friend3 = who();
    echo 'My friends are: '. $friend .' '. $friend3 .'<br />';


    function raise_sal() 
    {
        global $salary;
        $salary *= 1.1;
    }

    $salary = 50000;
    raise_sal();
    echo 'Congratulations! Your new salary is: $'. $salary .'<br />';
  • In the following code know what each of the MySQL specific functions purpose is.

e2sample2.php

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
    // Sample2 Script

    $link = mysqli_connect('itins3.matcmadison.edu','astudent','phpmysql', 'northwind');

    if (!$link)
    {
        exit('Connection to MySQL server failed!');
    }

    $query = 'SELECT CompanyName, Phone from Shippers';
    $result = mysqli_query($link, $query);

    if (!$result)
    {
        die('Query failed: '. mysqli_error($link));
    }

    $record = mysqli_fetch_assoc($result);

    foreach ($record as $key => $value)
    {
    ?>
        <?= "$key: $value" ?></br>
    <?php
    }

    echo '<br />';

    mysqli_close($link);
  • In the following code know that the regular expression is looking for and what results will be returned from the two regular expression functions.

NOTE: Look up items in the PHP Manual if you're not sure what they are or how they work!

e2sample3.php

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<html>
 <head>
  <title>Sample3 Script</title>
 </head>
 <body bgcolor="lavender">
    <?php
        $regex = '/pat/i';

        $search_array = array('Fitzpatrick','Peggy','Patrick',
                'Patricia','Johnathon');
        $newArray = preg_grep($regex, $search_array);

        echo '<pre>Found '. count($newArray)." matches\n";
        print_r($newArray);

        $newArray = preg_grep($regex, $search_array, PREG_GREP_INVERT);

        echo 'Found '. count($newArray)." that didn't match\n";
        print_r($newArray);
        echo '</pre>;
    ?>
 </body>
</html>
  • Given the following contents in the text file e2sample4.txt

e2sample4.txt

1
2
3
4
Steve Blenheim 100
Betty Boop 200
Igor Chevsky 300
Norma Cord 400
  • What would the following script produce for output? What is the regular expression doing?

e2sample4.php

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
<?php
    // Sample4 Script
    $fh = fopen('sample4.txt','r');

    if (!$fh)
    {
        exit('File sample4.txt not found.');
    }

    $text = fgets($fh);

    while (!feof($fh)) 
    {
        $new = preg_replace('/(\w+)\s(\w+)\s(\w+)/','$2, $1 $3',
                $text);

        echo $new .'<br />';

        $text = fgets($fh);
    }
  • Be able to determine what is going on with writing a file out to the disk. Be able to explain each of the file functions do.

e2sample5.php

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<html>
 <head><title>Sample5 Script</title></head>
 <body bgcolor="lightgreen">
    <?php
        $name = "John Doe";
        $address = "1001 Logic Dr.";
        $email = "johndoe@place.gov";
        $title = "VP";
        $outputstring = "$name\t$address\t$email\t$title\n";

        echo 'The data to be written:<br />';
        echo $outputstring .'<br>';

        $filename = $_SERVER['DOCUMENT_ROOT'] .'/files/info.txt';

        $filehandle = fopen($filename, "ab");

        if (fwrite($filehandle, $outputstring,
                strlen($outputstring)) == FALSE) 
        {
            echo "You cannot write to $filename.<br />";
        }
        else
        {
            $text = file_get_contents($filename);
            echo '<br />The contents of the file is:<br />';
            echo "<pre>$text</pre>”;
        }

        fclose($filehandle);
    ?>
 </body>
</html>
  • Where is this script looking for the file data.txt to be located? What happens if the file isn’t there? If the file is there what does the script do?

e2sample6.php

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<html>
 <head>
  <title>Sample6 Script</title>
 </head>
 <body bgcolor="lavender">
    <?php
        $filename = $_SERVER['DOCUMENT_ROOT'] .'/files/data.txt';

        if (!file_exists($filename)) 
        {
            exit("No such file as: $filename");
        }

        $fh = fopen($filename,'rb');

        while (!feof($fh)) 
        {
            $line = fgets($fh);
            echo $line .'<br />';
        }

        fclose($fh);

        $lines = file($filename);

        foreach ($lines as $line_num => $line) 
        {
            $line_num++;
            echo '<b>'. $line_num .'</b>: '. $line .'<br />';
        }
    ?>
 </body>
</html>
  • What is the purpose of the function mysqli_fetch_field_direct()? What does mysqli_num_fields() do?

e2sample7.php

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php
    $db = mysqli_connect('localhost','root','', 'northwind');
    if (!$db) 
    {
        exit('Connection to server failed: '. mysqli_error($db));
    }

    $result = mysqli_query($db, 'SELECT * FROM Customers');

    if (! $result) 
    {
        exit('Database query error: '. mysqli_error($db));
    }
?>

<table>
    <?php for ($i=0; $i < mysqli_num_fields($result); $i++): ?>
        <th><u> <?= mysqli_fetch_field_direct($result, $i)->name ?> </u></th>
    <?php endfor; ?>

    <?php while ($record = mysqli_fetch_row($result)): ?>
        <tr>
            <?php for ($j=0; $j < mysqli_num_fields($result); $j++): ?>
                  <td><?= $record[$j] ?></td>
            <?php endfor; ?>
        </tr>
    <?php endwhile; ?>
</table>
  • In this code what is the purpose of enctype="multipart/form-data"? What is the move_uploaded_file() and what is the end result of it being run in this php script?

e2sample8.html

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<html>
 <head>
  <title>Sample8 Form</title>
 </head>
 <body bgcolor="lavender">
  <form
       enctype="multipart/form-data"
       action="sample7.php"
       method="POST">
   Browse and select the file you want to upload: <br />
   <input name="my_file" type="file" />
   <br />
   <input type=submit value="Get File"/>
  </form>
 </body>
</html>

e2sample8.php

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<html>
 <head>
  <title>Sample7 Script</title>
 </head>
 <body bgcolor="#33ff33">
    <?php
        echo 'The uploaded file is: ', $_FILES['my_file']['tmp_name'],'<br />';

        $filename = $_FILES['my_file']['name'];
        $filesize = $_FILES['my_file']['size'];
        $directory = $_SERVER['DOCUMENT_ROOT'] .'/files/';
        $uploadFile = $directory . $filename;

        echo "The moved file is: $uploadFile<br />";

        if (move_uploaded_file($_FILES['my_file']['tmp_name'], $uploadFile)) 
        {
            echo 'The file was successfully uploaded.<br />';
            echo "The size of file, $filename, is $filesize bytes.<br />";
        }
    ?>
 </body>
</html>