PHP基礎知識(二)

來源:互聯網
上載者:User

標籤:

Global namespace  //看不懂看下面的中文 中英結合看看

When using namespaces, you may find that internal functions(內部(內建)函數) are hidden by functions you wrote. To fix this, refer to the global function by using a backslash before the function name.

<?phpnamespace phptherightway;function fopen(){    $file = \fopen();    // Our function name is the same as an internal function.                         // Execute the function from the global space by adding ‘\‘.}function array(){    $iterator = new \ArrayIterator();    // ArrayIterator is an internal class. Using its name without a backslash                                         // will attempt to resolve it within your namespace.}

global space (全域空間):

如果沒有定義任何命名空間,所有的類與函數的定義都是在全域空間,與 PHP 引入命名空間概念前一樣。在名稱前加上首碼 \ 表示該名稱是全域空間中的名稱,即使該名稱位於其它的命名空間中時也是如此。

<?phpnamespace A\B\C;/* 這個函數是 A\B\C\fopen */function fopen() {      /* ... */     $f = \fopen(...); // 調用全域的fopen函數     return $f;} ?>

 

StringsConcatenation
  • If your line extends beyond the recommended line length (120 characters), consider concatenating your line
  • For readability it’s best to use concatenation operators over concatenating assignment operators
  • While within the original scope of the variable, indent when concatenation uses a new line
<?php$a  = ‘Multi-line example‘;    // concatenating assignment operator (.=)$a .= "\n";$a .= ‘of what not to do‘;vs.$a = ‘Multi-line example‘      // concatenation operator (.)    . "\n"                     // indenting new lines    . ‘of what to do‘;

 

String types

String types are a constant feature within the PHP community, but hopefully this section will explain the differences between the string types and their benefits/uses.

Single quotes

Single quotes are the simplest way to define a string and are often the quickest. Their speed stems from PHP not parsing the string (doesn’t parse for variables). They’re best suited for:

  • Strings that do not need to be parsed
  • Writing of a variable into plain text
<?phpecho ‘This is my string, look at how pretty it is.‘;    // no need to parse a simple string/** * Output: * * This is my string, look at how pretty it is. */

  

Double quotes

Double quotes are the Swiss army knife of strings, but are slower due to the string being parsed. They’re best suited for:

  • Escaped strings
  • Strings with multiple variables and plain text
  • Condensing multi-line concatenation, and improving readability
<?phpecho ‘phptherightway is ‘ . $adjective . ‘.‘     // a single quotes example that uses multiple concatenating for    . "\n"                                       // variables and escaped string    . ‘I love learning‘ . $code . ‘!‘;vs.echo "phptherightway is $adjective.\n I love learning $code!"  // Instead of multiple concatenating, double quotes                                                               // enables us to use a parsable string

  While using double quotes that contain variables, it’s often the case that the variable will be touching another character. This will result in PHP not parsing the variable due to the variable being camouflaged. To fix this problem, wrap the variable within a pair of curly brackets.

<?php$juice = ‘plum‘;echo "I drank some juice made of $juices";    // $juice cannot be parsedvs.$juice = ‘plum‘;echo "I drank some juice made of {$juice}s";    // $juice will be parsed/** * Complex variables will also be parsed within curly brackets */$juice = array(‘apple‘, ‘orange‘, ‘plum‘);echo "I drank some juice made of {$juice[1]}s";   // $juice[1] will be parsed

  

Nowdoc syntax  Heredoc syntax

見PHP手冊。

 

Ternary operators(三元運算操作符)

Ternary operators are a great way to condense code, but are often used in excess. While ternary operators can be stacked/nested, it is advised to use one per line for readability.

<?php$a = 5;echo ($a == 5) ? ‘yay‘ : ‘nay‘;vs.// nested ternary$b = 10;echo ($a) ? ($a == 5) ? ‘yay‘ : ‘nay‘ : ($b == 10) ? ‘excessive‘ : ‘:(‘;    // excess nesting, sacrificing readability

  

To ‘return’ a value with ternary operators use the correct syntax.

<?php$a = 5;echo ($a == 5) ? return true : return false;    // this example will output an errorvs.$a = 5;return ($a == 5) ? ‘yay‘ : ‘nope‘;    // this example will return ‘yay‘

  

Variable declarations
<?php$about = ‘A very long string of text‘;    // uses 2MB memoryecho $about;vs.echo ‘A very long string of text‘;        // uses 1MB memory

  

 

 

 

PHP基礎知識(二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.