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

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

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

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,

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

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

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