Get the App
SLTechnology News&Howtos  ›  Development  › 

10 tutorials of common PHP code writing methods

Shulou Source: shulou.com Published: 2022-06-03 09:03:25 10月03日 Update

This article mainly explains the "10 PHP common code writing method tutorial", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's train of thought slowly in depth, together to study and learn "10 PHP common code writing method tutorial"!

1. Use the PHP Mail function to send Email

$to = "viralpatel.net@gmail.com"; $subject = "VIRALPATEL.net"; $body = "Body of your message here you can use HTML too. E.g. < br > < b > Bold < / b >"; $headers = "From: Peter\ r\ n"; $headers. = "Reply-To: info@yoursite.com\ r\ n"; $headers. = "Return-Path: info@yoursite.com\ r\ n"; $headers. = "X-Mailer: PHP5\ n"; $headers. = 'MIME-Version: 1.0'. "\ n"; $headers. = 'Content-type: text/html; charset=iso-8859-1'. "\ r\ n"; mail ($to,$subject,$body,$headers);? >

2. 64-bit encoding and decoding in PHP

Function base64url_encode ($plainText) {$base64 = base64_encode ($plainText); $base64url = strtr ($base64,'+ / =','-,'); return $base64url;} function base64url_decode ($plainText) {$base64url = strtr ($plainText,'- _,','+ / ='); $base64 = base64_decode ($base64url); return $base64;}

3. Obtain the remote IP address

Function getRealIPAddr () {if (! empty ($_ SERVER ['HTTP_CLIENT_IP'])) / / check ip from share internet {$ip=$_SERVER [' HTTP_CLIENT_IP'];} elseif (! empty ($_ SERVER ['HTTP_X_FORWARDED_FOR']) / / to check ip is pass from proxy {$ip=$_SERVER [' HTTP_X_FORWARDED_FOR'];} else {$ip=$_SERVER ['REMOTE_ADDR'];} return $ip;}

4. Date formatting

Function checkDateFormat ($date) {/ / match the format of the dateif (preg_match ("/ ^ ([0-9] {4})-([0-9] {2})-([0-9] {2}) $/", $date, $parts) {/ / check weather the date is valid of notif (checkdate ($parts [2], $parts [3], $parts [1])) return true;elsereturn false;} elsereturn false;}

5. Verify Email

$email = $_ POST ['email']; if (preg_match ("~ ([a-zA-Z0-9 thanks% thank you)? ^ _ `{|} ~]) @ ([a-zA-Z0-9]). ([a-zA-Z0-9] {2jue 4}) ~", $email) {echo' This is a valid email.';} else {echo 'This is an invalid email.';}

6. Easily parse XML in PHP

/ / this is a sample xml string$xml_string= " < moleculedb > < molecule name='Benzine' > < symbol > ben < / symbol > < code > A < / code > < / molecule > < molecule name='Water' > < symbol > h3o < / symbol > < code > K < / code > < / molecule > < / moleculedb >"; / load the xml string using simplexml function$xml = simplexml_load_string ($xml_string) / / loop through the each node of moleculeforeach ($xml- > molecule as $record) {/ / attribute are accessted by echo $record ['name'],'; / / node are accessted by-> operator echo $record- > symbol,''; echo $record- > code,'< br / >';}

7. Database connection

'. "n". '< html > < head >'. "n". '< title > 404 Not Found < / title >'. "n". '< / head > < body >'. "n". '< H2 > Not Found < / H2 >'. "n". '< p > The requested URL'. Str_replace (strstr ($_ SERVER ['REQUEST_URI'],'?),'', $_ SERVER ['REQUEST_URI']). ' Was not found on this server. < / p >'. N. '< / body > < / html >'. "n"; exit;} # In any file you want to connect to the database,# and in this case we will name this file db.php# just add this line of php code (without the pound sign): # include "db.php";? >

8. Create and parse JSON data

$json_data = array ('id'= > 1 rolf office = > "rolf",' country'= > 'russia', "office" = > array ("google", "oracle")); echo json_encode ($json_data)

9. Deal with MySQL timestamps

$query = "select UNIX_TIMESTAMP (date_field) as mydate from mytable where 1x1"; $records = mysql_query ($query) or die (mysql_error ()); while ($row = mysql_fetch_array ($records)) {echo $row;}

10. Extract the Zip file

/ Use the code as following:

The common functions of PHP are as follows

1.PHP string

The string declares the variable =''or''(single quotation marks are usually used because it is easier to write)

$str = 'Hello PHP'

Echo $str

Strpos calculates the position of characters in the string (starting at 0)

$str = 'Hello PHP'

Echo strpos ($str,'o'); / / calculates the position of the character in the string

Echo''

Echo strpos ($str,'PH')

Substr intercepts strings

$str = 'Hello PHP';// intercepts the string $str1 = substr ($str,2,3); / / intercepts the string echo $str1 of length 3 starting from position 2

If the length parameter is not passed in, it will be truncated from the specified position to the end of the string

Str_split Segmentation A fixed-length split of a string (default length is 1)

$str = 'Hello PHP';// split string $result = str_split ($str); / / Save the result to an array print_r ($result); / / enter an array echo' using print_r; $result1 = str_split ($str,2); print_r ($result1)

Explode (split characters, strings to be split) is divided by spaces

$str = 'Hello PHP Java C # Clearing gifts' result = explode ('', $str); print_r ($result)

Concatenation of strings

$str = 'Hello PHP Java C # Category strings concatenation / string concatenation $num = 100 leading str1 = $str.'Objective-C'. $num;echo $str1;echo''; $str2 = "$strObjective-C $num"; / / another easy way to write echo $str2 Thank you for your reading, the above is the "10 sections of PHP common coding methods tutorial" content, after the study of this article, I believe you have a deeper understanding of the 10 PHP common coding methods tutorials, the specific use of the need for you to practice and verify. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Characters strings commonly used codes tutorials methods locations lengths learning content situations data arrays caches validation simplicity writing functions functions parameters Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Docker macOS Shulou Information Huawei