Last active
July 9, 2025 00:06
-
-
Save Shakil-Shahadat/5ee21ddc3a18466fc11ec28d60674418 to your computer and use it in GitHub Desktop.
★ Miscellaneous PHP Codes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//------------------------------------------------------- | |
// Send Email | |
//------------------------------------------------------- | |
$to = '[email protected]'; | |
$subject = 'Test mail'; | |
$message = 'Hello! This is a simple email message.'; | |
$from = '[email protected]'; | |
$header = 'From: ' . $from; | |
mail( $to, $subject, $message, $header ); | |
echo 'Mail Sent.'; | |
// ToDo: See if mail() returns anything. If so, improve this example. | |
//------------------------------------------------------- | |
// Escape $_GET / $_POST Array | |
//------------------------------------------------------- | |
// For MySQL | |
// Escape $_GET elements | |
foreach ( $_GET as $key => $value ) | |
{ | |
$_GET[ $key ] = $db->escape_string( $_GET[ $key ] ); | |
} | |
// Escape $_POST elements | |
foreach ( $_POST as $key => $value ) | |
{ | |
$_POST[ $key ] = $db->escape_string( $_POST[ $key ] ); | |
} | |
// For SQLite | |
// Escape $_GET elements | |
foreach ( $_GET as $key => $value ) | |
{ | |
$_GET[ $key ] = $db->escapeString( $_GET[ $key ] ); | |
} | |
// Escape $_POST elements | |
foreach ( $_POST as $key => $value ) | |
{ | |
$_POST[ $key ] = $db->escapeString( $_POST[ $key ] ); | |
} | |
//------------------------------------------------------- | |
// Explode / Implode | |
//------------------------------------------------------- | |
// Explode / String to Array | |
$pizza = 'piece1 piece2 piece3 piece4 piece5 piece6'; | |
$pieces = explode( ' ', $pizza ); | |
echo $pieces[ 0 ], PHP_EOL; // piece1 | |
echo $pieces[ 1 ], PHP_EOL; // piece2 | |
// Ref: https://www.php.net/manual/en/function.explode.php | |
// Implode / Array to String | |
$array = [ 'lastname', 'email', 'phone' ]; | |
var_dump( implode( ',', $array ) ); // string(20) "lastname,email,phone" | |
// The separator is optional | |
var_dump( implode( [ 'a', 'b', 'c' ] ) ); // string(3) "abc" | |
// Ref: https://www.php.net/manual/en/function.implode.php | |
//------------------------------------------------------- | |
// Miscellaneous | |
//------------------------------------------------------- | |
// Todays' date in the format 20XX-XX-XX | |
echo date( 'Y-m-d' ); | |
// Todays' date in the format XX-XX-XX hour:min:sec | |
echo date( 'y-m-d h:i:sa' ); | |
// Decode 'encodeURIComponent' encoded string | |
urldecode( ); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment