PHP Concise function (PHP simple and clear function syntax) _php tips

Source: Internet
Author: User
Tags cos explode getdate php book shuffle sin string format unique id

1, with MySQL-related

Mysql_connect
Establish a connection to the MySQL server
Grammar


Resource mysql_connect (String server[,string usingname[,string password[, BOOL New_link[,int Client_flags]]]
eg

Copy Code code 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
Grammar

BOOL Mysql_data_seek (Resource Result_indetifier,int row_number)
eg
Copy Code code 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 pointer back to the first line of the query fruit




Mysql_fetch_array
To have the results of a query in an array (one record per array element)
Grammar

Array mysql_fetch_array (Resource Result[,int result_type])
eg
Copy Code code 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
Gets a row of query results and stores them as object types, exactly the same as mysql_fetch_array (), except that mysql_fetch_object () can only get query results by field names
Echo $row->fieldname; Correct usage
Echo $row->0; Error usage
Grammar

Object Mysql_fetch_object (Resource result)
eg
Copy Code code 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
Grammar

int mysql_insert_id ([esource Link_identifier])
eg
Copy Code code 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
Get a few lines in the query results
Grammar

int mysql_num_rows (resource result)
eg
Copy Code code 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 out a query statement with SQL syntax
Grammar

Resource mysql_query (String Query[,resource link_identifier])
eg
Copy Code code 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 you want to access
Grammar

BOOL mysql_select_db (String Database_name[,resource link_identifier])
eg
Copy Code code 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 function
Copy
Copy text
Grammar
BOOL Copy (string source,string dest)
eg
Copy Code code as follows:

Copy ("Abc.txt", "/tmp/newabc.txt");

Fclose
Close a pointer to an open file
Grammar
BOOL Fclose (Resource handle)
eg
Copy Code code as follows:

$FP =fopen ("Abc.txt", "w");
Fclose ($FP);

Fgets
Get the contents of a column from the point where the file pointer refers
Grammar
String fgets (Resource handle[,int length])
eg
Copy Code code as follows:

$FP =fopen ("Abc.txt", "w");
$txtdata = Fgets ($fp, 4096);

File
Read the entire contents of the file into an array
Grammar
Array file (string Filename[,int use_include_path[,resource context])
eg
Copy Code code as follows:

$content =file ("Abc.txt");


File_exists
Check to see if a file exists
Grammar

BOOL File_exists (string filename)
eg
Copy Code code as follows:

if (file_exists ("Abc.txt"))
echo "This file exists";
Else
echo "This file does not exist";

FileSize
Get File size
Grammar
int filesize (string filename)
eg
Copy Code code as follows:

$size =filesize ("Abc.txt");

fopen
Open a file or URL
Grammar
Resource fopen (string filename,string mode[,bool use-include_path[,resource Zcontext])
eg
Copy Code code as follows:

$FP =fopen ("Abc.txt");
$FP =fopen ("Http://www.jb51.net/bacteroid/", "R");


Fputs
Write data to a file
Grammar
int fputs (Resource handle,string string[,int length])
eg
Copy Code code as follows:

$FP =fopen ("Abc.txt");
Fputs ($fp, "helloworld!");


Fseek
Set the location of the file pointer
Grammar
int fseek (Resource handle,int offset[,int whence])
eg
Copy Code code as follows:

$FP =fopen ("Abc.txt", "w");
$txtdata =fgets ($fp, 4096);
Fseek ($fp, 0);//point the pointer back at the beginning


Mkdir
Create a Directory
Grammar
BOOL MkDir (String Pathname[,int mode[,bool Recursive[,resource Context]])
eg
Copy Code code as follows:

mkdir ("Ljt/newfolder");


Unlink
deleting files
Grammar
int unlink (string filename);
eg
Copy Code code as follows:

Unlink ("Abc.txt");

3, date and time function
Data
Returns the local time/date in the specified format
Grammar
String Date (string format[,int timestamp])
eg
Copy Code code as follows:

$time =date ("y-m-d g:i:s");


GetDate
Get information on date and time
Grammar
Array GetData ([int timestamp])
eg
Copy Code code as follows:

$now =getdate ();
$year = $now ["Year"];
$month = $now ["Month"];


Gettimeofday
Get the current time (including GMT)
Grammar
Array gettimeofday (void)
eg
Copy Code code as follows:

$time =gettimeofday ();

4, String processing functions
Explode
Splits a string into an array based on the specified delimiter
Grammar
Array explode (string separator,string string[,int limit])
eg
Copy Code code as follows:

$str = "A,b,c";
$res =explode (",", $str);/$res [0]=a


Implode
Concatenate the contents of an array into a string
Grammar
String implode (string Glue,array pieces)
eg
Copy Code code as follows:

$newarray =array (' A ', ' B ', ' C ');
$res =implode (",", $newarray);/$res =a,b,c

Strlen
Gets the length of the string
Grammar
int strlen (String string)
eg
Copy Code code as follows:

strlen ("www.jb51.net");/return 15
[C/ode]
Substr
Gets a part of the character specified by the string (substring)
Grammar
String substr ("www.gxnu.edu.cn", 1,7); Return to "Ww.gxnu"
5. Math Function Library
To place the decimal part of a floating-point number unconditionally
Grammar
float ceil (float value)
eg
[Code]
Echo Ceil (9.99);//Return 10
echo ceil (9.12);//Return 10

Cos
Gets the cosine of a floating-point value
Grammar
float cos (float arg)
eg
Copy Code code as follows:

$numcos =cos (0.5);

Floor
To remove the decimal part of a floating-point number unconditionally
Grammar
Float floor (floor value)
eg
Copy Code code as follows:

echo Floor (9.12);//Return 9
Echo Floor (9.99);//Return 9

Rand
Produces a range of random values
Grammar
I
NT rand ([int min,in Max])
eg
Copy Code code as follows:

$num =rand (0,100);//Generate a random number between 1 and 100

Round
Rounding the decimal part of a floating-point number
Grammar
Float round (float value)
eg
Copy Code code as follows:

Float Round (9.99)//return 10
Float round (9.12)//Return 9

Sin
Gets the sine value of a floating-point value
Grammar
Float sin (float arg)
eg
Copy Code code as follows:

$numsin =sin (0.5);


6. Session function
Session_register
Describe the variables in one or more sessions
Grammar
BOOL Session_register (mixed name[,mixed ...])
eg
Copy Code code as follows:

$name = "Flag";
Session_register ("name");


Session_Start
Initializing session information
Grammar
BOOL Session (void)
eg
Copy Code code as follows:

Session_Start ();


7. Array function
Count
There are several array functions in the computed array
Grammar
int count (mixed var[,int mode])
eg
Copy Code code as follows:

Count ($array);

List
Assign an element value in an array to a variable
Grammar
void list (mixed varname,mixed ...)
eg
Copy Code code as follows:

$array =array (A,B,C);
List ($str 1, $str 2, $str 3) = $array//$str 1=a


Range
Creates an array in the specified range
Grammar
Array range (int low,int high[,int Step])
eg
Copy Code code as follows:

$array =array (0,9);

Shuffle
To reorder elements in an array in random order
Grammar
BOOL Shuffle (array array)
eg
Copy Code code as follows:

Shuffle ($array);


Author: bacteria-like body

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.