Chapter 1 - Review

Review questions from chapter 1.

Output
Show all Hide all
1Toggle answer visibilityWhat tags are used to surround PHP code?

<?php PHP CODE HERE ?>

2Toggle answer visibilityWhat extension should a PHP file have?

filename.php

3Toggle answer visibilityWhat does a page’s encoding refer to? What impact does the encoding have on the page?

For HTML5 <meta charset="utf-8">

激光, 這兩個字是甚麼意思. A page's encoding determines how bytes of data are converted graphically into meaningful language. A page's encoding is literally declared via the meta tag in the document header. A document's embedded encoding must match the explicitly declared encoding for the text to be interpreted properly.

UTF-8 has replaced ASCII as the most popular encoding on the internet. It represents every character in the Unicode character set (Unicode provides a unique number for every character, no matter what the platform, no matter what the program, no matter what the language :)

4Toggle answer visibilityWhat PHP functions, or language constructs, can you use to send data to the Web browser?
echo
a language construct that echoes passed parameters and does not return a value. If you want to pass more than one parameter to echo, the parameters must not be enclosed within parentheses.
echo('param1 ', 'param2 ','param3')param1 param2 param3
print()
a language construct that outputs its parameter, always returns the value 1.
print('I want ice cream.')I want ice cream.
printf()
outputs a formatted string.
printf('a %s with %u wings.', 'bird', 2) a bird with 2 wings.
5Toggle answer visibilityHow does using single versus double quotation marks differ in creating or printing strings?
Strings can be interpreted or literally output. If contained within double quotes, a string will be interpreted, meaning any php variables will be converted to their values and any escaped characters will display their unescaped characters. Single quotes will result in a literal rendition of your text (except for escaped single quotes and escaped backslashes \' or \).

6Toggle answer visibilityWhat does it mean to escape a character in a string?
To escape a character in a string you must prepend a backslash. This will override the interpreted meaning of the character and replace it with it's literal value. For example: in single quotations to display a single quote, you must write \'
7Toggle answer visibilityWhat are the three comment syntaxes in PHP? Which one can be used over multiple lines?
  1. # comment spanning one line
  2. // another comment spanning one line
  3. /*and yet another
    comment spanning multiple lines*/
8Toggle answer visibilityWhat character do all variable names begin with? What characters can come next? What other characters can be used in a variable’s name?

All php variables begin with the $ followed by a letter or underscore then any combination of letters, numbers or underscores.

ALL OK $_aCoolVariable, $my_name, $John4521
NOT OK $24_hours, $cost-per-unit

9Toggle answer visibilityAre variable names case-sensitive or case-insensitive?

Variable names are CASE-SENSITIVE. $Error!=$error

10Toggle answer visibilityWhat is the assignment operator?

=

$color = "blue";
The variable $color is assigned the value "blue"

11Toggle answer visibilityHow do you create a string variable?

A string variable is created using the assignment operator and quotations.
$text = "Dog, as a devil deified, lived as a god."

12Toggle answer visibilityWhat is the concatenation operator? What is the concatenation assignment operator?
.
The concatenation operator acts as glue between values.
$var = "make " . "peace";
.=
The concatenation assignment operator appends the value on the right of the equal sign to the end of the left side operand.
$var .= " not war;"
13Toggle answer visibilityHow are constants defined and used?

Constants are defined using the define() function. They cannot be overwritten and are by default case sensitive. To output a constant - it must not be in quotations. They only accept null or scalar values(ie. boolean,float, int, string).
define(NAME_OF_CONSTANT, "value of constant");

Source
<?php
$review = array(
	1=>array(
		'q'=>'What tags are used to surround PHP code?',
		'a'=>"<p><b>&lt;?php</b> <em>PHP CODE HERE</em> <b>?&gt;</b></p>"
	),
	2=>array(
		'q'=>'What extension should a PHP file have?',
		'a'=>"<p>filename<b>.php</b></p>"
	),
	3=>array(
		'q'=>'What does a page’s encoding refer to? What impact does the encoding have on the page?',
		'a'=>"<p>For HTML5  <code>".htmlspecialchars('<meta charset="utf-8">')."</code></p>
		<p>激光, 這兩個字是甚麼意思. A page's encoding determines how bytes of data are converted graphically into meaningful language.
		A page's encoding is literally declared via the meta tag in the document header. A document's embedded encoding
		must match the explicitly declared encoding for the text to be interpreted properly.</p>	
		<p>UTF-8 has replaced ASCII as the most popular encoding on the internet. It represents every character in the Unicode character
		 set (Unicode provides a unique number for every character, no matter what the platform, no matter what the program, no matter what the language :)</p>"
	),
	4=>array(
		'q'=>'What PHP functions, or language constructs, can you use to send data to the Web browser?',
		'a'=>"
		<dl>
		<dt>echo</dt>
			<dd> a language construct that echoes passed parameters and does not return a value.
			 If you want to pass more than one parameter to echo, the parameters must not be enclosed
			 within parentheses.
			<div class='code-example'>
				<code class='code-snippet'>echo('param1 ', 'param2 ','param3')</code>
				&rarr; 
				<span class='code-output'>".htmlspecialchars(sprintf('%s %s %s','param1','param2','param3'))."</span>
			</div>
			</dd>
		<dt>print()</dt>
			<dd>a language construct that outputs its parameter, always returns the value 1.
			<div class='code-example'>
				<code class='code-snippet'>print('I want ice cream.')</code>
				&rarr; 
				<span class='code-output'>".htmlspecialchars(sprintf('I want ice cream.'))."</span>
			</div>
			</dd>
		<dt>printf()</dt>
			<dd>outputs a formatted string.
			<div class='code-example'>
				<code class='code-snippet'>printf('a %s with %u wings.', 'bird', 2) </code>
				&rarr; 
				<span class='code-output'>".htmlspecialchars(sprintf('a %s with %u wings.', 'bird', 2))."</span>
			</div>
			</dd>
		</dl>"
	),
	5=>array(
		'q'=>'How does using single versus double quotation marks differ in creating or printing strings?',
		'a'=>"Strings can be interpreted or literally output. If contained within double quotes, a string will be interpreted,
		meaning any php variables will be converted to their values and any escaped characters will display their unescaped characters.
		Single quotes will result in a literal rendition of your text (except for escaped single quotes and escaped backslashes \' or \\).</p>"
	),
	6=>array(
		'q'=>'What does it mean to escape a character in a string?',
		'a'=>"To escape a character in a string you must prepend a backslash. This will override the interpreted meaning
		of the character and replace it with it's literal value.".' For example: in single quotations to display a single quote, you must write <b>\\\'</b>'
	),
	7=>array(
		'q'=>'What are the three comment syntaxes in PHP? Which one can be used over multiple lines?',
		'a'=>'<ol><li># comment spanning one line</li>
				<li>// another comment spanning one line</li>
				<li>/*and yet another<br/>comment spanning multiple lines*/</li>
				</ol>'
	),
	8=>array(
		'q'=>'What character do all variable names begin with? What characters can come next? What other characters can be used in a variable’s name?',
		'a'=>'<p>All php variables begin with the <b>$</b> followed by a letter or underscore then any combination of letters, numbers or underscores.</p>
		<p><span style="color:green">ALL OK</span> $_aCoolVariable,    $my_name,    $John4521<br/>
		 <span class="error">NOT OK</span>  $24_hours,    $cost-per-unit </p>'
	),
	9=>array(
		'q'=>'Are variable names case-sensitive or case-insensitive?',
		'a'=>'<p>Variable names are <b>CASE-SENSITIVE</b>.  $Error!=$error</p>'
	),
	10=>array(
		'q'=>'What is the assignment operator?',
		'a'=>'<p><b>=</b></p><p>$color<b> = </b>"blue";<br />The variable $color is assigned the value "blue"</p>'
	),
	11=>array(
		'q'=>'How do you create a string variable?',
		'a'=>'<p>A string variable is created using the assignment operator and quotations.<br />
		$text = "Dog, as a devil deified, lived as a god."</p>'
	),
	12=>array(
		'q'=>'What is the concatenation operator? What is the concatenation assignment operator?',
		'a'=>'<dl><dt><b>.</b></dt>
		<dd>The concatenation operator acts as glue between values.<br />
		$var = "make " . "peace";</dd><dt><b>.=</b></dt>
		<dd>The concatenation assignment operator appends the value on the right of the equal sign to the end of the left side operand.<br />
		$var .= " not war;"</dd>
		</dl>'
	),
	13=>array(
		'q'=>'How are constants defined and used?',
		'a'=>'<p>Constants are defined using the define() function. They cannot be overwritten and are by default case sensitive. To output a constant - it must not be in quotations. They only accept null or scalar values(ie. boolean,float, int, string).<br/ >
		define(NAME_OF_CONSTANT, "value of constant");<br />
		</p>'
	)
);
include('templates/review.php');
?>