


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

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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

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

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,

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

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

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