PHP study summary 2

Source: Internet
Author: User
PHP study summary 2 34. Array

(1) $ arr = array (); creates an empty array and assigns the created empty array to the variable $ arr.

(2) index array: the index array is an array of keys in the index group and integers. The order of keys starts from 0,

Initialization: $ friut = array ("Apple", "banana", "pineapple ");

(3) index array assignment

There are three ways to assign values to an index array:

First, assign values using the brackets following the name of the array variable. of course, the keys in the brackets in the index array must be integers. For example, $ arr [0] = 'apple ';



Type 2: Use array () to create an empty array and use the => symbol to separate keys and values. the left side indicates the key and the right side indicates the value. Of course, in the index array, the key must be an integer. For example, array ('0' => 'apple ');



Third: use array () to create an empty array and assign values directly to the array using single quotation marks or double quotation marks. the array will create an integer key starting from 0 by default. For example, array ('apple'); this array is equivalent to array ('0' => 'apple ');
(4) access the array content

$ Fruit0 = $ fruit ['0'];

If it is a variable, no quotation marks are required ..

Eg. $ fruit [$ index]

35.
Foreach cyclically accesses the values in the index array

$ Fruit = array ('apple', 'banana ', 'Pineapple ');
Foreach ($ fruit as $ k => $ v ){
Echo'
The value of '. $ k.' is '. $ v;
}


36.
Join array initialization
First: $ arr = array ("orange" => "orange ",

"Orange" => "orange"

);
Type 2: assign values using the brackets following the name of the array variable. of course, the keys in the brackets must be strings in the associated array. For example, $ arr ['apple'] = 'apple ';

You can use "" or ''in the copy process.

Access associated array content

$ Fruit0 = $ fruit ['bana'];


37. output of arrays and variables

Print_r ($ arr0)

38.
Custom functions

Function name (){
Echo 'Eric ';
}

Call method:

Name ()

Return value
Returns an array.

Function numbers ()
{
Return array (1, 2, 3 );
}

$ Arr = numbers (1, 2, 3 );

39.
Variable functions
That is, the function is called by the value of the variable. because the value of the variable is variable, you can call different functions by changing the value of a variable.


Function name (){
Echo 'Jobs ';
}
$ Func = 'name ';
$ Func (); // call a variable function

Use $ func to bind a function through the function name. the function name can be changed, so the corresponding call

$ Func () can also execute different functions.

40.
Determine whether a function exists

Function func (){
}
If (function_exists ('function ')){
Echo 'exists ';
}

Whether the class can be defined using class_exists.

Class MyClass {
}
// Check whether the class exists before use
If (class_exists ('myclass ')){
$ Myclass = new MyClass ();
}

Whether the file exists.

$ Filename = 'test.txt ';
If (! File_exists ($ filename )){
Echo $ filename. 'not exists .';
}

41. Class and object

Class Car {
$ Name = 'auto ';
Function getName (){
Return $ this-> name;
}}

If the var keyword is not added with protected or private, the default value is public.

// Instantiate a car object
$ Car = new Car ();
$ Car-> name = 'Audi A6 '; // you can specify the attribute value of an object.
Echo $ car-> getName (); // name of the output object in the method of calling the object

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.