Results: 133
strtotime
The following expressions return the same time
echo strtotime("2031-05-28T00:00:00").PHP_EOL;
echo strtotime("2031-05-28 00:00:00").PHP_EOL;
echo strtotime("2031-05-28").PHP_EOL;
The result is:
1937692800
1937692800
1937692800
$password = "your_password";
Hash the password with cost factor
9
$start9 = microtime(true);
$hash9 = password_hash($password, PASSWORD_BCRYPT, ['cost' => 9]);
$end9 = microtime(true);
$executionTime9 = $end9 - $start9;
Hash the password with cost factor
10
$start10 = microtime(true);
$hash10 = password_hash($password, PASSWORD_BCRYPT, ['cost' => 10]);
$end10 = microtime(true);
$executionTime10 = $end10 - $start10;
Calculate the speedup factor
$speedupFactor = $executionTime10 / $executionTime9;

// Display the hashed passwords, execution times, and speedup factor
echo "Hashed password with cost 9: " . $hash9 . "\n";
echo "Execution time with cost 9: " . $executionTime9 . " seconds\n\n";
echo "Hashed password with cost 10: " . $hash10 . "\n";
echo "Execution time with cost 10: " . $executionTime10 . " seconds\n\n";
echo "Speedup factor (9 vs 10): " . $speedupFactor . " times faster\n";
$start = microtime(true);

sleep(3);
// Your script code here

$end = microtime(true);
$latency = round($end - $start, 2);
echo "Script execution time: " . $latency . " seconds";
The
round
function will round the result to
2
decimals
Array to XML with SimpleXMLElement CODE
Built-in class
SimpleXMLElement
converts array to XML
$test_array = array (
  'bla' => 'blub',
  'foo' => 'bar',
  'another_array' => array (
    'stack' => 'overflow',
  ),
);

$xml = new SimpleXMLElement('<rootTag/>');
to_xml($xml, $test_array);
print $xml->asXML();

function to_xml(SimpleXMLElement $object, array $data)
{   
    foreach ($data as $key => $value) {
        if (is_array($value)) {
            $new_object = $object->addChild($key);
            to_xml($new_object, $value);
        } else {
            // if the key is an integer, it needs text with it to actually work.
            if ($key != 0 && $key == (int) $key) {
                $key = "key_$key";
            }

            $object->addChild($key, $value);
        }   
    }   
}
More about array indexes CODE
It's possible to define keys only for some of the array items
$array = [1, 2, "name"=>"Three", 4];
print_r($array);
Result
Array
(
    [0] => 1
    [1] => 2
    [name] => Three
    [2] => 4
)
If we set higher index manually, it will continue adding 1 to the max index
$array = [1, 2, 7=>"Three", 4];
print_r($array);
Result
Array
(
    [0] => 1
    [1] => 2
    [7] => Three
    [8] => 4
)
If we set higher index manually, it will continue adding 1 to the index
$array = [1, 2, 7=>"Three", 4=>"forth", 25];
print_r($array);
Result
Array
(
    [0] => 1
    [1] => 2
    [7] => Three
    [4] => forth
    [8] => 25
)
Only integer and string values are allowed as array keys
$array = [
	1    => "a",
    "1"  => "b",
    "-1"  => "-b",
    '1.5'  => "c",
    true => "d",
];
print_r($array);
Result
Array
(
    [1] => d
    [-1] => -b
    [1.5] => c
)
parse_ini_file
The function
parse_ini_file()
returns the settings as an associative array from
ini
file
session_write_close
Sessions automatically end when the PHP script finishes executing but can be manually ended using the
session_write_close()
function
ctype_alnum
function
ctype_alnum
returns true if the value contains only alphanumeric letters. In the example
IF
statement will be executed because the value is alphanumeric
$var = 'as2df234asdf';

if (ctype_alnum($var)) {
	echo 'if';
} else {
	echo 'else';
}
Returns value of
title
element from
$xml
function value_in($element_name, $xml, $content_only = true) {
    if ($xml == false) {
        return false;
    }
    $found = preg_match('#<'.$element_name.'(?:\s+[^>]+)?>(.*?)'.
            '</'.$element_name.'>#s', $xml, $matches);
    if ($found != false) {
        if ($content_only) {
            return $matches[1];  //ignore the enclosing tags
        } else {
            return $matches[0];  //return the full pattern match
        }
    }
    // No match found: return false.
    return false;
}

$title = value_in('title', $xml);
Sort array of arrays by sub-array values
We need to sort the array by
price
value
$inventory = array(

   array("type"=>"fruit", "price"=>3.50),
   array("type"=>"milk", "price"=>2.90),
   array("type"=>"pork", "price"=>5.43),

);
The solution is to use
array_multisort
function
$price = array();
foreach ($inventory as $key => $row)
{
    $price[$key] = $row['price'];
}
array_multisort($price, SORT_DESC, $inventory);
In PHP 5.5 or later
array_column
function can be used
$price = array_column($inventory, 'price');

array_multisort($price, SORT_DESC, $inventory);
Results: 133