usort($arr, function($a, $b) {
return $a['stock'] <=> $b['stock'];
});
The strpos() function returns the position of the first occurrence of a substring in a string. If the substring is not found it returns false. Also note that string positions start at 0, and not 1.
(strpos($mystring, $word) !== false) ? echo "Word Found!" : echo "Word Not Found!";
(in_array('xxx', $arr)) ? /* in array */ : /* not int*/ ;
if (file_exists('example.png')) {
// File exists
}
Detect if the client is using a mobile device
if(preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"])) {
// Is mobile...
}
$url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
Snippet will display the scripts execution time
$time_start = microtime(true);
// Your scripts here...
echo 'Total execution time in seconds: ' . (microtime(true) - $time_start);
$to = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" . 'Reply-To: webmaster@example.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
$url = "http://example.com/file.json";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
$result = curl_exec($curl);
curl_close($curl);
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
Insertint, removing, unseting, reindexing, reversing, merging, sorting, if exit , counting, converting array
//Inserting new items into an array:
$names = array();
array_push($names, 'David', 'John');
//Remove item from an array:
unset($names['David']);
// or...
unset($names[0]);
//Reindex the values after you have removed an item:
$names = array_values($names);
//Reverse an array:
$reversed = array_reverse($names);
//Merge two or more arrays:
$merged = array_merge($arr1, $arr2);
//Return only the array keys:
$keys = array_keys(array('name' => 'David', 'age' => 28));
//Sort an array:
sort($names);
//Sort an array in reverse order:
rsort($names);
//Check if item exists in an array:
if (in_array('David', $names)) {
// item exists
}
//Check if key exists in an array:
if (array_key_exists('name', array('name' => 'David', 'age' => 28))) {
// key exists
}
//Count the number of items in an array:
count($names);
//Convert comma-separated list to array:
$array = explode(',', 'david,john,warren,tracy');