search
HomeBackend DevelopmentPHP TutorialSummary 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.

Copy code The code is as follows:

$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)

Copy code The code is as follows:

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:

Copy code The code is as follows:

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.

Copy code The code is as follows:

$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.

Copy code The code is as follows:

$num=100.001;
$a=sprintf(“%d”,$num);
echo $a; //100
?>

print_r(); is used to output array

Function: Only used to output arrays.

Copy code The code is as follows:

$a = array (1, 2, array (“a”, “b”, “c”));
print_r ($a);

Return:
Copy code The code is as follows:

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.

Copy code The code is as follows:

$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.

Copy code The code is as follows:

$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:

Copy code The code is as follows:

$color = "red";
?>

Roses are =$color?>


php common functions

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

php common functions

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>>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/882915.htmlTechArticleSummary of commonly used output functions in PHP, php output function echo(); "output content"; can output multiple items at the same time A string, can have multiple parameters, does not require parentheses, and has no return value. prin...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.