WT Notes ch3
WT Notes ch3
Array Functions:
Syntax:
count(array, mode)
Parameter Description
Example 1:
<?php
$animals=array("Lion","Tiger","Elephant");
echo count($animals);
?>
Output:
3
Example 2:
<?php
$cars=array (
"Volvo"=>array ("XC60", "XC90"),
"BMW"=>array ("X3", "X5"),
"Toyota"=>array ("Highlander")
);
echo "Normal count: " . count($cars)."<br>";
echo "Recursive count: " . count($cars,1);
?>
Output:
Normal count: 3
Recursive count: 8
Web Technology Unit-3 Notes
Syntax:
The optional second parameter flags may be used to modify the sorting behavior using these values:
SORT_REGULAR - compare items normally; the details are described in the comparison operators
section
SORT_LOCALE_STRING - compare items as strings, based on the current locale. It uses the locale,
which can be changed using setlocale()
Example:
<?php
$cars = array("Volvo", "BMW", "Toyota");
sort($cars,);
$clength = count($cars);
for($x = 0; $x < $clength; $x++) {
echo $cars[$x];
echo "<br>";
}
?>
<?php
$cars = array("Volvo", "BMW", "Toyota");
sort($cars);
?>
Output:
BMW
Toyota
Volvo
Web Technology Unit-3 Notes
Syntax:
Example:
<?php
print_r(array_count_values($array));
?>
Output:
Array( [1] => 2 [hello] => 2 [world] => 1)
Syntax:
Example:
<?php
if (array_key_exists('first', $search_array)) {
?>
Output:
The 'first' element is in the array
Syntax:
Example:
<?php
print_r($stack);
?>
Output:
Array
array_search( ) : Searches the array for a given value and returns the first corresponding key if
successful
Syntax:
array_search(mixed $needle, array $haystack, bool $strict = false): int | string | false
Returns the key for needle if it is found in the array, false otherwise.
If needle is found in haystack more than once, the first matching key is returned. To return the keys
for all matching values, use array_keys() with the optional search_value parameter instead.
Example:
<?php
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
Syntax:
If preserve_keys set to true numeric keys are preserved. Non-numeric keys are not affected by this
setting and will always be preserved.
Example:
<?php
$a=array("a"=>"Volvo","b"=>"BMW","c"=>"Toyota");
print_r(array_reverse($a));
?>
Output:
Array ( [c] => Toyota [b] => BMW [a] => Volvo )
Syntax:
The optional second parameter flags may be used to modify the sorting behavior using these values:
Example:
<?php
$input = array("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique($input);
print_r($result);
?>
Output:1
Array ( [a] => green [0] => red [1] => blue )
array_values( ) : returns all the values from the array and indexes the array numerically
Syntax:
Example:
<?php
"gold");print_r(array_values($array));
?>
Output:
Array ( [0] => XL [1] => gold )
Web Technology Unit-3 Notes
Syntax:
Example:
<?php
if (in_array("Irix", $os)) {
if (in_array("mac", $os)) {
?>
Output:
Got Irix
Syntax:
Example:
<?php
function myfunction($value,$key)
}
Web Technology Unit-3 Notes
$a=array("a"=>"red","b"=>"green","c"=>"blue");
array_walk($a,"myfunction");
?>
Output:
The key a has the value red
String Functions
explode( ) : Returns an array of strings, each of which is a substring of string formed by splitting it
on boundaries formed by the string separator.
Syntax:
If limit is set and positive, the returned array will contain a maximum of limit elements with the last
element containing the rest of string.
If the limit parameter is negative, all components except the last -limit are returned.
Example:
<?php
?>
Output:
Array ( [0] => Hello [1] => world. [2] => Have [3] => a [4] => nice[5] => day. )
Web Technology Unit-3 Notes
implode( ) : Returns a string containing a string representation of all the array elements in the
same order, with the separator string between each element.
Syntax:
Example:
<?php
$arr = array('Hello','World!','nice','Day!');
?>
Output:
Hello World! Nice Day!
trim ( ) : Strip whitespace (or other characters) from the beginning and end of a string
Syntax:
Example:
<?php
$str = "Hello World!";
echo $str . "<br>";
echo trim($str,"Hed!");
?>
Output:
Hello World!
llo Worl
md5 ( ) : Calculates the MD5 hash of string and returns that hash.
Syntax:
Example:
<?php
Web Technology Unit-3 Notes
$str = "Hello";
echo md5($str);
?>
Output:
8b1a9953c4611296a827abf8c47804d7
Syntax:
Example:
<?php
if (str_contains($string, 'lazy')) {
Output:
The string 'lazy' was found in the string
str_replace ( ) : Returns a string or an array with all occurrences of string in main string replaced
with the given string.
Syntax:
Example:
<?php
?>
Output:
Hello Ankit!
Web Technology Unit-3 Notes
Syntax:
Example:
<?php
print_r(str_split("Hello"));
?>
Output:
Array ( [0] => H [1] => e [2] => l [3] => l [4] => o )
strcmp ( ) : Compare two strings (case-sensitive). Returns 0 if two strings are equal, <0 if string1
is less than string2 and >0 if string1 is greater than string2.
Syntax:
Example:
<?php
echo strcmp("Hello world!","Hello world!"); // the two strings are equal
echo <"br">;
echo strcmp("Hello world!","Hello"); // string1 is greater than string2
echo <"br">;
echo strcmp("Hello world!","Hello world! Hello!"); // string1 is less than string2
?>
Output:
0
-7
Web Technology Unit-3 Notes
Syntax:
Example:
<?php
echo $len;
?>
Output:
12
strpos ( ) : Returns the position of the first occurrence of a string inside another string (case-
sensitive)
Syntax:
Example:
<?php
echo strpos("I love php, I love php too!","php");
?>
Output:
7
Syntax:
Example:
<?php
?>
Output:
hello world
Web Technology Unit-3 Notes
Example:
<?php
echo strtoupper("Hello WORLD!");
?>
Output:
HELLO WORLD
Syntax:
Returns a string formatted according to the given format string using the given integer timestamp or
the current time if no timestamp is given. In other words, timestamp is optional.
S - The English ordinal suffix for the day of the month (2 characters st, nd, rd or th.
Works well with j)
a - Lowercase am or pm
A - Uppercase AM or PM
Z - Timezone offset in seconds. The offset west of UTC is negative, and the offset
east of UTC is positive (-43200 to 43200)
r - The RFC 2822 formatted date (e.g. Thu, 21 Dec 2000 16:01:07 +0200)
U - The seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
Optional. This is an integer Unix timestamp that defaults to the current local time if
timestamp
a timestamp is not given. In other words, it defaults to the value of time().
Example:
<?php
echo "Today is " . date("Y/m/d") . "<br>";
echo "Today is " . date("Y.m.d") . "<br>";
echo "Today is " . date("Y-m-d") . "<br>";
echo "Today is " . date("l");
?>
Output:
Today is 2024/01/06
Today is 2024.01.06
Today is 2024-01-06
Today is Sunday
Syntax:
Checks the validity of the date formed by the arguments. A date is considered valid if each parameter
is properly defined.
Example:
<?php
var_dump(checkdate(12, 31, 2000));
var_dump(checkdate(2, 29, 2001));
?>
Output:
bool(true)
bool(false)
Web Technology Unit-3 Notes
date_parse( ): Returns associative array with detailed info about given date/time
Syntax:
Example:
<?php
print_r(date_parse("2022-05-01 12:30:45.5"));
?>
Output:
Array ( [year] => 2022 [month] => 5 [day] => 1 [hour] => 12 [minute] => 30 [second] =>
45 [fraction] => 0.5 [warning_count] => 0 [warnings] => Array ( ) [error_count] => 0
[errors] => Array ( ) [is_localtime] => ))
getdate( ): Returns an array that contains date and time information for a Unix timestamp.
Syntax:
Returns an associative array containing the date information of the timestamp, or the current local
time if no timestamp is given.
Example:
<?php
$today = getdate();
print_r($today);
?>
Output:
Array
[seconds] => 40
[minutes] => 58
Web Technology Unit-3 Notes
[hours] => 21
[mday] => 17
[wday] => 2
[mon] => 6
Syntax:
Example:
<?php
$t=time();
echo($t . "<br>");
echo(date("Y-m-d",$t));
?>
Output:
1407124248
2014-08-03
Syntax:
Parameter Description
hour Optional. Specifies the hour
is_dst Optional. Parameters always represent a GMT date so is_dst doesn't influence
the result.
Example:
<?php
?>
Output:
Last day in Feb 2000 is: 29
File Functions
Syntax:
fopen(string $filename, string $mode, bool $use_include_path = false, resource $context = null):
resource | false
Web Technology Unit-3 Notes
mode Description
'r' Open for reading only; place the file pointer at the beginning of the file.
'r+' Open for reading and writing; place the file pointer at the beginning of the file.
'w' Open for writing only; place the file pointer at the beginning of the file and truncate the file
to zero length. If the file does not exist, attempt to create it.
'w+' Open for reading and writing; otherwise it has the same behavior as 'w'.
'a' Open for writing only; place the file pointer at the end of the file. If the file does not exist,
attempt to create it. In this mode, fseek() has no effect, writes are always appended.
'a+' Open for reading and writing; place the file pointer at the end of the file. If the file does not
exist, attempt to create it. In this mode, fseek() only affects the reading position, writes are
always appended.
'x' Create and open for writing only; place the file pointer at the beginning of the file. If the
file already exists, the fopen() call will fail by returning false and generating an error of
level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to
specifying O_EXCL|O_CREAT flags for the underlying open(2) system call.
'x+' Create and open for reading and writing; otherwise it has the same behavior as 'x'.
'c' Open the file for writing only. If the file does not exist, it is created. If it exists, it is neither
truncated (as opposed to 'w'), nor the call to this function fails (as is the case with 'x'). The
file pointer is positioned on the beginning of the file. This may be useful if it's desired to
get an advisory lock (see flock()) before attempting to modify the file, as using 'w' could
truncate the file before the lock was obtained (if truncation is desired, ftruncate() can be
used after the lock is requested).
'c+' Open the file for reading and writing; otherwise it has the same behavior as 'c'.
'e' Set close-on-exec flag on the opened file descriptor. Only available in PHP compiled on
POSIX.1-2008 conform systems.
Example:
<?php
?>
Web Technology Unit-3 Notes
Syntax:
stream - A file system pointer resource that is typically created using fopen().
Example:
<?php
$filename = "/usr/local/something.txt";
fclose($handle);
?>
Syntax:
fwrite() writes the contents of data to the file stream pointed to by stream.
Example:
<?php
$file = fopen("test.txt","w");
fclose($file);
?>
Output:
21
Web Technology Unit-3 Notes
Syntax:
Example:
< ?php
fclose($handle);
?>
Syntax:
Example:
<?php
$file = 'example.txt';
$newfile = 'example.txt.bak';
if (!copy($file, $newfile)) {
?>
Syntax:
Example:
<?php
$filename = '/path/to/foo.txt';
if (file_exists($filename)) {
} else {
?>
Syntax:
Example:
<?php
$filename = 'somefile.txt';
?>
Syntax:
Example:
<?php
$filename = 'somefile.txt';
?>
Syntax:
Example:
<?php
if (!is_dir('examples')) {
mkdir('examples');
rmdir('examples');
?>
Syntax:
Example:
<?php
fclose($fh);
unlink('test.html');
?>