PHP Grammar Quick Learning Notes

Source: Internet
Author: User

1.gettype () The type used to get the variable

2.var_dump () To view the type and value of an expression

3. Define a string that is enclosed in single quotes, and double quotes will parse the character inside the string.

The 4.HERODOC structure is <<<foobra XXXX FOOBAR, which is understood as a structure string.

5. Use the string to connect.

6. Key = = value pair.

The 7.unset () function allows you to delete a key in an array.

8. The way the array is declared $arr =array ("foo" = "Bar", 12=>true);

9. Presentation of the collection

<? PHP // Collection Display $colors=  array(' Red ', ' green ', ' been ', ' yellow '); foreach ($colors as$color) {    echo "color has $color";}? >

10. Initialization of objects and invocation of methods

<? PHP // initialization of the object class foo{    publicfunction  do_foo () {         echo "method executed";    }} $bar=new  foo; $bar-Do_foo ();? >

11. Callback Method

<?PHP//callback functionfunctionFirst () {Echo"First time display";}classmyclass{Static functionMyfirst () {Echo"Static method Display"; }}Call_user_func(' First ');//Call_user_func (Array (' first ', ' Myfirst '));$obj=NewMyclass ();Call_user_func(Array($obj, ' Myfirst '));?>

12.php variables are case-sensitive

No add global will get an error, and the real result is 2.

<? PHP // scope of use of variables $a=1; $b=2; function sum () {    $b=$a+$b;} Sum (); Echo $b ;? >

Added global, showing the result is 3.

<? PHP // scope of use of variables $a=1; $b=2; function sum () {    global$a,$b;     $b=$a+$b;} Sum (); Echo $b  ?>

13. Static variables

<? PHP // Static Variables function Test () {    $a=0;     Echo $a ;     $a+ +;} Test (); Test (); Test ();? >

The output is 000.

<? PHP // Static Variables function Test () {    static$a=0;     Echo $a ;     $a+ +;} Test (); Test (); Test ();? >

The display output is 012

13. Functions that can determine the type of a variable

GetType (), Is_array (), Is_float (), Is_int (), Is_object () and is_string ();

14. Setting Up Cookies

Setcookie (); and $_cookie, $http _cookie_cars and $_request.

15. Defining Constants (define and const)

Define ("foo", "something"), or const foo= "something"

echo Foo

16. Magic Constants

Magic constants are case insensitive

Echo __line__;//Current line numberEcho  __file__;//the full path of the current fileEcho__dir__;//the directory where the files are locatedEcho __function__;//Function NameEcho __class__;//the name of the classEcho __method__;//method Name of the classEcho__namespace__;//the name of the current namespace

17. Expressions

$a= 5;

Represents assigning a value of 5 to a variable $ A

18. Operators

Error operator: @. When placed in front of a PHP expression, any error messages that may be generated by the expression are ignored.

Execute operator: PHP supports an execution operator: an inverse quotation mark ('). Note that this is not a single quote! PHP will attempt to execute the contents of the anti-quote as a shell command and return its output information (for example, it can be assigned to a variable instead of simply discarding it to standard output). The effect of using the inverse quote operator "'" is the same as the function shell_exec (). <?php

$output = `ls -al`;
echo "<pre>$output</pre>";
?>

Array operators: + union = = equal, = = = Congruent,! = Unequal,! = = Not congruent

19. Control Structure

Continue end loop after statement execution

20.php function

An anonymous function, also known as a closure function, temporarily creates a function that has no name and is often used as a parameter to a callback function.

<? PHP // anonymous function variable assignment $greet=function($name) {    printf("Hello%s",$name) ;}; $greet (' World '); $greet (' PHP ');? >

Note is%s.

21st. Classes and objects

In PHP:: is a reference method for static and static properties in a class.

You can use the value of the test:: $test property Without instantiating the object directly

The extends keyword inherits the methods and members of another class, and can only inherit multiple classes without extending a single inheritance

Inherited methods and members can be overwritten by a re-declaration with the same name, unless the parent class defines the method with the final keyword. The overridden method or member can be accessed through the parent::.

22. Loading objects automatically

For example, the MYCLASS1 and Myclass2 classes need to be loaded from myclass1.php and myclass2.php.

<? PHP // anonymous function variable assignment function __autoload ($class _name) {    require_once$class _name. php ';} $obj=new  Myclass1 (); $obj=new  Myclass2 ();? >

23. Constructors and destructors

<?PHP//constructor Functionclassbaseclass{function__construct () {Print"Constructor for parent class"; }}classSubclassextendsbaseclass{function__construct () {Parent::__construct (); Print"Constructors for subclasses"; }}$obj=NewBaseClass ();$obj=Newsubclass ();?>

The result of the output is "constructor of the constructor subclass of the parent class's constructor";

To execute a destructor for a parent class, you must explicitly call parent::__destruct ()in the destructor body of the subclass.

 <? php  class   Mydestructableclass {  __construct () { print  "in constructor" ;   $this ->name = "Mydestructableclass"    ;  function   __destruct () { print  "destroying".  $this ->name.   "; }}   $obj  = new   Mydestructableclass ();   $obj ->__destruct (); ? 

No big difference.

24.php of Access control

Access control for properties and methods: public, private,protected.

The public class can be accessed anywhere, private is the class access, and the protected class can only be accessed by the class in which it resides.

Inheritance of Class 25

Extends

26.static keywords

To facilitate access to the members or methods of a class without instantiating the object.

<? PHP class Foo {    publicstaticfunction  astaticmethod () {        echo " Hello ";    }} Foo::astaticmethod (); $classname = ' Foo '; $classname // As of PHP 5.3.0

The output is Hellohello

PHP Grammar Quick Learning Notes

Related Article

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.