WE read a question on the PHP forum where a novice developer had asked advanced PHP tips to learn PHP web development.
We thought to share it here on the blog as well.
Are you ready to explore it?
Want to Develop a Web Application?
Book your free consultation with web app experts.
Click Here To Get Your Free Quote
Php background process code
Index.php
$url = 'backgroundScript.php';
$postfields = ['company_name' => "brainbinary", 'developer_name' => "nik"];
$ch = curl_init($url);
curl_setopt_array($ch, array(CURLOPT_RETURNTRANSFER => true, CURLOPT_NOSIGNAL => 1, CURLOPT_TIMEOUT_MS => 50, CURLOPT_VERBOSE => 1, CURLOPT_POSTFIELDS => http_build_query($postfields), CURLOPT_HEADER => 1, CURLOPT_FRESH_CONNECT => true,));
curl_exec($ch);
curl_close($ch);
$postfields = ['company_name' => "brainbinary", 'developer_name' => "nik"];
$ch = curl_init($url);
curl_setopt_array($ch, array(CURLOPT_RETURNTRANSFER => true, CURLOPT_NOSIGNAL => 1, CURLOPT_TIMEOUT_MS => 50, CURLOPT_VERBOSE => 1, CURLOPT_POSTFIELDS => http_build_query($postfields), CURLOPT_HEADER => 1, CURLOPT_FRESH_CONNECT => true,));
curl_exec($ch);
curl_close($ch);
backgroundScript.php
set_time_limit(0);
header("Connection: close");
ignore_user_abort(true);
// write your background execute code
header("Connection: close");
ignore_user_abort(true);
// write your background execute code
Delete whole folder with it’s content
$dir = 'put your folder url path here'; // ex: '../uploads/media/1
if (is_dir($dir)) {
array_map('unlink', glob($dir . '/*'));
rmdir($dir);
}
if (is_dir($dir)) {
array_map('unlink', glob($dir . '/*'));
rmdir($dir);
}
Print debug log in particular location
index.php
require_once(errorLog.php');
cloud_error_log('put your error result here'); // error result like text, array etc.
cloud_error_log('put your error result here'); // error result like text, array etc.
errorLog.php
function cloud_error_log($errordata) {
$handle = fopen('errorLogFile.txt', 'a+');
$logtext = "******************" . date('d-m-Y h:i:s') . "******************\n\n";
$logtext .= print_r($errordata, true);
$logtext .= "\n\n**********************************************************\n\n";
$errorlog = fwrite($handle, $logtext);
fclose($handle);
chmod('errorLogFile.txt', 0777);
return true;
}
$handle = fopen('errorLogFile.txt', 'a+');
$logtext = "******************" . date('d-m-Y h:i:s') . "******************\n\n";
$logtext .= print_r($errordata, true);
$logtext .= "\n\n**********************************************************\n\n";
$errorlog = fwrite($handle, $logtext);
fclose($handle);
chmod('errorLogFile.txt', 0777);
return true;
}
Getting list of dates from day name of particular year and month
function getMonthDatefromDay($month, $year, $searchdayname) {
$start_date = "01-".$month."-".$year;
$start_time = strtotime($start_date);
$end_time = strtotime("+1 month", $start_time);
for($i=$start_time; $i<$end_time; $i+=86400)
{
if(strtolower(date('D', $i)) == $searchdayname)
$list[] = date('Y-m-d', $i);
}
return $list;
}
print_r(getMonthDatefromDay(09, 2016, 'mon'));
$start_date = "01-".$month."-".$year;
$start_time = strtotime($start_date);
$end_time = strtotime("+1 month", $start_time);
for($i=$start_time; $i<$end_time; $i+=86400)
{
if(strtolower(date('D', $i)) == $searchdayname)
$list[] = date('Y-m-d', $i);
}
return $list;
}
print_r(getMonthDatefromDay(09, 2016, 'mon'));
Encrypt and Decrypt password
function encrypt_decrypt($string, $action='encrypt') {
$encrypt_method = "AES-256-CBC";
$secret_key = 'AA74CDCC2BBRT935136HH7B63C27'; // user define private key
$secret_iv = '5fgf5HJ5g27'; // user define secret key
$key = hash('sha256', $secret_key);
$iv = substr(hash('sha256', $secret_iv), 0, 16); // sha256 is hash_hmac_algo
if( $action == 'encrypt' ) {
$output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
$output = base64_encode($output);
}
else if( $action == 'decrypt' ){
$output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
}
return $output;
}
$encrypt_method = "AES-256-CBC";
$secret_key = 'AA74CDCC2BBRT935136HH7B63C27'; // user define private key
$secret_iv = '5fgf5HJ5g27'; // user define secret key
$key = hash('sha256', $secret_key);
$iv = substr(hash('sha256', $secret_iv), 0, 16); // sha256 is hash_hmac_algo
if( $action == 'encrypt' ) {
$output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
$output = base64_encode($output);
}
else if( $action == 'decrypt' ){
$output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
}
return $output;
}
echo "Your Encrypted password is => ". $pwd = encrypt_decrypt('brainbinary', 'encrypt');
echo '
';
echo "Your Decrypted password is => ". encrypt_decrypt($pwd, 'decrypt');
echo '
';
echo "Your Decrypted password is => ". encrypt_decrypt($pwd, 'decrypt');
Create random Unique Key
echo md5(uniqid(time()));
Display users with suffix. (ex: Facebook users like 10K)
function formatWithSuffix($input)
{
$suffixes = array('', 'K', 'M', 'B', 'T');
$suffixIndex = 0;
while(abs($input) >= 1000 && $suffixIndex < sizeof($suffixes)) {
$suffixIndex++;
$input /= 1000;
}
return ($input > 0 ? floor($input * 1000) / 1000 : ceil($input * 1000) / 1000). $suffixes[$suffixIndex];
}
echo formatWithSuffix('10000');
{
$suffixes = array('', 'K', 'M', 'B', 'T');
$suffixIndex = 0;
while(abs($input) >= 1000 && $suffixIndex < sizeof($suffixes)) {
$suffixIndex++;
$input /= 1000;
}
return ($input > 0 ? floor($input * 1000) / 1000 : ceil($input * 1000) / 1000). $suffixes[$suffixIndex];
}
echo formatWithSuffix('10000');
Multiple array sorting
$data = array(['id'=>1,'name'=>"sameer"],['id'=>2,'name'=>"yagnesh"],['id'=>3,'name'=>"chirag"]);
foreach ($data as $key => $row) {
$id[$key] = $row['id'];
$name[$key] = $row['name'];
}
array_multisort($id, SORT_DESC, $name, SORT_ASC, $data);
print_r($id);
print_r($name);
foreach ($data as $key => $row) {
$id[$key] = $row['id'];
$name[$key] = $row['name'];
}
array_multisort($id, SORT_DESC, $name, SORT_ASC, $data);
print_r($id);
print_r($name);
Get size of the specified URL or file
echo filesize("test.txt");
Plus, bonus tip:
Never use for($i=0; $i
In the above example, the first method will call count function on each iteration for loop, while in the second iteration, count function is being called only once.
Hope, these tips will help you in your next PHP web development project.