


Summary of commonly used output functions in PHP, php output function_PHP tutorial
Summary of commonly used output functions in PHP, php output functions
echo(); "Output content";
Can output multiple strings at the same time, can have multiple parameters, does not require parentheses, and has no return value.
print(); has a return value of 1, and 0 can only contain one parameter
Only one string and one parameter can be output at the same time. Parentheses are required and there is a return value. When the execution fails, it returns false.
The usage of print is very similar to that of C language, so % in the output content will be specially interpreted.
$a=print(‘hi’);
echo $a;
//——————————-
hi 1 //1 is the value of $a.
//——————————–
die(); Generally used to judge database connection. Once die() is executed, the content after die() will not be executed
die(); // The difference between and exit().
Has two functions: first output the content, and then exit the program. (commonly used in linked servers and databases)
mysql_connect("locahost","root","root") or die("Link to server failed!");
printf(); The same usage as C language, you can format the output
printf(); //f refers to format formatting
printf(“Parameter 1″, Parameter 2): Parameter 1= what format to output; Parameter 2= output variable.
(% s: according to string; %d: according to integer; %b: according to binary; % x: according to hexadecimal; %X: according to hexadecimal uppercase output; %o: according to octal; % f: by floating point type)
Function, returns the number of output characters, formats the text and then outputs it, such as:
printf (“$%01.2f” , 43.2); //$43.20
$ represents the padding character
0 means there are not enough digits to fill in without affecting the original value
1 represents the total width of the output
2 represents the number of decimal places, with rounding
%f means displaying it as a floating point number
Formatting commands and instructions:
%% prints the percent symbol without conversion.
%b Convert integer to binary.
%c Convert integer to corresponding ASCII character.
%d Convert integer to decimal.
%f times precision number converted to floating point number.
%o Convert integer to octal.
%s Convert integer to string.
%x integer is converted to lower case hexadecimal.
%X Convert integer to uppercase hexadecimal.
$num=100.001;
printf(“%d”,$num); //100
printf(“%s”,$num); //100.001
printf(“%s—%d—%b—%x—%o—%f”,$num,$num,$num,$num,$num,$num)
//100.001—100—1100100—64—144—1001.00100
printf(“%.2f”,$num); //100.00 (keep 2 decimal places)
printf(“%.1f”,$num); //100.0 (retain 1 decimal place)
printf(“%`#10s”,$num); //#10s
printf(“%#10s”,$num); //10s
?>
sprintf; Store the output content in a variable
This cannot be output directly. First assign it to a variable and then output the variable.
$num=100.001;
$a=sprintf(“%d”,$num);
echo $a; //100
?>
print_r(); is used to output array
Function: Only used to output arrays.
$a = array (1, 2, array (“a”, “b”, “c”));
print_r ($a);
Return:
Array ( [0] => 1 [1] => 2 [2] => Array ( [0] => a [1] => b [2] => c ) )
var_dump(); can output any content
Output the content, type, or string content, type, and length of the variable. Often used for debugging.
$a=100;
var_dump($a); //int(100)
$a=100.356;
var_dump($a); //float(100.356)
?>
var_export ();
Returns structural information about the variables passed to this function. It is similar to var_dump(), except that the returned representation is legal PHP code.
You can return the value of a variable by setting the second parameter of the function to TRUE.
$a = array (1, 2, array (“a”, “b”, “c”));
var_export ($a);
/*
Output:
array (
0 => 1,
1 => 2,
2 =>
array (
0 => ‘a’,
1 => ‘b’,
2 => ‘c’,
),
)
*/
$b = 3.1;
$v = var_export($b, TRUE);
echo $v;
/*
Output:
3.1
*/
?>
Simplified usage:
$color = "red";
?>
Roses are =$color?>
It’s not difficult to master some frequently used functions. Use it a few times and remember it.
Then check the manual when you encounter something you don’t know
In fact, it is impossible for everyone who does PHP to know all the functions. Remember, but you must remember the frequently used ones, otherwise it will be too shameful.
The manual is very important, check it out if you don’t know how to do it.
Okay, answer your questions one by one.
You must use functions frequently in your work.
Functions are definitely very important
Frequent use of functions will speed up the efficiency of function learning. For example, if you make a case and use all the available functions, you will remember many functions
Commonly used functions The functions are string processing and array processing
In fact, there is no need to search everywhere for so-called "commonly used functions". The so-called "commonly used functions" vary from person to person. Someone often uses a certain function, but it does not mean that you will also use it frequently. There are many PHP functions. It is key to always have a PHP help manual. If you encounter something you are not familiar with, check it out. The more times you check it, the more often it will become "commonly used".
Generally speaking, string, array, and database class functions are relatively commonly used categories.
The functions are listed below for reference only.
==========================================
//================================Time and Date========= ======================
//y returns the last two digits of the year, the four-digit number of year Y, the number of month m, and the English month of M. d number of the month, D day of the week in English
$date=date("Y-m-d");
//include,include_once.require,require_once
//require("file.php" ) Before the PHP program is executed, the files specified by require will be read in. If an error occurs, it will be fatal.
//include("file.php") can be placed anywhere in the PHP program. The file specified by include will not be read until the PHP program is executed. If an error occurs,
//= ==============================Output printing================== =============
//sprintf("%d","3.2") ;//Only formatting, return the formatted string, no output.
//printf("%d","3.2") ;//Formatting and output
//print("3.2") ;//Only output
//echo "nihao" ,"aa";//Can output multiple strings
//print_r(array("a","b","c"));//Display the key values and elements of the array in sequence
//============================== Commonly used string functions========== =====================
//Get the length of the string, how many characters there are, spaces are also counted
$str=" sdaf sd ";
$len=strlen($str);
//Use the string in the first parameter to connect each element in the subsequent array and return a string.
$str=implode("-",array("a","b","c"));
//String splitting method, returns an array, using the first parameter The characters in will split the following string, and intercept the characters before, after, and between the specified characters. If the specified character is at the beginning or end, the element at the beginning or end of the returned array will be an empty string
//If it is not divided into a string, Returns a null value to the corresponding element of the array. The last limit returns the length of the array. If there is no limit, it will continue to be divided.
$array=explode("a","asdd...the rest of the text>>

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
