search
HomeBackend DevelopmentPHP TutorialDetailed explanation of PHP basic function examples

Detailed explanation of PHP basic function examples

Mar 17, 2018 am 09:59 AM
phpExampleDetailed 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
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

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment