Table of Contents
Summary of commonly used output functions in PHP, php output functions
php common functions
Home Backend Development PHP Tutorial Summary of commonly used output functions in PHP, php output function_PHP tutorial

Summary of commonly used output functions in PHP, php output function_PHP tutorial

Jul 13, 2016 am 10:18 AM
php Output function

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


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 of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles