How to write PHP code beautifully

Source: Internet
Author: User
How to write PHP code pretty
It's an old question. The advent of classes in php5 the logic and writing of PHP into one level. But for PHP code such as queries, there is a process-style "stew" image. In particular, PHP now does not require a var variable, anonymous function is not perfect. In general, writing this kind of code is still a lack of aesthetics.
If I write JS it should be like this:

(function (W) {
var a = 1,
B= 2,
Fun = function () {
return a + B;
};

W.onload = function () {
Fun ();
}
}) (window);

It is easy to distinguish between such variables and the execution code. But if it was PHP it would be a lot more messy:

$a = 1;
$b = 2;
function Fun () {
Global $a;
Global $b;
return $a + $b;
}
foreach ($_get as $key = = $val) {
$ $key = UrlDecode ($val);
}
if ((int) $getVal > 1) {
...
} else {
...
}
$val = null;
$arr = Array ();
foreach ($getArr as $val) {
if (strlen ($val) > 0) {
$arr [] = $val;
}
}
echo Fun (), implode ('-', $arr);
Exit

The more you write down, the more you can't read ...
Read the Xplus written by discuz!, it used a lot of functions and class packaging. But some of the judgment and query PHP code is still procedural. I really want to know how people usually write PHP code?! Especially if you've ever written. NET and JSP code farming say it! PHP Writing


------Solution--------------------
Three letters: OOP
Package Class,java requires full oop so his code looks more beautiful
  • 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.