Intro to PHP Flashcards

1
Q

PHP: ______.

A

“PHP: Hypertext Preprocessor,” is a server language. This means that it executes code on the server then sends the results to the browser primarily as html content.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

PHP manages __________ content, communicates with _____________, tracks user sessions, sends _______, read/write ________, implements __________, and ________ __________.

A

PHP manages dynamic content, communicates with databases, tracks user sessions, sends email, read/writes files, implements security, and processes forms.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

A PHP block must begin with ______ and end with ______ .

A

a PHP block must begin with .

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

_______ PHP blocks can be used inside of one file.

A

Multiple PHP blocks can be used inside of one file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Code (PHP) to output to page

A

echo(); //output to page

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Code (PHP) that shows information about the server’s PHP installation

A

phpinfo(); //shows information about the server’s PHP installation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Code (PHP) to view all errors warnings and notices

A

error_reporting(E_ALL);

//view all errors warnings and notices

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Different servers have different ________. If something doesn’t work, it might be a server ______ issue.

A

Different servers have different configurations. If something doesn’t work, it might be a server configuration issue.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

PHP and HTML code _____ be used on the same page.

A simple way to use this technique effectively is to remember that _____ goes inside of PHP blocks and HTML _____t.

A

PHP and HTML code can be used on the same page.

A simple way to use this technique effectively is to remember that PHP goes inside of PHP blocks and HTML doesn’t.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

To ouput html, use the _____ statement. _____(“html goes here”);

A

To ouput html, use the echo statement. echo(“html goes here”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Variables start with a _

A

Variables start with a $

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

No variable ______ is required. You can just start using variables. e.g $someVar = 12

A

No variable declaration is required. You can just start using variables. e.g $someVar = 12

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Concatenation uses a _____ NOT a ___ sign. e.g.

A

Concatenation uses a period NOT a plus sign.

Example:
$var1 = “I love”;
$var2 = “servers”;
echo($var1.$var2); //outputs “I loveservers”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Double quotes will _____ variables, and special characters, like \n or \t.

Single quotes will not and will ____ the literal string.

A

Double quotes will parse variables, and special characters, like \n or \t.

Single quotes will not and will output the literal string.

Example:
$name = “Joe”;
‘Hi I am $name’ //Hi I am $name
“Hi I am $name” //Hi I am Joe

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Example of an if statement:

A

$test = 1;
if ($test == 1) {
echo (“we <b>passed</b> with a score of $test”); //note how you can embed a variable directly in a string
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Arrays hold _____ _____ referenced by a ______ ______ .

A

Arrays hold multiple values referenced by a numerical index.

Example:
$myArray = array(“a”,”b”,”c”); //create an array

array_push($myArray,”d”); //add to end of array
array_pop($myArray) //removes and returns the last element of an array
count($myArray); //returns the array length

echo($myArray[0]); //prints out the first value in the array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Looping through a php array:

A

for ($i=0; $i

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

The indexes of Associative arrays are _____, not numbers.

A

The indexes of Associative arrays are strings, not numbers.

Example:
$myArray = array(“first” => “Wasim”, “last” => “Singh”);
$myArray[“middle”] = “Andy”; //add associative property and value

19
Q

foreach loop:

A

foreach( $array as $value ) {
echo (“Value is $value <br></br>”);
}

20
Q

Single-quoted strings aren’t _______, so whatever you’ve put in the string, that’s what will show up.

Double-quoted strings are _____ and any PHP variables in the string are _______.
Additionally, escaped characters like \n for newline and \t for tab are not evaluated in single-quoted strings, but are evaluated in double-quoted strings

A

Single-quoted strings aren’t parsed, so whatever you’ve put in the string, that’s what will show up.

Double-quoted strings are parsed and any PHP variables in the string are evaluated. Additionally, escaped characters like \n for newline and \t for tab are not evaluated in single-quoted strings, but are evaluated in double-quoted strings

21
Q

We can pass information to a PHP script via the _ _ _ method or the _ _ _ _ method.

These methods are defined in an HTML form and the _ _ _ method adds variables at the end of the URL.

A

We can pass information to a PHP script via the GET method or the POST method.

These methods are defined in an HTML form and the GET method adds variables at the end of the URL.

PHP can access these GET and POST variables and values via the $_GET and $_POST associative arrays.

22
Q

Using ___, the URL is formatted with _____-value pairs like so:

http://yourwebsite.com/form-processor.php?name1=value1&name2=value2&name3=value3

A

Using GET, the URL is formatted with name-value pairs like so:

http://yourwebsite.com/form-processor.php?name1=value1&name2=value2&name3=value3

Note the format of the variables and the question mark at the beginning to signify the beginning of the variables string. Also notice the ampersand that separates variable/value pairs. This is known as a URL encoded string.

23
Q

The ____ method produces a long string that appears in your _______ logs, in the browser’s Location: box and is recognized by search engines

A

The GET method produces a long string that appears in your server logs, in the browser’s Location: box and is recognized by search engines

24
Q

The ____ method is restricted to send up to a limited number of characters (based on the browser)

A

The GET method is restricted to send up to a limited number of characters (based on the browser)

25
Q

GET method isn’t used to send _____ data, like images or word documents, to the server.

A

GET isn’t used to send binary data, like images or word documents, to the server.

26
Q

The ______ method transfers information via HTTP headers.

A

The POST method transfers information via HTTP headers.

27
Q

The _____ method does not have any restriction on data size to be sent.

A

The POST method does not have any restriction on data size to be sent.

28
Q

The POST method can be used to send ____ as well as _____ data.

A

The POST method can be used to send ASCII as well as binary data.

29
Q

The data sent by POST method goes through ______ _____ so security depends on _____ protocol. By using Secure ____ you can make sure that your information is secure.

A

The data sent by POST method goes through HTTP header so security depends on HTTP protocol. By using Secure HTTP you can make sure that your information is secure.

30
Q

When hooking up forms, be sure to include the _____ attribute for all elements in your form. Those _____ become the associative variable name in the $_GET and $_POST arrays.

A

Be sure to include the name attribute for all elements in your form. Those names become the associative variable name in the $_GET and $_POST arrays.

31
Q

The action attribute of your form itself is the processing page that your _____ and _______ will be submitted to.

A

The action attribute of your form itself is the processing page that your form and variables will be submitted to.

32
Q

The ______ attribute of your form determined the method of form submission. It may be GET or POST.

A

The method attribute of your form determined the method of form submission. It may be GET or POST. There are others but we will focus on GET an POST for now.

33
Q

Instead of one form page and one processing page illustrated above, there are three pages:

1)
2)
3)

