Generales Flashcards Preview

PHP-OOP > Generales > Flashcards

Flashcards in Generales Deck (30):
1

What is a variable

Variables are "containers" for storing information.

2

What is a variable

Variables are "containers" for storing information.

3

How is defined a variable


4

Rules for PHP variables:

•A variable starts with the $ sign, followed by the name of the variable
•A variable name must start with a letter or the underscore character
•A variable name cannot start with a number
•A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
•Variable names are case-sensitive ($age and $AGE are two different variables)

5

PHP Data Types

PHP supports the following data types:
• String
• Integer
• Float (floating point numbers - also called double)
• Boolean
• Array
• Object
• NULL
• Resource

6

PHP Operators

Operators are used to perform operations on variables and values.
PHP divides the operators in the following groups:

• Arithmetic operators
• Assignment operators
• Comparison operators
• Increment/Decrement operators
• Logical operators
• String operators
• Array operators

7

PHP Conditional Statements

In PHP we have the following conditional statements:
• if statement - executes some code if one condition is true
• if...else statement - executes some code if a condition is true and another code if that condition is false
• if...elseif....else statement - executes different codes for more than two conditions
• switch statement - selects one of many blocks of code to be executed

8

PHP - The if...elseif....else Statement

if (condition) {
code to be executed if this condition is true;
} elseif (condition) {
code to be executed if this condition is true;
} else {
code to be executed if all conditions are false;
}

9

The PHP switch Statement

switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all labels;
}

10

When use switch statement

Use break to prevent the code from running into the next case automatically. The default statement is used if no match is found.

11

PHP Loops

In PHP, we have the following looping statements:

-while - loops through a block of code as long as the specified condition is true
-do...while - loops through a block of code once, and then repeats the loop as long as the specified condition is true
-for - loops through a block of code a specified number of times
-foreach - loops through a block of code for each element in an array

12

While Loop
The while loop executes a block of code as long as the specified condition is true.
";
$x++;
}
?>


The example below first sets a variable $x to 1 ($x = 1). Then, the while loop will continue to run as long as $x is less than, or equal to 5 ($x <= 5). $x will increase by 1 each time the loop runs ($x++):

13

The PHP do...while Loop

The do...while loop will always execute the block of code once, it will then check the condition, and repeat the loop while the specified condition is true.

";
$x++;
} while ($x <= 5);
?>

The example below first sets a variable $x to 1 ($x = 1). Then, the do while loop will write some output, and then increment the variable $x with 1. Then the condition is checked (is $x less than, or equal to 5?), and the loop will continue to run as long as $x is less than, or equal to 5:

14

The PHP for Loop

The for loop is used when you know in advance how many times the script should run.

";
}
?>
The example below displays the numbers from 0 to 10:

15

Parameters of a for loop


init counter: Initialize the loop counter value
test counter: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends.
increment counter: Increases the loop counter value

16

The PHP foreach Loop

The foreach loop works only on arrays, and is used to loop through each key/value pair in an array.

For every loop iteration, the value of the current array element is assigned to $value and the array pointer is moved by one, until it reaches the last array element.

The following example demonstrates a loop that will output the values of the given array ($colors):

";
}
?>

17

How is defined a variable

18

Rules for PHP variables:

•A variable starts with the $ sign, followed by the name of the variable
•A variable name must start with a letter or the underscore character
•A variable name cannot start with a number
•A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
•Variable names are case-sensitive ($age and $AGE are two different variables)

19

PHP Data Types

PHP supports the following data types:
• String
• Integer
• Float (floating point numbers - also called double)
• Boolean
• Array
• Object
• NULL
• Resource

20

PHP Operators

Operators are used to perform operations on variables and values.
PHP divides the operators in the following groups:

• Arithmetic operators
• Assignment operators
• Comparison operators
• Increment/Decrement operators
• Logical operators
• String operators
• Array operators

21

PHP Conditional Statements

In PHP we have the following conditional statements:
• if statement - executes some code if one condition is true
• if...else statement - executes some code if a condition is true and another code if that condition is false
• if...elseif....else statement - executes different codes for more than two conditions
• switch statement - selects one of many blocks of code to be executed

22

PHP - The if...elseif....else Statement

if (condition) {
code to be executed if this condition is true;
} elseif (condition) {
code to be executed if this condition is true;
} else {
code to be executed if all conditions are false;
}

23

The PHP switch Statement

switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all labels;
}

24

When use switch statement

Use break to prevent the code from running into the next case automatically. The default statement is used if no match is found.

25

PHP Loops

In PHP, we have the following looping statements:

-while - loops through a block of code as long as the specified condition is true
-do...while - loops through a block of code once, and then repeats the loop as long as the specified condition is true
-for - loops through a block of code a specified number of times
-foreach - loops through a block of code for each element in an array

26

While Loop
The while loop executes a block of code as long as the specified condition is true.
";
$x++;
}
?>


The example below first sets a variable $x to 1 ($x = 1). Then, the while loop will continue to run as long as $x is less than, or equal to 5 ($x <= 5). $x will increase by 1 each time the loop runs ($x++):

27

The PHP do...while Loop

The do...while loop will always execute the block of code once, it will then check the condition, and repeat the loop while the specified condition is true.

";
$x++;
} while ($x <= 5);
?>

The example below first sets a variable $x to 1 ($x = 1). Then, the do while loop will write some output, and then increment the variable $x with 1. Then the condition is checked (is $x less than, or equal to 5?), and the loop will continue to run as long as $x is less than, or equal to 5:

28

The PHP for Loop

The for loop is used when you know in advance how many times the script should run.

";
}
?>
The example below displays the numbers from 0 to 10:

29

Parameters of a for loop


init counter: Initialize the loop counter value
test counter: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends.
increment counter: Increases the loop counter value

30

The PHP foreach Loop

The foreach loop works only on arrays, and is used to loop through each key/value pair in an array.

For every loop iteration, the value of the current array element is assigned to $value and the array pointer is moved by one, until it reaches the last array element.

The following example demonstrates a loop that will output the values of the given array ($colors):

";
}
?>