Home Backend Development PHP Tutorial Detailed explanation of PHP basic function examples

Detailed explanation of PHP basic function examples

Mar 17, 2018 am 09:59 AM
php Example Detailed explanation

This article mainly shares with you detailed examples of PHP basic functions, hoping to help everyone.

1. Functions related to output:

l print(): similar to the content provided by echo output, returning 1

l print_r() Generally used to print arrays , does not print the type

l var_dump(): print the value of the data, also There are types

2. Functions related to time

l date(): Display date and time

            Example: date(“Y-m-dH:i:s”)

l time(): timestamp (number of seconds)

l microtime (): Return the timestamp and microseconds

l strtotime(): Parse the date and time string into a timestamp

3 , Mathematics related functions:

l max($v1,$v2,$v3…) Find the maximum value

l min($v1,$v2,$v3…) Find Minimum value

l rand() random number

l mt_rand() generates a better random number

l round(variable[,N]) Keep n small bits

l ceil() Round up

l floor() Round down

l pow(x,y) Find xY

l abs( )Absolute value

l sqrt(): Find the square root

4. Functions related to functions

l function_exists('function name' ) Determine whether the function exists

l func_get_arg(subscript) Get the parameters corresponding to the specified subscript

l func_get_args(): Get in the custom function All parameters (the return value is an array)

l func_num_args(): Get the number of parameters of the current custom function

5. String-related functions

Basic function strlen(): get the length of the stringmb_strlen($string variable name, character encoding)

l implode (connection method, array): connect into one String

l explode(split character, target string): becomes array

l str_split(string, character length): Split characters according to the specified length The string is obtained as an array

l trim(string[,specified character]): The default is to remove the spaces on both sides of the string

l ltrim(): Remove the

## on the left #l rtrim(): Remove the

l on the right

substr(string, starting position starts from 0 [, length])

l

strstr (string, matching characters): Starting from the specified position, intercepting to the end (can be used to remove the file suffix name)

l strtolower(): all lowercase

l strtoupper() : All uppercase

l ucfirst(): Capitalize the first letter

l strpos (string, matching character): Determine the position where the character appears in the target string (first time)

l strrpos (string, matching characters)

l str_replace (matching target, replacement content, string itself)

l printf/sprintf (output string has placeholder, Sequential placeholder content..)%d %s

l str_repeat(): Repeat a string n times

l str_shuffle(): Shuffle the string randomly

6. Array-related functions

Array sorting functions

l

sort(): Sort by the value of the elements in ascending order (subscript rearrangement)

l

rsort(): Sort by the value of the elements in descending order (subscript rearrangement)

l

asort(): Sort by the value of the elements in ascending order (subscripts retained)

l

arsort(): Sort by the value of the elements in descending order (subscripts reserved) Reserved)

l

ksort(): Array key name (subscript) in ascending order, (subscript reserved)

l

krsort() : Arrange in descending order by key name, (subscripts retained)

l

shuffle(): Randomly shuffle the array elements, and the array subscripts will be rearranged

l Sort: The default is ascending order, if r is descending order)

l Yesa: Just keep the index relationship

l has k: issorted by key name

Pointer

l

reset() resets the pointer and returns the array pointer to the first position

l end() Reset the pointer and point the array pointer to the last element

l next() Move the pointer down to get the value of the next element

l prev() Move the pointer up to get the value of the previous element                                                        Move the pointer)

l key(): Get the subscript value corresponding to the current pointer (without moving the pointer)

Array Others Function

l count(): Count the number of elements in the array

l array_push(): Add an element to the array (After the array)

l array_pop(): Take out an element from the array (After the array)

l array_shift(): From the array Remove an element from the array (in front of the array)

l array_unshift(): Add an element from the array (in front of the array)

l array_reverse( ): Array elements are reversed

l in_array(): Determine whether an element exists in the array

l array_keys(): Get an All subscripts of the array, return an index array

l array_values(): Get all the values ​​of an array, return an index array

l array_merge():Merge one or more arrays

array_merge($arr1,$arr2)

l array_splice():Remove part of the array and replace it with other values

$input

<span style="color:#0000BB;">= array(</span><span style="color:#007700;">"red"</span><span style="color:#DD0000;">, </span><span style="color:#007700;">"green"</span><span style="color:#DD0000;">, </span><span style="color:#007700;">"blue"</span><span style="color:#DD0000;">, </span>##"yellow"<span style="color:#007700;"></span>);<span style="color:#DD0000;"></span><span style="color:#007700;"></span>array_splice
(<span style="color:#0000BB;"></span> $input<span style="color:#007700;"></span>##, <span style="color:#0000BB;"></span>2<span style="color:#007700;"></span>);<span style="color:#0000BB;"></span><span style="color:#007700;">//$input is now array(“red”,”</span> green

”);<span style="color:#0000BB;"></span>l array_chunk():<span style="color:#0000BB;">Split an array into multiple ones</span>l

shuffle():

Shuffle the arrayl

range():

Create an array containing the specified range unitl

is_file():

Judge whether the given file name is a normal filel

define():

Define a constantl

defined():

Check whether a constant with a certain name existsl

ini_set( ):

Set a value for a configuration option ini_set

(<span style="color:#0000BB;"></span>'display_errors'<span style="color:#007700;"></span>, <span style="color:#DD0000;"></span>'1'<span style="color:#007700;"></span>);<span style="color:#DD0000;"></span> l basename():<span style="color:#007700;">Return the file name part of the path</span>l

is_array():

Detect whether the variable is an arrayl

list():

Assign the values ​​in the array to some variables

<span style="color:#0000BB;">$info </span><span style="color:#007700;">= array(</span><span style="color:#DD0000;">'coffee'</span> <span style="color:#007700;">, </span><span style="color:#DD0000;">'brown'</span><span style="color:#007700;">, </span><span style="color:#DD0000;">'caffeine'</span> <span style="color:#007700;">);</span>
<span style="color:#007700;">list(</span><span style="color:#0000BB;">$drink</span><span style="color:#007700;">, </span><span style="color:#0000BB;">$color</span>##, <span style="color:#007700;"></span> $power<span style="color:#0000BB;"></span>##) = <span style="color:#007700;"></span>$info<span style="color:#0000BB;"></span>;<span style="color:#007700;"></span>
echo <span style="color:#007700;"></span>"<span style="color:#DD0000;"></span>$drink<span style="color:#0000BB;"></span> is <span style="color:#DD0000;"></span>$color<span style="color:#0000BB;"></span> and <span style="color:#DD0000;"></span>$power<span style="color:#0000BB;"></span> makes it special.\n"<span style="color:#DD0000;"></span>;<span style="color:#007700;"></span>l

nl2br():

Insert HTML newline mark before all new lines in the stringl

array_rand():

Randomly take out one or more units from the arrayl

count ():

Calculate the number of units in the array or the number of attributes in the objectl

reset():

Point the internal pointer of the array to the first unitRelated recommendations:

Search performance test of PHP function

Sharing of some vulnerability collections of PHP function

Usage example of php function array_walk

The above is the detailed content of Detailed explanation of PHP basic function examples. For more information, please follow other related articles on the PHP Chinese website!

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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Hot Topics

Java Tutorial
1671
14
PHP Tutorial
1276
29
C# Tutorial
1256
24
PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 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 vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

See all articles