1. So far so good
Now you have all basics of php learned. Sure, there are a lot of more to learn
like OOP or references, not to mentioned about extensions like
mysqli. However, this is a good point to check the stuff you learned in the
chapters before. For this reason there are some questions you should answered before
proceeding to the next chapters.
1. When do you use a while loop or a for loop?
A while loop is used if you dont know how many iterations you will have.
The for loop use a special layout to count a variable from one value to
another value. So a for loop is used to runs a programm block a given number
of iterations.
2. Which comment types exists?
There are one line comments which starts with // and there
are multi line comments which starts with /* (or /**) and
ends with */.
3. How does a function call looks?
If you want to call a function use the name of the function. After that you
can provide parameters for the function. These are seperated with commas and
the whole parameter list is written in parentheses. And then the function call
(and all other statements) are finished with a ;.
4. What runs on a server, what runs on a client?
PHP is only executed and interpreted on the server. Actions like "starting winamp" is not
possible. PHP itself just generate a html document.
5. How do I subtract 1 from a value?
There are 3 possible ways.
<?php
$var = 10;
$var = $var - 1;
$var -= 1;
$var--;
?>
6. What datatypes exists in php?
There are the following datatypes.
-
boolean
-
integer
-
float, double
-
String
-
Array
You haven't learned the following datatypes yet.
-
Object
-
Resource
-
Null
7. How do I concatenate 2 strings, 2 variables or one string and one variable together?
Expressions can be concatenate with the dot (.) operator.
<?php
$string1 = "foo";
$string2 = "bar";
$var = "bla"."blu";
$var = "bli".$string1;
$var = $string1.$string2;
?>
8. How starts and end a php document?
A php document starts with <?php and ends with ?>.
9. How starts a html document?
A html document starts with the DOCTYPE which defines what
kind of html document is used.
10. Which both parts are send from the server to the client and in which order?
First the server sends the headers of the requested file/URL.
After that the content is send. If some content is
already send (like a space, but also the BOM)
the headers cannot be changed as they are gone.
11. What is the result of (!(!true XOR true) AND !(!false OR !true)) XOR (false OR (true XOR !false))?
The result is false.
(!(!true XOR true) AND !(!false OR !true)) XOR (false OR (true XOR !false))
(!(false XOR true) AND !( true OR false)) XOR (false OR (true XOR true ))
(!( true ) AND !( true )) XOR (false OR ( false ))
( false AND false ) XOR (false OR false )
( false ) XOR ( false )
false
12. Whats the diffence between \n and <br />?
\n is an escape sequence in php and results in a newline in the output of the html code.
<br /> however is an html tag which creates a new line in the
browsers output.
13. When gets an else part executed?
If the expression in the if part results in false then the
else part gets executed.
14. What does break; and what continue;?
break; stops the current loop or switch and continues with the next
php code. continue; cancels the current iteration and begins with the next
one. In a for loop the iteration statement gets also executed.
15. What is a valid index for an array entry?
An index can only be an integer number or a string.
16. Write a script which saves the integer numbers from -10 to +10 in an array
<?php
$array = array(); // create an empty array
for ($i=-10; $i<=10; $i++) {
$array[] = $i;
}
// easier: $array = range(-10, 10);
?>
17. Write a script which saves all even numbers between $start and $stop in an array
<?php
// $start and $stop are defined somewhere before
$array = array(); // create an empty array
if ($start%2) { // if the division results in a remaining of 2
$start++; // increase the value by one
}
for ($i=$start; $i<=$stop; $i+=2) {
$array[] = $i;
}
?>
18. Write a script which sets the two last digits of an integer variable to 0
<?php
$number = 3463; // example
$remain = $number % 100;
$number -= $remain;
echo $number;
/* shorter version
$number -= $number % 100;
*/
?>