What are the tricks of PHP?

Source: Internet
Author: User
Php Chinese network (www.php.cn) provides the most comprehensive basic tutorial on programming technology, introducing HTML, CSS, Javascript, Python, Java, Ruby, C, PHP, basic knowledge of MySQL and other programming languages. At the same time, this site also provides a large number of online instances, through which you can better learn programming... Reply: Since PHP was playing with closures, it was hard for everyone to jump to the fifth floor.


1. basics: Define a function in one sentence and call it immediately
This is often done in JavaScript because variable pollution is intolerable. But how can this function be implemented in PHP? One feasible method is:

call_user_func(function(){        echo "hello,world";});
@ Chen has another solution.


  pro()); //Error: Call to protected method A::pro()     var_dump($this->pub());};$ext = $ext->bindTo($a);var_dump($ext());


  


  scalar}. 
"; }; $obj->scalar = "php bbb"; //$obj = (object) "php bbb"; callback($callback);
The C language tends to be an operator -->, and this PHP also does.

Add one: you can directly enter the URL in the PHP source code without comments (Black Magic ):


  
What do you mean when it comes to odd tricks? you can play games, but never use them in real projects. let's make two contributions:
Exchange two values:
List ($ B, $ a) = array ($ a, $ B );
$ A = array ($ B, $ B = $ a) [0]; of course, it is a magic BOM header. I don't know how many PHP beginners can't sleep well in the Quora list.
What are some cool PHP tricks? Convert XML strings to Arrays

$xmlObj = simplexml_load_string($xmlStr);$xmlArr = json_decode(json_encode($xmlObj) , true);
1. stdClass ();

$obj = new stdClass;$obj->a = 1;$obj->b = 2;

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.