Summary of PHP2014.5.14:

Source: Internet
Author: User
Tags explode

1: Generate random numbers

<?php

Session_Start ();

Generate 4 Random numbers

$arr = Array (

"A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M",

"N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",

"0", "1", "2", "3", "4", "5", "6", "7", "8", "9");

$rand = "";

for ($i =0; $i <4; $i + +) {

$rand. = $arr [Rand (0,count ($arr)-1)]; Generate random numbers

}

$_session[' check_pic '] = $rand;

Generate picture :imagecreatetruecolor(Create a picture with an extra color)

$img =imagecreatetruecolor (100,30);

Production Color :

(1): When the first call to generate a color method, is to generate a background color, the default is black

If you want to customize the color, you need to use the Imagefill function

$BG =imagecolorallocate($img, 189,196,220);

Imagefill ($img, 0,0, $BG);

(2): The second call to this method, you can generate the image above the text or other styles of color

$te =imagecolorallocate ($img, Rand (0,255), Rand (0,255), Rand (0,255));

$te 2=imagecolorallocate ($img, Rand (0,255), Rand (0,255), Rand (0,255));

$te 3=imagecolorallocate ($img, Rand (0,255), Rand (0,255), Rand (0,255));

generate text above the picture :(the first value is the picture, the second value is the size of the font, the third value is the horizontal axis, the 41st value is the ordinate, the 51st value is the generated word, and the 61st value is the color)

imagestring($img, Rand (5,10), Rand (3,70), Rand (3,15), $rand, $te);

$posX =rand (6,45);

for ($i =0; $i <4; $i + +) {

$posY =rand (2,15);

SUBSTR: Returns a substring of a string

Imagestring ($img, rand (2,5), $posX, $posY, substr ($rand, $i, 1), $te);

$posX +=rand (8,25);

}

create an interference line, just draw a line

$posLineX 1=rand (6,30);

$posLineX 2=rand (30,60);

Draw 4 Lines

for ($i =0; $i <5; $i + +) {

$posLineY 1=rand (2,10);

$posLineY 2=rand (11,28);

Generate interference lines (the first value is the picture, the second value is the beginning of the horizontal axis, the third value is the starting coordinate, the fourth value is the end of the horizontal axis, the fifth value is the ending ordinate, and the 61st value is the color) imageline( $img, $posLineX 1, $posLineY 1, $posLineX 2, $posLineY 2, $te 2);

$posLineX 1+=rand (30,60);

$posLineX 2 +=rand (61,98);

}

Build Point

for ($i =0; $i <50; $i + +) {

Generate points (the first value is a picture, the second value is random at the horizontal axis of the build point, the third value is random at the ordinate generation point, the 41st value is the color)

Imagesetpixel     ($img, Rand (2,100), Rand (0,50), $te 3); }

To take PHP as a picture output, you must give the file a header declaration

Header ("Content-type:image/jpeg");

Final image generation

Imagejpeg ($IMG);

?>

2: Define constants (two types)
(1):define (): wherever you define it
(2):const (): can only be defined at the front

3: Want to use global variables in the function must be declared by global
Example: $conn = ""; Global variables
function Get_conn () {
Global $conn;
$conn = mysql_connect (host,user,pwd) or Die (Mysql_error ());
mysql_select_db ("BBS", $conn);
mysql_query ("Set names ' UTF8 '");
}

4: Call function (two methods):
(1):require_once("function.php");
(2):include("mysql.php");

5: Not only one parameter of a function can have multiple

  6:explode (): Divide the string into the array;

implode (): Splitting the array into strings   

Example: $arr = Array (1,2,3,4,5);

$str = "1982-4-2";

Test1 ($str, $arr);

function test1 ($str 1= "", $str 2=array ()) {

Splits an array into a string implode

The next method means: $str 2 should be a number of groups

The array is then separated by commas to form a new string

$s 1 = implode(",", $str 2);

echo $s 1;

Splitting a string into an array explode

This function means: first $str1 is a string, which is assembled according to a specification

This specification is a style that must conform to the first parameter above

$s 2 = explode("-", $str 1);

Print_r ($s 2);

}

7: Parameter default value for function

Example: Test2 ("test");

Default value of the function

function test2 ($db = "BBS") {

$conn = mysql_connect (host,user,pwd) or Die (Mysql_error ());

mysql_select_db ($db, $conn);

mysql_query ("Set names ' UTF8 '");

}

8:vsprintf (): Format a string with an array that you want to use
Example: $arr =[1,2,3];
Echo vsprintf ("%d-%d-%02d", $arr);

9: Call your own time format method
Example: function FormatDateTime ($date) {
$arr = Explode ("-", $date);
$str = vsprintf ("%04d-%02d-%02d", $arr);
return $str;
}
$time = FormatDateTime ("99-8-8");
Echo $time;

Ten: Func_num_args (): Get the number passed over

Func_get_args (): Gets all incoming arguments, returns an array

Example://Get function all parameters

TEST4 ("SELECT * from Users where username=?", "Pangpang");

Get all parameters passed in function test4 () {

Gets the number of arguments passed over

$num = Func_num_args ();

Echo $num;

Gets all the arguments passed in, returning an array

$arr = Func_get_args ();

Var_dump ($arr);

}

One:str_replace: Replace

Example: function mysql_ping () {

Gets an array of all arguments passed in

$arr = Func_get_args ();

Get the first parameter, in our section, the first argument is actually the SQL statement

$sql = $arr [0];

The incoming SQL statement, in fact, begins with the location of the variable that is used instead

You need to convert the variable to a symbol that can replace the '%s ' of the formatted string.

$sql = str_replace("?", "'%s '", $sql);

Array_shift, is the first element of the array is moved out. Returns the value of the move out, and then the rest of the array

$values = Array_shift ($arr);

$sql = vsprintf($sql, $arr);

Echo $sql;

}

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.