PHP Unit-4
PHP Unit-4
1st way:
$season=array("summer","winter","spring","autumn");
2nd way:
$season[0]="summer";
$season[1]="winter";
$season[2]="spring";
$season[3]="autumn";
Example
File: array1.php
<?php
$season=array("summer","winter","spring","autumn");
echo "Season are: $season[0], $season[1], $season[2] and $season[3]";
?>
Output:
Season are: summer, winter, spring and autumn
File: array2.php
<?php
$season[0]="summer";
$season[1]="winter";
$season[2]="spring";
$season[3]="autumn";
echo "Season are: $season[0], $season[1], $season[2] and $season[3]";
?>
Output:
Season are: summer, winter, spring and autumn
(2) PHP Associative Array
We can associate name with each array elements in PHP using => symbol.
There are two ways to define associative array:
1st way:
$salary=array("Sonoo"=>"350000","John"=>"450000","Kartik"=>"200000");
2nd way:
$salary["Sonoo"]="350000";
$salary["John"]="450000";
$salary["Kartik"]="200000";
Example
File: arrayassociative1.php
<?php
$salary=array("Sonoo"=>"350000","John"=>"450000","Kartik"=>"200000");
echo "Sonoo salary: ".$salary["Sonoo"]."<br/>";
echo "John salary: ".$salary["John"]."<br/>";
echo "Kartik salary: ".$salary["Kartik"]."<br/>";
?>
Output:
Sonoo salary: 350000
John salary: 450000
Kartik salary: 200000
File: arrayassociative2.php
<?php
$salary["Sonoo"]="350000";
$salary["John"]="450000";
$salary["Kartik"]="200000";
echo "Sonoo salary: ".$salary["Sonoo"]."<br/>";
echo "John salary: ".$salary["John"]."<br/>";
echo "Kartik salary: ".$salary["Kartik"]."<br/>";
?>
Output:
Sonoo salary: 350000
John salary: 450000
Kartik salary: 200000
Example:
$fruits = array("apple", "banana", "orange");
echo "The current element in the array is: " .
current($fruits);
(3) next() - Advances the internal pointer of an array by one and returns the new current
element.
Syntax: next($array)
Example:
$fruits = array("apple", "banana", "orange");
echo "The next element in the array is: " . next($fruits);
(4) previous() - Rewinds the internal pointer of an array by one and returns the previous
element.
Syntax: prev($array)
Example:
$fruits = array("apple", "banana", "orange");
end($fruits); // move the pointer to the end of the array
echo "The previous element in the array is: " . prev($fruits);
(5) end() - Moves the internal pointer of an array to the last element and returns it.
Syntax: end($array)
Example:
$fruits = array("apple", "banana", "orange");
echo "The last element in the array is: " . end($fruits);
Example:
$fruits = array("orange", "apple", "banana");
sort($fruits);
print_r($fruits);
Example:
$fruits = array("orange", "apple", "banana");
rsort($fruits);
print_r($fruits);
Output:
Array
(
[0] => orange
[1] => banana
[2] => apple
)
(8) assort() - Sorts an associative array by its values in ascending order, while maintaining
the key-value associations.
Syntax: asort($array)
Example:
$age = array("Peter"=>35, "John"=>25, "Mary"=>30);
asort($age);
print_r($age);
Output:
Array
(
[John] => 25
[Mary] => 30
[Peter] => 35
)
(9) arsort() - Sorts an associative array by its values in descending order, while maintaining
the key-value associations.
Syntax: arsort($array)
Example:
$age = array("Peter"=>35, "John"=>25, "Mary"=>30);
arsort($age);
print_r($age);
Example:
$fruits1 = array("apple", "banana");
$fruits2 = array("orange", "kiwi");
$fruits3 = array("grape", "mango");
$all_fruits = array_merge($fruits1, $fruits2, $fruits3);
print_r($all_fruits);
Output:
Array
(
[0] => banana
[1] => orange
[2] => kiwi
)
Example:
$fruits = array("banana", "orange");
array_unshift($fruits, "apple", "kiwi");
print_r($fruits);
Example:
$age = array("Peter"=>35, "John"=>25, "Mary"=>30);
$keys = array_keys($age);
print_r($keys);
Output
Array
(
[0] => Peter
[1] => John
[2] => Mary
)
(14) array_key_exists() - Checks if a given key exists in an array.
Syntax: array_key_exists($key, $array)
Example:
$age = array("Peter"=>35, "John"=>25, "Mary"=>30);
if (array_key_exists("John", $age)) {
echo "John is in the array.";
} else {
echo "John is not in the array.";
}
Output
John is in the array.
Example:
$fruits = array("apple", "banana");
array_push($fruits, "orange", "kiwi");
print_r($fruits);
Example:
$fruits = array("apple", "banana", "orange");
$last_fruit = array_pop($fruits);
print_r($fruits);
echo "The last fruit was: " . $last_fruit;
Output
Array
(
[0] => apple
[1] => banana
)
The last fruit was: orange
Example:
$age = array(35, 25, 30);
$name = array("Peter", "John", "Mary");
array_multisort($age, SORT_DESC, SORT_NUMERIC, $name,
SORT_ASC,
(18) array_search() - Searches an array for a given value and returns the corresponding key
if successful.
Syntax: array_search($value, $array, $strict)
Example:
$fruits = array("apple", "banana", "kiwi", "orange");
$key = array_search("kiwi", $fruits);
echo "The key of kiwi is: " . $key;
Output
The key of kiwi is: 2
Here's an example of a user-defined function that takes two parameters and returns their sum:
function add($num1, $num2) {
$sum = $num1 + $num2;
return $sum;
}
In this example, the function is named add and accepts two parameters $num1 and $num2.
The function then calculates the sum of the two numbers and returns the result using the
return keyword.
To call the add function and pass it two numbers as arguments, you would use the following
code:
$result = add(2, 3);
echo $result; // Outputs 5
This would output 5 because the add function adds the two arguments 2 and 3 and returns the
result.
You can define any number of user-defined functions to perform specific tasks and make
your code more modular and easier to read. When creating user-defined functions, it's
important to choose descriptive names and to ensure that the function performs only one task
to keep your code organized and maintainable.
Here's an example of a PHP function that accepts two arguments and returns their product:
function multiply($num1, $num2) {
$product = $num1 * $num2;
return $product;
}
In this example, the multiply() function accepts two arguments $num1 and $num2. It then
multiplies these two numbers and returns the product using the return keyword.
To call the multiply() function and pass it two numbers as arguments, you would use the
following code:
This would output 6 because the multiply() function multiplies the two arguments 2 and 3
and returns the result.
You can pass any number of arguments to a PHP function, and the function can use these
arguments to perform specific tasks. It's important to ensure that the function's parameters
match the data types and formats of the arguments that will be passed to it to avoid errors and
unexpected results.
(2) const: Defines a constant with a specified name and value. The value cannot be changed
during the script's execution. Here's an example:
const MAXIMUM_VALUE = 100;
echo MAXIMUM_VALUE; // Outputs 100
(3) include(): Includes and evaluates the specified file. If the file is not found, it generates a
warning and continues execution. Here's an example:
include 'myFile.php';
(4) require(): Includes and evaluates the specified file. If the file is not found, it generates a
fatal error and stops execution. Here's an example:
require 'myFile.php';
(5) die() / exit(): Terminates the script's execution and outputs a message. Here's an example:
if ($value < 0) {
die('Error: Value must be positive.');
}
In the above example, if the condition is true, the script will terminate and output the error
message.
These functions can be used in various ways to accomplish specific tasks in PHP scripts. It's
important to use them correctly and to understand their specific behaviors to avoid errors and
unexpected results.