1/61
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
Used to aggregate a series of similar items together, arranging and dereferencing them in some specific way
Array
Each member of the __ __ references a corresponding value and can be a simple numerical reference to the value's position in the series, or it could have some direct correlation to the value
array index
__ __ does not need to declare how many elements that the array variable have
PHP array
Array index in PHP can be also called as __ __
array keys
Array can be used as ordinary array same as in __ and __ arrays
C and C++ arrays
<?php
$fruit[0] = "orange";
$fruit[1] = "apple";
$fruit[2] = "grapes";
$fruit[3] = "banana";
echo "<pre>";
print_r($fruit);
echo "</pre>";
?>
Array
(
[0] => orange
[1] => apple
[2] => grapes
[3] => banana
)
<?php
$fruit[] = "orange";
$fruit[] = "apple";
$fruit[] = "grapes";
$fruit[] = "banana";
echo "<pre>";
print_r($fruit);
echo "</pre>";
?>
Array
(
[0] => orange
[1] => apple
[2] => grapes
[3] => banana
)
<?php
$fruit = array("orange", "apple", "grapes", "banana");
echo "<pre>";
print_r($fruit);
echo "</pre>";
?>
Array
(
[0] => orange
[1] => apple
[2] => grapes
[3] => banana
)
Short for print recursive
print_r() function
This takes an argument of any type and prints it out, which includes printing all its parts recursively
print_r() function
Same as the print_r function except that it prints additional information about the size and type of the values it discovers
var_dump() function
Functions are commonly used for debugging
print_r() and var_dump()
The point of this of these functions is to help you visualize what's going on with compound data structures like arrays
print_r() and var_dump()
<?php
$fruit = array(
"orange",
"apple",
"grapes",
"banana");
echo "<pre>";
print_r($fruit);
echo "</pre>";
?>
Array
(
[0] => orange
[1] => apple
[2] => grapes
[3] => banana
)
<?php
$fruit = array(
"orange",
"apple",
"grapes",
"banana");
echo "<pre>";
var_dump($fruit);
echo "</pre>";
?>
array(4) {
[0] =>
string(6) "orange"
[1] =>
string(5) "apple"
[2] =>
string(6) "grapes"
[3] =>
string(6) "banana"
}
A statement used to iterate or loop through the element in an array. With each loop, a foreach statement moves to the next element in an array.
foreach() function
Specify an array expression within a set of parenthesis following the foreach keyword
foreach statement
foreach($arr as $value) {
//do something with $value variable
}
foreach($arr as $key => $value) {
//do something with $key and/or $value variable
}
foreach() syntax
The name of the array that you're walking through
$arr
The name of the variable where you want to store the key (optional)
$key
The name of the variable where you want to store the value
$value
<?php
$fruit = array(
"orange",
"apple",
"grapes",
"banana",
"dragon fruit");
foreach($fruit as $value) {
echo "$value<br/>";
}
?>
orange
apple
grapes
banana
dragon fruit
Sorts by value; assign new numbers as the keys
sort($array)
Sorts by value in reverse order; assign new number as the keys
rsort($array)
Sorts by value; keeps the same key
asort($array)
Sorts by value in reverse order; keeps the same key
arsort($array)
Sorts by key
ksort($array)
Sorts by key in reverse order
krsort($array)
Sorts by a function
usort($array, functionname)
<?php
$fruit = array(
"orange", "apple",
"grapes", "banana",
"guava", "blueberry");
sort($fruit); //sort array in ascending
echo "<pre>";
print_r($fruit);
echo "</pre>";
rsort($fruit); //sort array in descending
echo "<pre>";
print_r($fruit);
echo "</pre>";
?>
Array
(
[0] => apple
[1] => banana
[2] => blueberry
[3] => grapes
[4] => guava
[5] => orange
)
Array
(
[0] => orange
[1] => guava
[2] => grapes
[3] => blueberry
[4] => banana
[5] => apple
)
<?php
$fruit = array(
"orange", "apple",
"grapes", "banana",
"guava", "blueberry");
asort($fruit);
echo "<pre>";
print_r($fruit);
echo "</pre>";
arsort($fruit);
echo "<pre>";
print_r($fruit);
echo "</pre>";
?>
Array
(
[1] => apple
[3] => banana
[5] => blueberry
[2] => grapes
[4] => guava
[0] => orange
)
Array
(
[0] => orange
[4] => guava
[2] => grapes
[5] => blueberry
[3] => banana
[1] => apple
)
<?php
$fruit = array(
'1010' => "orange",
'1002' => "apple",
'1023' => "grapes",
'1006' => "banana",
'1015' => "guava",
'1009' => "blueberry");
ksort($fruit);
echo "<pre>";
print_r($fruit);
echo "</pre>";
krsort($fruit);
echo "<pre>";
print_r($fruit);
echo "</pre>";
?>
Array
(
[1002] => apple
[1006] => banana
[1009] => blueberry
[1010] => orange
[1015] => guava
[1023] => grapes
)
Array
(
[1023] => grapes
[1015] => guava
[1010] => orange
[1009] => blueberry
[1006] => banana
[1002] => apple
)
A group of PHP statements that performs a specific task
Functions
Designed to allow you to reuse the same code in different locations
Functions
Functions that are provided by the user of the program
User Defined Functions
Functions that are built-in into PHP to perform some standard operations
Predefined Functions
Syntax (User Defined Functions):
function name(param) {
//code to be executed by the function
}
Where:
function - is the keyword used to declare a function
name - is the name of the function or function identifier
param - is the formal parameters of the function.
Parameter must follow the rule of naming identifiers
Declared outside a function and is available to all parts of the program
Global Variables
Declared inside a function and is only available within the function in which it is declared
Local Variables
Used to retain the values calls to the same function
Static Variables
Used to aggregate a series of similar items together
Array
Array index references a __ __
corresponding value
__ __ can be simple numerical or have some direct correlation to the value
Array index
Also known as Array Keys
Array index
Used to print the array structure
print_r function
The same as print_r function except it adds additional information about the data of each element
var_dump function
Used to iterate through the element in an array
foreach statement
Using __ __ you can display both the keys and value of each element in the array
foreach statement
PHP provides functions for array manipulation such as ______
sort(), rsort(), asort(), arsort(), ksort(), krsort(), and usort() functions
_________ functions are used to sort elements in the array in ascending order.
sort(), asort(), and ksort()
_________ functions are used to sort elements in the array in descending order
rsort(), arsort(), and krsort()
__ and __ does not maintain its index reference for each values
sort() and rsort()
__, __, __, and __ maintains its reference for each values
asort(), ksort(), arsort(), and krsort()
__ and __ used to sort elements by values
asort() and arsort()
__ and __ used to sort elements by keys
ksort() and krsort()
__ is a group of PHP statements that performs a specific task
Functions
Can be user defined generally defined by the user of the program and predefined that are build in using libraries
Functions
You use __ in different ways. __ can only do something without passing values. You can pass values to a __ and you can ask __ to return a value.
Function/s
Used in PHP to declare a function
function keyword
A function that is declared inside a function is said to be __
hidden
To gain access to a variable that is outside from the function we use the __ __
global keyword
We use __ __ to declare a variable inside a function that will act as accumulator variable this will let the program remember the last value of the variable that was used
static keyword