Chapter 3 - Review

Review Questions from Chapter 3

Output
Show all Hide all
1Toggle answer visibilityWhat is an absolute path? What is a relative path?

The absolute path of a document includes the root directory.
On windows c:\Programs\Path\To\File.text
On a server, an absolute path includes the full url http://www.mysite.com/path/to/file.html

A relative path uses the current file location as a reference point. To move up the file tree use ../new_folder.

2Toggle answer visibilityWhat is the difference between include()and require()?

include() on failure will issue a warning but continues to run the script.

require() on failure will issue a fatal warning and halt further processing.

3Toggle answer visibilityWhat is the difference between include( )and include_once( )? Which function should you generally avoid using and why?

include_once takes up more processing time/power as it needs to check if the file has already been included. Generally use include_once() or require_once() for files that should not be duplicated ie.. files that define functions or set variables to initial values.

4Toggle answer visibilityWhy does it not matter what extension is used for an included file?

An included file is sent to the browser window as html with any php closing and opening tags being parsed properly. Thus the file extension can be anything within reason. Care should be made to keep any file that's not supposed to be accessed by web users outside the document root.

5Toggle answer visibilityWhat is the significance of the $_SERVER["REQUEST_METHOD"]value?
The $_SERVER["REQUEST_METHOD"] value indicates which method was used to display the page. After form submission the value is equivalent to the form method attribute. So you can use this value to determine if a form has been submitted or not.
6Toggle answer visibilityHow do you make the following form elements sticky? Text input, Select menu, Radio button, Check box, Textarea

A sticky form element improves form usability - meaning that the value is populated with any previously submitted data preventing unneccesary data reentry. Each form element's value is handled slightly differently.

Let's pretend the user submitted value is "red" and that value is stored in $_POST['form-element-name'].
Confirm that the value is valid then assign it to a php variable, $ch3_value.

$ch3_value = $_POST['form-element-name'] = "red"
Now lets make these various inputs reflect this user value.
Text input
<input type='text' name='text_name' value='$ch3_value'/>
Select menu
<select name='select_name'><option selected value='red'>Red</option><option value='blue'>Blue</option></select>

Assign all option values that match the user input to have the selected attribute. User submitted content may be an array!

Radio button
<input type="radio" name="radio_name" value="red" checked />Red<br/><input type="radio" name="radio_name" value="blue"/>Blue
Red
Blue

Assign the radio button value that matches the user input to have the checked attribute.

Check box
<input type="checkbox" name="checkbox_name" value="red" checked />Red<br/><input type="checkbox" name="checkbox_name" value="blue"/>Blue
Red
Blue

Assign all checkbox elements whose value matches the user input to have the checked attribute. User may submit an array!

Textarea
<textarea rows="1" cols="50" name="textarea_name">$ch3_value</textarea>

Handle user submitted text carefully! It could contain harmful code.

7Toggle answer visibilityIf you have a PHP error caused by code placed within an HTML tag, where must you look to find the error message?

If an error message occurs within an HTML tag, you must inspect the source code from the browser in order to find it.

8Toggle answer visibilityWhat is the syntax for defining your own function?

function function_name() { 
			#function logic goes here
			}

9Toggle answer visibility What is the syntax for defining a function that takes arguments?

function function_name(param1, param2) {
			#function logic goes here
			}

10Toggle answer visibilityWhat is the syntax for defining a function that takes arguments with default values? How do default values impact how the function can be called?

function function_name(param1, param2 = "green") {
			#function logic goes here
			}

Functions with default arguments must be called with parameters that match the order of the parameters in the function definition. If a default parameter is not set in the function call, the default value will be applied within the function during runtime.

11Toggle answer visibilityHow do you define and call a function that returns a value?

$var = function_name("Johnny");

function function_name(param1) {
			return "My name is ".$param1;
			}

