开启PHP错误提示(建议初学者开启此功能)
<?
error_reporting(E_ALL);
ini_set("display_errors", TRUE);
?>
PHP 四种标记
1.
<?php ?>
2.
<script language = 'php'>
</script>
3.
<? ?>
4.
<% %>
第三第四种需要在php.ini 中开启
PHP注释
有三种
1.
//这里加注释
2.
#后面跟注释
3.
/* 和C一样这里是注释 */
八种原始数据类型
boolean (布尔型)
integer (整型)
float (浮点)
string (字符串)
object (对象)
array (数组)
resource (资源类)
NULL (空)
函数 gettype() 查看变量是什么类型
函数 is_type() 判断变量是不是type 类型
函数 settype() 转换类型
integer型可以用十进制,十六进制,八进制,二进制(好像是PHP 5.X版本后才开始支持的)
0123 //八进制
0x1A //十六进制
0b1010 //二进制
注意: 如果向八进制数传递了一个非法数字(即一个8以上的数),则后面的会被忽略
比如,02397 会被保存为 023
注意: 给定一个数超出了integer的范围,则将会被解释为float,也会返回成float
string的一些语法
在‘ ’ 中输入的字符串基本符合一般编程字符串要求,比如‘需要\' ,"需要\" ,\需要\\ ,但是!!\t,\r,\n 等转义字符在字符串中无法转义
“ ” 如果是用双引号括起来的,那么转义字符能完成转义
heredoc 结构 <<< 在该运算符之后提供一个标识符然后换行,最后用标识符结束
例如 :
<?php
$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;
/* More complex example, with variables. */
class foo
{
var $foo;
var $bar;
function __construct()
{
$this->foo = 'Foo';
$this->bar = array('Bar1', 'Bar2', 'Bar3');
}
}
$foo = new foo();
$name = 'MyName';
echo <<<EOT
My name is "$name". I am printing some $foo->foo.
Now, I am printing some {$foo->bar[1]}.
This should print a capital 'A': \x41
EOT;
?>
A nowdoc is identified with the same <<< sequence used for heredocs, but the identifier which follows is enclosed in single quotes, e.g. <<<'EOT'.
nowdoc 结构和heredocs 的用法很相似,都用<<< 开头,但是不一样的是接下来跟着的标识符需要用单引号把标识符括起来
比如
<<<'EOT'
注意:nowdoc 是PHP 5.3以后的特性
字符串有两种语法,一种叫简单语法,一种叫复杂语法,简单的语法规则是最常用和最方便的,它可以用最少的代码在一个 string 中嵌入一个变量,一个 array 的值,或一个 object 的属性。复杂规则语法的显著标记是用花括号包围的表达式。
简单语法:
当 PHP 解析器遇到一个美元符号($)时,它会和其它很多解析器一样,去组合尽量多的标识以形成一个合法的变量名。可以用花括号来明确变量名的界线。
比如:
<?php
$juice = "apple";
echo "He drank some $juice juice.".PHP_EOL;
// Invalid. "s" is a valid character for a variable name, but the variable is $juice.
echo "He drank some juice made of $juices.";
// Valid. Explicitly specify the end of the variable name by enclosing it in braces:
echo "He drank some juice made of ${juice}s.";
?>
复杂(花括号)语法:
复杂语法不是因为其语法复杂而得名,而是因为它可以使用复杂的表达式。
任何具有 string 表达的标量变量,数组单元或对象属性都可使用此语法。只需简单地像在 string 以外的地方那样写出表达式,然后用花括号 { 和 } 把它括起来即可。由于 { 无法被转义,只有 $ 紧挨着 { 时才会被识别。可以用 {\$ 来表达 {$。下面的示例可以更好的解释:
<?php
// Show all errors
error_reporting(E_ALL);
$great = 'fantastic';
// Won't work, outputs: This is { fantastic}
echo "This is { $great}";
// Works, outputs: This is fantastic
echo "This is {$great}";
// Works
echo "This square is {$square->width}00 centimeters broad.";
// Works, quoted keys only work using the curly brace syntax
echo "This works: {$arr['key']}";
// Works
echo "This works: {$arr[4][3]}";
// This is wrong for the same reason as $foo[bar] is wrong outside a string.
// In other words, it will still work, but only because PHP first looks for a
// constant named foo; an error of level E_NOTICE (undefined constant) will be
// thrown.
echo "This is wrong: {$arr[foo][3]}";
// Works. When using multi-dimensional arrays, always use braces around arrays
// when inside of strings
echo "This works: {$arr['foo'][3]}";
// Works.
echo "This works: " . $arr['foo'][3];
echo "This works too: {$obj->values[3]->name}";
echo "This is the value of the var named $name: {${$name}}";
echo "This is the value of the var named by the return value of getName(): {${getName()}}";
echo "This is the value of the var named by the return value of \$object->getName(): {${$object->getName()}}";
// Won't work, outputs: This is the return value of getName(): {getName()}
echo "This is the return value of getName(): {getName()}";
?>