首页 / PHP / PHP基础知识(二)
PHP基础知识(二)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了PHP基础知识(二),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4210字,纯文字阅读大概需要7分钟。
内容图文
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.
<?php namespace 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 引入命名空间概念前一样。在名称前加上前缀 \ 表示该名称是全局空间中的名称,即使该名称位于其它的命名空间中时也是如此。
<?php namespace A\B\C; /* 这个函数是 A\B\C\fopen */ function fopen() { /* ... */ $f = \fopen(...); // 调用全局的fopen函数 return $f; } ?>
Strings
Concatenation
- 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
<?php echo ‘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
<?php echo ‘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 parsed vs. $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 error vs. $a = 5; return ($a == 5) ? ‘yay‘ : ‘nope‘; // this example will return ‘yay‘
Variable declarations
<?php $about = ‘A very long string of text‘; // uses 2MB memory echo $about; vs. echo ‘A very long string of text‘; // uses 1MB memory
原文:http://www.cnblogs.com/perseverancevictory/p/4246233.html
内容总结
以上是互联网集市为您收集整理的PHP基础知识(二)全部内容,希望文章能够帮你解决PHP基础知识(二)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。