Zend Flashcards

1
Q

What languages influence PHP syntax

A

Prdominantly C language, but influence from PERL. Recent object-orenented syntax from JAVA

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

What are the four types of PHP tags?

A

Standard < ? php code ? >
Short < \? code \? > & < ?= $variable ? >
Script < script language=”php” > code < /script >
ASP < % code % >

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

What PHP tags should be used and why?

A

Standard

Best solution for portablility and backwards compatiblity. Also, cannot be disabled in PHP config file.

Other tags are considered depreciated.

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

What character is used to terminate PHP statements?

A

;

This is not required for the last statement in the closing tag, but should be added for consistency.

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

How is a line commented? Multi-line commented?

A

Single line: // line

          #  line

Multi-line: /* line1

             line2

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

Where are three places where you CANNOT use whitespace characters?

A
  • Between \
    )
  • To break apart variable and function names (e.g. $var name, function foo bar)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What characters define a code block?

A

{“ and “}””

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

What is a language construct?

A

Elements pre-built into the language that follow special rules. The most common in PHP is the echo construct. It is not a function since is does not return a value.

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

What are the two general categories of PHP data types and their differences?

A

Scalar: Contain one value at a time

Composite: Containers of other data

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

What are the four scalar data types?

A

Boolean: Either true or false

int: signed numeric iteger value
float: signed floating-point value
string: collection of binary data

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

What are the three different notation of integers PHP supports?

A

Decimal: 10; -11; 1452: No thousand separator needed, or allowed

Octal: 0666; 0100: Identified by leading zero, used mainly to identify UNIX permisions

Hexadecmial: 0x123; 0XFF; -0x100: Base 16 notation, leading 0x that is case insensitive

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

What are the two different notations of floating-point numbers PHP supports?

A

Decimal: 0.12, 12.14, -.123: Traditional decimal notation

Exponential: 2E7, 1.2e2: a set of significant digits (also called mantissa), followed by the case-insensitive letter E and then by an exponent. The resulting number is expressed multiplied by 10 to the power of the exponent, for example, 1e2 equals 100.

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

Can the precision and range of numbers vary in PHP, and if so, why?

A

Yes. Depending on how PHP was complied, on a 64 bit platform, maybe capable of wider range then on 32 bit platform.

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

Does PHP track overflows?

A

No.

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

Consider the following statement:

echo (int) ((0.1 + 0.7) * 10)

What is printed out and why?

A
  1. This is because prior to the int conversion, the number is stored as 7.999999 instead of 8. The int conversion chops the remander off. Because of this, you should consider using the BCMath exension instead of PHP built in data types
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Many programmers consider strings equivilant to text. Is this the case in PHP? Why or why not?

A

This is not the case in PHP. Strings are considered binary data. Due to this, you could store text, image files, spreadsheet, music recordings, etc in string data types.

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

Describe data-type conversions for the following:
Number converted to a Boolean
String converted to a Boolean
Boolean converted to a number or string

A

A number to a boolean becomes false if the original value is zero, true otherwise
A string converted to a boolean is false if it is empty or equals “0”. If it contains other data, even multiple zeros, it is true.
When boolean is converted to a number or string, 1 if true, 0 if false

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

What two compound data types are supported in PHP?

A

Array: Containers of ordered data elements that can contain other data types, event other arrays
Objects: Containers of both data and code. Form basis of Object Oriented programing

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

What two other data types, aside from Scaler and Compound are found in PHP?

A

NULL: indicates a variable has no value.

resource: used to indicate external resources that are not natively used by PHP, such as a file handle.

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

What is type conversion and how does a user force it in PHP?

A

Type conversion is converting from one data type to another. This is done by placing the type you want to convert to in brackets before the expression. Example:
$x = 10.88;
echo (int) $x; //outputs 10

Not that you cannot convert any value to a resource, but you can convert a resource to a value, resulting in the resource’s ID number.

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

PHP variables are “loosely typed” while other languages are “strongly typed.” What is the difference between the two?

A

Loosely typed: implicitly change the type of the variable as needed depending on the operation being performed on it’s value

Strongly typed: variable can only contain one type of data throughout its existence.

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

What are the rules for naming variable in PHP?

A

Number, letters and underscore only allowed. Variable must start with underscore or letter.

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

What is a variable variable?

A

It is the process of taking the value of one variable and using it has the name of another variable using the “$$” syntax. Example:
$name = ‘foo’;
$$name = ‘bar’;
echo $foo; //Displays ‘bar’

Due to this, it is possible to create variable names that do not follow PHP variable naming rules. Use extreme care since they can make code difficult to understand and improper use could lead to security issues.

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

Describe what the following code will do in PHP:

 function myFunc() {
 echo 'myFunc!';
 }

$f = ‘myFunc’;
$f();

A

Setting $f to ‘myFunc’ and then executing the statement $f() will execute the function called ‘myFunc’, echoing out “myFunc!”

25
Q

What does the function isset() do in PHP?

 Example:
 echo isset ($x);
A

Will return true if variable exists AND has a value other then NULL.

26
Q

How do you define a constant in PHP?

A

Use the ‘define’ function:

define(‘EMAIL’, ‘somoneon@here.net’);
echo EMAIL; //Display someone@here.net

27
Q

What are the naming rules for constants in PHP?

A

They can contain only letters, numbers and underscores. They should be named in uppercase letters as in standard programing practices, but is not required.

28
Q

What are the various operators used in PHP?

A

Assignment: assign data to variables
Arithmetic: perform basic math functions
String: join two or more strings
Comparision: compare two pieces of data
Logical: perform logical operations on boolean data

Bitwise: manipulating bits using boolean math
Error Control: suppressing errors
Execution: executing system commands
Incrementing/Decrementing: incrementing and decrementing numberical values
Type: identifying objects

29
Q

What are the five arithmetic operators used in PHP?

A
Addition: $a = 1 +3.5;
 Subtraction: $a = 4 - 2;
 Multiplication: $a = 8 * 3:
 Division: $a = 15 / 5;
 Modulus: $a = 23 % 7;
30
Q

How are incrementing and decrementing operators used in PHP? What are their symbols?

A

If the operator is placed after its operand, the interpreter will first return the value of the latter (unchanged), then then increment/decrement it by 1

If the operator is placed before the operand, the interpeter will first increment/decrement the value of the latter, and then return the newly calculated value.
Example:
$a = 1;
echo $a++ //Output 1, $a now equal to 2
echo ++$a; //Output 3, $a now equal to 3
echo –$a: //Output 2, $a now equal to 2
echo $a– //Output 2, $a now equal to 1

31
Q

How are strings concatenated in PHP?

A

Using the “.” character.

Example: $var1 . $var2

32
Q

What data type will bitwise operators only work on? What if they aren’t that data type?

A

Integers. If the operands are not integers, the interpreter will attempt to convert them.

33
Q

What are the six bitwise operators and how they are represented?

A
Not: ~
 AND: &
 OR: |
 XOR: ^
 Left Shift: <<
 Right Shift: >>
34
Q

How to the bitwise operators shift right/left work?

A

Shift Left: shifts bits left resulting in multiplication.
$x = 1;
echo $x &laquo_space;1 // Outputs 2
echo $x &laquo_space;2 // Outputs 4

Shift Right: shifts bits right resulting in division.
$x = 8;
echo $x&raquo_space; 1 // Outputs 4
echo $x&raquo_space; 2 // Outputs 2

35
Q

Why must you be carefull when using the bitwise operators right/left shift?

A

It is possible to shift the bits out of the integer type that holds them, resulting in either overflow or underflow scenarios.

36
Q

By default, assignment operators work by value. What does this mean?

A

When you assign a variable to another, its value is copied to the variable. Any change made to the copied value does not affect the original variable.

37
Q

Describe how to assign variable by reference and what that means.

A
To pass a variable by reference, you use the "&" character.
 Example:
 $a = 10;
 $b = &$a; //by reference
 $b = 20;
 echo $a; //Outputs 20

This works for all data types, except objects, which are always passed by reference.

38
Q

In comparision operators, what are the four equivalence operations?

A

== Equivalence, true if both operands can be converted to a common datatype and are equal
=== Identity, true if only the datatypes are the same and the values are the same.
!= Not Equivalent, true if both operands are not equivalent, with out regards to data type
!== Not Identical, true if both operands are not of the same data type or do not have the same value

39
Q

What comparision operators are used for greater/less then?

A

< and and >=, greater then, or greater then or equal to

40
Q

What are the four logical operators used in PHP and their symbols?

A

NOT: “!” prior to operand: !$a
AND: “&&” or “and”: $a and $b, $a && $b
OR: “||” or “or”: $a || $b, $a or $b
Exclusive OR: “XOR”: $a XOR $b

41
Q

Out of the four logical operators, why is the NOT operator different?

A

The NOT “!” operator is urinary and works on one operand, while the other three are binary and work on two operands.

42
Q

If the left-hand operand evaluates to false in an AND operation, is the right-hand side evaluated?

A

Not in PHP, if the left-hand side is false, the right-hand side is never evaluated.

43
Q

What is the error suppression operator?

A

The “@” symbol is used for the error suppression operator. If it it prepends an expression, then it will prevent PHP from outputting an error, provided that the function uses PHP’s own functionality to report errors.

Example:

$x = @mysql_connect();

44
Q

How would you execute shell commands in PHP?

A

You can use the backtick operator to execute shell commands.

Example:

$a = ls -l;

45
Q

What is operator associativity?

A

Associativity is the direction operations are performed on an operation. This can be left to right, right to left, or none.

46
Q

What is a control structure?

A

They allow you to control the flow of a script. Examples of these are if statements, loops, etc.

47
Q

What are conditional structures and examples there of?

A

Conditional structures are used to change the flow of a script based on one or more conditions.

Example:

 if (expression){
 }
 elseif (expression){
 }
 else (expression){
 }

10 == $x ? ‘YES’ : ‘NO”;

 switch($a){
 case true:
 break;
 default;
 break
 }
48
Q

What ternary operator is used as a shortcut for if-then-else?

A

Example:

echo 10 == $x ? ‘YES’ : ‘NO”;

Same as:

 if (10 == $x){
 echo 'YES';
 }
 else{
 echo 'NO';
 }
49
Q

Describe a switch construct?

A

An initial expression is evaluated once, then compared to individual case values. A break statement is required to break out of the statement, or cases below the match will continue to be executed.

Example:

 $a = true;
 switch($a){
 case true:
 echo "true";
 break;
 default;
 echo "not true";
 break
 }
50
Q

What is an iterative construct?

A

Iterative constructs make it possible to run the same portion of code multiple times. These are more commonly known as loops.

51
Q

How many iterative constructs does PHP have and what are they?

A

Four, although only two are necessary to the functioning of the language.

while (condition){//code}
do{code}while(condition);
for(start instruction, condition, end loop instruction){//code};
foreach(array as value){//code}

52
Q

How does the break keyword affect a loop?

A

When the break keywords is encountered in a loop, the loop will be exited at that point.

53
Q

In the following code snippet, what does “break 2” mean:

 for ($i = 0; $i < 10; $i++){
 for ($j = 0; $j < 3; $j++){
 if (($j+ $i) % 5 == 0){
 break 2;
 }
 }
 }
A

Adding a number after the break keyword tells the interpreter how many nested loops to break out of. In this example, “break 2” will break out of both loops.

54
Q

Why should you always remember to end the “break” statement with a semi-colon?

A

If the break statement does not end with a semi-colon, and the code right after returns an integer, then this is appended to the break statement, possibly breaking out of multiple loops when not intended to.

Example;

$a = 1;
break
$a + 1;

would equivalate to break 2;

55
Q

What does the “continue” keyword do in a loop?

A

The continue keyword will stop the rest of the code from executing in the loop, and start a new iteration of the loop.

Example:

 while(true){
 echo "hello";
 continue;
 echo "there";
 }

“hello” will be echoed forever, but “there” will never be echoed.

56
Q

What are the five error levels in PHP?

A

Compile-time errors: Errors detected by the parser while it is compiling a script. Cannot be trapped in the script itself.

Fatal errors: Errors that halt the execution of a script. Cannot be trapped.

Recoverable errors: Errors that represent significant failures, but can still be handled in a safe way.

Warnings: Recoverable errors that indicate a run-time fault. Do not halt the execution of a script.

Notices: Indicate that an error condition occurred, but is not necessarily significant. Do not halt the execution of a script.

57
Q

By default, PHP reports errors it encounters to the scripts output. Why is this bad in a production environment and how can it be turned off?

A

Displaying all errors to users in a production environment is bad form, and a possible security issue. To disable errors from being displayed, in the php.ini add/edit the following line to the following:

display_errors = Off

58
Q

What function can be used to set your own error handler in PHP?

A

set_error_handler(error_function,error_types)

 1)
 {
 trigger_error("A custom error has been triggered");
 }
 ?>