Generales Flashcards
What is a variable
Variables are “containers” for storing information.
What is a variable
Variables are “containers” for storing information.
How is defined a variable
$name = 0;
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)
PHP Data Types
PHP supports the following data types: • String • Integer • Float (floating point numbers - also called double) • Boolean • Array • Object • NULL • Resource
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
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
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;
}
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; }
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.
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
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++):
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:
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:
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