PHP concise functions (PHP simple and clear function syntax)

Source: Internet
Author: User
Tags php book shuffle

1. Related to mysql

Mysql_connect
Establish a connection with the MySQL server
Syntax

Resource mysql_connect (string server [, string usingname [, string password [, bool new_link [, int client_flags])
Eg:

Copy codeThe Code is as follows: $ DB_HOST = "localhost ";
$ DB_LOGIN = "root ";
$ DB_PASSWORD = "123456 ";
$ Conn = mysql_connect ($ DB_HOST, $ DB_LOGIN, $ DB_PASSWORD );

Mysql_data_seek
Move the internal query pointer to the query row
Syntax

Bool mysql_data_seek (resource result_indetifier, int row_number)
Eg:Copy codeThe Code is as follows: $ DB_HOST = "localhost ";
$ DB_LOGIN = "root ";
$ DB_PASSWORD = "123456 ";
$ DB_NAME = "flag ";
$ Conn = mysql_connect ($ DB_HOST, $ DB_LOGIN, $ DB_PASSWORD );
Mysql_select_db ($ DB_NAME );
$ Res = mysql_query ("SELECT * from product ");
$ Row = mysql_fetch_array ($ res );
For ($ I = 0; $ I <$ num; $ I ++)
$ Row = mysql_fetch_array ($ res );
Mysql_data_seek ($ res, 0); // move the pointer back to the first row of the query result

Mysql_fetch_array
Store the query results in an array (one record for each array element)
Syntax

Array mysql_fetch_array (resource result [, int result_type])
EgCopy codeThe Code is as follows: $ DB_HOST = "localhost ";
$ DB_LOGIN = "root ";
$ DB_PASSWORD = "123456 ";
$ DB_NAME = "flag ";
$ Conn = mysql_connect ($ DB_HOST, $ DB_LOGIN, $ DB_PASSWORD );
Mysql_select_db ($ DB_NAME );
$ Res = mysql_query ("SELECT * from product ");
$ Row = mysql_fetch_array ($ res );

Mysql_fetch_object
A row of query results is obtained and stored as an object. The usage of MySQL_fetch_array () is the same. The difference is that mysql_fetch_object () can only obtain query results by field name.
Echo $ row-> fieldname; // correct usage
Echo $ row-> 0; // incorrect usage
Syntax

Object mysql_fetch_object (resource result)
EgCopy codeThe Code is as follows: $ DB_HOST = "localhost ";
$ DB_LOGIN = "root ";
$ DB_PASSWORD = "123456 ";
$ DB_NAME = "flag ";
$ Conn = mysql_connect ($ DB_HOST, $ DB_LOGIN, $ DB_PASSWORD );
Mysql_select_db ($ DB_NAME );
$ Res = mysql_query ("SELECT * from product ");
$ Row = $ mysql_fetch_object ($ res );
While ($ row)
{
Echo $ row à p _ id;
Echo $ row à p _ name;
}

Mysql_insert_id
After adding a message using the INSERT command, you can use this function to obtain the unique id of the newly added record.
Syntax

Int mysql_insert_id ([esource link_identifier])
EgCopy codeThe Code is as follows: $ DB_HOST = "localhost ";
$ DB_LOGIN = "root ";
$ DB_PASSWORD = "123456 ";
$ DB_NAME = "flag ";
$ Conn = mysql_connect ($ DB_HOST, $ DB_LOGIN, $ DB_PASSWORD );
Mysql_select_db ($ DB_NAME );
$ SQLStr "insert into produce (p_id, p_name) VALUES ('', 'php Book ')";
$ Res = mysql_query ($ res );
$ P_id = mysql_insert_id ();

Mysql_num_rows
There are several rows in the query result.
Syntax

Int mysql_num_rows (resource result)
EgCopy codeThe Code is as follows: $ DB_HOST = "localhost ";
$ DB_LOGIN = "root ";
$ DB_PASSWORD = "123456 ";
$ DB_NAME = "flag ";
$ Conn = mysql_connect ($ DB_HOST, $ DB_LOGIN, $ DB_PASSWORD );
Mysql_select_db ($ DB_NAME );
$ Res = mysql_query ("SELECT * from product ");
$ Num = mysql_num_rows ($ res );

Mysql_query
Send a query statement of SQL syntax
Syntax

Resource mysql_query (string query [, resource link_identifier])
EgCopy codeThe Code is as follows: $ DB_HOST = "localhost ";
$ DB_LOGIN = "root ";
$ DB_PASSWORD = "123456 ";
$ DB_NAME = "flag ";
$ Conn = mysql_connect ($ DB_HOST, $ DB_LOGIN, $ DB_PASSWORD );
Mysql_select_db ($ DB_NAME );
$ Res = mysql_query ("SELECT * from product ");

Mysql_select_db
Select the name of the database to be accessed
Syntax

Bool mysql_select_db (string database_name [, resource link_identifier])
EgCopy codeThe Code is as follows: $ DB_HOST = "localhost ";
$ DB_LOGIN = "root ";
$ DB_PASSWORD = "123456 ";
$ DB_NAME = "flag ";
$ Conn = mysql_connect ($ DB_HOST, $ DB_LOGIN, $ DB_PASSWORD );
Mysql_select_db ($ DB_NAME );

2. File System Functions
Copy
Copy text
Syntax
Bool copy (string source, string dest)
EgCopy codeThe Code is as follows: copy ("abc.txt", "/tmp/newabc.txt ");

Fclose
Close a pointer to open a file
Syntax
Bool fclose (resource handle)
EgCopy codeThe Code is as follows: $ fp = fopen ("abc.txt", "w ");
Fclose ($ fp );

Fgets
Retrieve the column content from the position indicated by the file pointer
Syntax
String fgets (resource handle [, int length])
EgCopy codeThe Code is as follows: $ fp = fopen ("abc.txt", "w ");
$ Txtdata = fgets ($ fp, 4096 );

File
Read the entire file content to the array
Syntax
Array file (string filename [, int use_include_path [, resource context])
EgCopy codeThe Code is as follows: $ content = file ("abc.txt ");

File_exists
Check whether the file exists
Syntax

Bool file_exists (string filename)
EgCopy codeThe Code is as follows: if (file_exists ("abc.txt "))
Echo "this file exists ";
Else
Echo "this file does not exist ";

Filesize
Get File Size
Syntax
Int filesize (string filename)
EgCopy codeThe Code is as follows: $ size = filesize ("abc.txt ");

Fopen
Open a file or url
Syntax
Resource fopen (string filename, string mode [, bool use-include_path [, resource zcontext])
EgCopy codeThe Code is as follows: $ fp = fopen ("abc.txt ");
$ Fp = fopen ("http://www.jb51.net/bacteroid/", "r ");

Fputs
Write data to a file
Syntax
Int fputs (resource handle, string [, int length])
EgCopy codeThe Code is as follows: $ fp = fopen ("abc.txt ");
Fputs ($ fp, "helloworld! ");

Fseek
Set the position indicated by the file pointer
Syntax
Int fseek (resource handle, int offset [, int whence])
EgCopy codeThe Code is as follows: $ fp = fopen ("abc.txt", "w ");
$ Txtdata = fgets ($ fp, 4096 );
Fseek ($ fp, 0); // point the pointer back to the starting position

Mkdir
Create a directory
Syntax
Bool mkdir (string pathname [, int mode [, bool recursive [, resource context])
EgCopy codeThe Code is as follows: mkdir ("ljt/newfolder ");

Unlink
Delete an object
Syntax
Int unlink (string filename );
EgCopy codeThe Code is as follows: unlink ("abc.txt ");

3. Date and Time Functions
Data
Returns the local time/date in the specified format.
Syntax
String date (string format [, int timestamp])
EgCopy codeThe Code is as follows: $ time = date ("Y-m-d g: I: s ");

Getdate
Obtain date and time information
Syntax
Array getdata ([int timestamp])
EgCopy codeThe Code is as follows: $ now = getdate ();
$ Year = $ now ["year"];
$ Month = $ now ["month"];

Gettimeofday
Obtain the current time (including Greenwich Mean Time)
Syntax
Array gettimeofday (void)
EgCopy codeThe Code is as follows: $ time = gettimeofday ();

4. string processing functions
Explode
Splits a string into an Array Based on the specified delimiter.
Syntax
Array explode (string separator, string [, int limit])
EgCopy codeThe Code is as follows: $ str = "a, B, c ";
$ Res = explode (",", $ str); // $ res [0] =

Implode
Concatenates the array content into a string
Syntax
String implode (string glue, array pieces)
EgCopy codeThe Code is as follows: $ newarray = array ('A', 'B', 'C ');
$ Res = implode (",", $ newarray); // $ res = a, B, c

Strlen
Returns the length of a string.
Syntax
Int strlen (string)
EgCopy codeCode: strlen ("www.jb51.net"); // return 15
[C/ode]
Substr
Obtains a specified part of the character string (sub-string)
Syntax
String substr ("www.gxnu.edu.cn",); // return "ww. gxnu"
5. mathematical function library
Unconditionally carry the decimal part of the Floating Point Number
Syntax
Float ceil (float value)
Eg
[Code]
Echo ceil (9.99); // returns 10
Echo ceil (9.12); // returns 10

Cos
Returns the cosine of a floating point value.
Syntax
Float cos (float arg)
EgCopy codeThe Code is as follows: $ numcos = cos (0.5 );

Floor
Remove the decimal part of the floating point number unconditionally.
Syntax
Float floor (floor value)
EgCopy codeThe Code is as follows: echo floor (9.12); // return 9
Echo floor (9.99); // return 9

Rand
Generates a random value in a range.
Syntax
I
Nt rand ([int min, in max])
EgCopy codeThe Code is as follows: $ num = rand (0,100); // generates a random number ranging from 1 to 100.

Round
Rounds the decimal part of a floating point number to carry.
Syntax
Float round (float value)
EgCopy codeCode: float round (9.99) // return 10
Float round (9.12) // return 9

Sin
Returns the sine of a floating point value.
Syntax
Float sin (float arg)
EgCopy codeThe Code is as follows: $ numsin = sin (0.5 );

6. Session Functions
Session_register
Description: variables in one or more sessions
Syntax
Bool session_register (mixed name [, mixed...])
EgCopy codeThe Code is as follows: $ name = "flag ";
Session_register ("name ");

Session_start
Initialize Session information
Syntax
Bool session (void)
EgCopy codeThe Code is as follows: session_start ();

7. array Functions
Count
There are several array functions in the calculation array.
Syntax
Int count (mixed var [, int mode])
EgCopy codeThe Code is as follows: count ($ array );

List
Assign the element values in the array to the variable.
Syntax
Void list (mixed varname, mixed ...)
EgCopy codeThe Code is as follows: $ array = array (a, B, c );
List ($ str1, $ str2, $ str3) = $ array; // $ str1 =

Range
Creates an array within the specified range.
Syntax
Array range (int low, int high [, int step])
EgCopy codeThe Code is as follows: $ array = array (0, 9 );

Shuffle
Re-random sorting of elements in the array
Syntax
Bool shuffle (array)
EgCopy codeThe Code is as follows: shuffle ($ array );

Author: Fungi

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.