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