A

Instead of one form page and one processing page illustrated above, there are three pages:

1) Form page
2) Processing page which accepts POST variables from the Form page, processes them and then redirects the user to a destination page
3) Destination page

34
Q

Make forms using 3 pages because it prevents: 1-3

A

1) prevents forms from being submitted multiple times,
2) prevents POST errors when using the “Back” and “Forward” buttons,
3) prevents POST data from being processed multiple times when the processing page is refreshed

35
Q

_______ code to be added at the end of the processing code on the processing page:

A

Redirection code to be added at the end of the processing code on the processing page:

header(“Location: path-to-your-destination-page.php”);

36
Q

To check if a GET or POST variable is available, use the _____) function.

______() determines if a variable is set and is not NULL.

A

To check if a GET or POST variable is available, use the isset() function.

isset() determines if a variable is set and is not NULL.

if(isset($_POST['input'])){
	//process
}else{
	//handle else
}
37
Q

Upload a file (HTML):

A
38
Q

Upload a file (PHP):

A
$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

if (move_uploaded_file($_FILES[‘userfile’][‘tmp_name’], $uploadfile)) {
echo “File is valid, and was successfully uploaded.\n”;
} else {
echo “Possible file upload attack!\n”;
}

echo ‘Here is some more debugging info:’;
print_r($_FILES);

39
Q

When uploading a file, the enctype of the form must be ‘_____/______-data’

A

The enctype of the form must be ‘multipart/form-data’

40
Q

When files are uploaded to the server, an _____ that holds the file information is created.

A

When files are uploaded to the server, an array that holds the file information is created.

Array (
[file_upload] => Array (
[name] => an-image.png
[type] => image/png
[tmp_name] => /path-to-temp-folder-on-server
[error] => 0
[size] => 328119 )
)
41
Q

If a file of the same name already exists on the server, that file will be overwritten by the new upload.

To avoid this, old files may be deleted by using the ______() function and in many cases, uploaded files can be given a unique name so that existing files won’t be overwritten. As a best practice, organize uploaded files by year, month and day.

A

If a file of the same name already exists on the server, that file will be overwritten by the new upload.

To avoid this, old files may be deleted by using the unlink() function and in many cases, uploaded files can be given a unique name so that existing files won’t be overwritten. As a best practice, organize uploaded files by year, month and day.

42
Q

Developers should _______ the file (make sure the filetype is correct and file size limits are met etc.)

A

Developers should validate the file (make sure the filetype is correct and file size limits are met etc.)

43
Q

When uploading a file that you may want to track, you may be tempted to store that file in the database. Is this recommended?

A

When uploading a file that you may want to track, you may be tempted to store that file in the database. This isn’t recommended.

44
Q

To store images/files:

1)
2)
3)

A

1) Upload the file to the server (using the file upload code)
2) Store the filename in a database table.
3) To retrieve the file, create “img” or “a” tags that reference the file by its name (now stored in the database)