Source
<?php
$ch3_value="red";
$review = array(
	1=>array(
		'q'=>'What is an absolute path? What is a relative path?',
		'a'=>'<p>The <b>absolute path</b> of a document includes the root directory. <br/>
		On windows <em>c:\Programs\Path\To\File.text</em><br/>
		On a server, an absolute path includes the full url <em>http://www.mysite.com/path/to/file.html</em></p>
		<p>A <b>relative path</b> uses the current file location as a reference point. To move up the file tree use <em>../new_folder</em>.</p>'
	),
	2=>array(
		'q'=>'What is the difference between include()and require()?',
		'a'=>'<p><b>include()</b> on failure will issue a warning but continues to run the script.</p>
		<p><b>require()</b> on failure will issue a fatal warning and halt further processing.</p>'
	),
	3=>array(
		'q'=>'What is the difference between include( )and include_once( )? Which function should you generally avoid using and why?',
		'a'=>'<p><b>include_once</b> takes up more processing time/power as it needs to check if the file has already been included.
		Generally use include_once() or require_once() for files that should not be duplicated ie.. files that define functions or set variables to initial values.<p>'
	),
	4=>array(
		'q'=>'Why does it not matter what extension is used for an included file?',
		'a'=>'<p>An included file is sent to the browser window as html with any php closing and opening tags being parsed properly. Thus the file extension can be anything within reason.
		 Care should be made to keep any file that\'s not supposed to be accessed by web users outside the document root. </p>'
	),
	5=>array(
		'q'=>'What is the significance of the $_SERVER["REQUEST_METHOD"]value?',
		'a'=>'The $_SERVER["REQUEST_METHOD"] value indicates which method was used to display the page. After form submission the value is equivalent to the form method attribute.
		So you can use this value to determine if a form has been submitted or not.'
	),
	6=>array(
		'q'=>'How do you make the following form elements sticky? Text input, Select menu, Radio button, Check box, Textarea',
		'a'=>'<p>A sticky form element improves form usability - meaning that the value is populated with any previously submitted
		 data preventing unneccesary data reentry. Each form element\'s value is handled slightly differently.</p>
		<p>Let\'s pretend the user submitted value is <b>"red"</b> and that value is stored in <b>$_POST[\'form-element-name\']</b>.<br />
		Confirm that the value is valid then assign it to a php variable, <b>$ch3_value</b>.
		</p>
		<pre>$ch3_value = $_POST[\'form-element-name\'] = "red"</pre>
		<dl> Now lets make these various inputs reflect this user value.
		<dt><b>Text input</b></dt>
		<dd>
			<div class="code-example">
				<span class="code-output">'.htmlspecialchars(sprintf("<input type='text' name='text_name' value='\$ch3_value'/>")).'</span>
				<br />
				<code class="code-snippet"><input type="text" name="text_name" value="'.sprintf('%s',$ch3_value).'" /></code>
			</div>
		</dd>
		<dt><b>Select menu</b></dt>
		<dd>
			<div class="code-example">
				<span class="code-output">'.htmlspecialchars(sprintf("<select name='select_name'><option selected value='red'>Red</option><option value='blue'>Blue</option></select>")).'</span>
				<br />
				<code class="code-snippet"><select name="select_name"><option selected value="'.sprintf('%s',$ch3_value).'">Red</option><option value="blue">Blue</option></select></code>
			</div>
			<p>Assign all option values that match the user input to have the selected attribute. User submitted content may be an array!</p>
		</dd> 
		<dt><b>Radio button</b></dt>
		<dd>
			<div class="code-example">
				<span class="code-output">'.htmlspecialchars(sprintf('<input type="radio" name="radio_name" value="red" checked />Red<br/><input type="radio" name="radio_name" value="blue"/>Blue')).'</span>
				<br />
				<code class="code-snippet"><input type="radio" name="radio_name" value="'.sprintf('%s',$ch3_value).'" checked />Red<br/><input type="radio" name="radio_name" value="blue"/>Blue</code>
			</div>
			<p>Assign the radio button value that matches the user input to have the checked attribute.</p>
		</dd>
		<dt><b>Check box</b></dt>
		<dd>
			<div class="code-example">
				<span class="code-output">'.htmlspecialchars(sprintf('<input type="checkbox" name="checkbox_name" value="red" checked />Red<br/><input type="checkbox" name="checkbox_name" value="blue"/>Blue')).'</span>
				<br />
				<code class="code-snippet"><input type="checkbox" name="checkbox_name" value="'.sprintf('%s',$ch3_value).'" checked />Red<br/><input type="checkbox" name="checkbox_name" value="blue"/>Blue</code>
			</div>
			<p>Assign all checkbox elements whose value matches the user input to have the checked attribute. User may submit an array!</p>
		</dd>
		<dt><b>Textarea</b></dt>
		<dd>
			<div class="code-example">
				<span class="code-output">'.htmlspecialchars(sprintf('<textarea rows="1" cols="50" name="textarea_name">$ch3_value</textarea>')).'</span>
				<br />
				<code class="code-snippet"><textarea rows="1" cols="50" name="textarea_name">'.sprintf('%s',$ch3_value).'</textarea></code>
			</div>
			<p>Handle user submitted text carefully! It could contain harmful code.</p>
		</dd>
		</dl>'
	),
	7=>array(
		'q'=>'If you have a PHP error caused by code placed within an HTML tag, where must you look to find the error message?',
		'a'=>'<p>If an error message occurs within an HTML tag, you must inspect the source code from the browser in order to find it.</p>'
	),
	8=>array(
		'q'=>'What is the syntax for defining your own function?',
		'a'=>'<p>
			<pre>function function_name() { 
			#function logic goes here
			}</pre></p>'
	),
	9=>array(
		'q'=>' What is the syntax for defining a function that takes arguments?',
		'a'=>'<p>
			<pre>function function_name(param1, param2) {
			#function logic goes here
			}</pre></p>'
	),
	10=>array(
		'q'=>'What is the syntax for defining a function that takes arguments with default values? How do default values impact how the function can be called?',
		'a'=>'<p>
			<pre>function function_name(param1, param2 = "green") {
			#function logic goes here
			}</pre></p>
			<p>Functions with default arguments must be called with parameters that match the order of the parameters in the function definition.
			If a default parameter is not set in the function call, the default value will be applied within the function during runtime.</p>'
	),
	11=>array(
		'q'=>'How do you define and call a function that returns a value?',
		'a'=>'<p>
			<code> $var = function_name("Johnny");</code>
			<pre>function function_name(param1) {
			return "My name is ".$param1;
			}</pre></p>'
	)
);
include('templates/review.php');
?>