Php code is beautiful

Source: Internet
Author: User
How to write php code is beautiful. this is an old problem. The appearance of classes in php5 makes php logic and writing a level. However, php code such as queries still shows a process-style "garbled" image. In particular, php does not require var variables, and anonymous functions are not perfect. In general, writing such code lacks aesthetics. If I write JavaScript code like this, :( & nbsp php code is beautiful
This is an old problem. The appearance of classes in php5 makes php logic and writing a level. However, php code such as queries still shows a process-style "garbled" image. In particular, php does not require var variables, and anonymous functions are not perfect. In general, writing such code lacks aesthetics.
If I write js like this:

( function(w) {
var a = 1,
    b= 2,
    fun = function() {
      return a + b;
   };

w.onload = function(){
  fun();
}
})(window);

In this way, variables and the executed code are separated easily. However, php is much 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 it down, the more you can't read it...
Read discuz! Write xplus, which uses a lot of functions and class packaging. However, some php code for judging and querying is still procedural. I really want to know how everyone writes php code at ordinary times ?! Let's talk about it, especially for coders who have written. net and jsp! Php writing
------ Solution --------------------
Three letters: OOP
Encapsulate CLASS, JAVA requires full OOP, so his code looks pretty

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.