$foo = "0"; // $foo is a string (ASCII 48) $foo++; // $foo is the string "1" (ASCII 49) $foo += 1; // $foo is now an integer (2) $foo = $foo + 1.3; // $foo is now a double (3.3) $foo = 5 + "10 Little Piggies"; // $foo is a double (15) $foo = 5 + "10 Small Pigs"; // $foo is an integer (15)
要改变变量的类型,也可用settype() 函数。
1、强制类型转换
PHP中的强制类型转换和C中一样: 在需要进行类型强制转换的变量前的括号中写出想要的类型名。
$foo = 10; // $foo is an integer $bar = (double) $foo; // $bar is a double 允许的强制转换是: (int), (integer) - cast to integer (real), (double), (float) - cast to double (string) - cast to string (array) - cast to array (object) - cast to object
$foo = 1 + "10.5"; // $foo is a double (11.5) $foo = 1 + "-1.3e3"; // $foo is a double (-1299) $foo = 1 + "bob-1.3e3"; // $foo is a double (1) $foo = 1 + "bob3"; // $foo is an integer (1) $foo = 1 + "10 Small Pigs"; // $foo is an integer (11) $foo = 1 + "10 Little Piggies"; // $foo is a double (11); the string contains 'e'
通常,你希望根据条件执行多于一条语句。当然,不需要给每条语句都加上 IF 判断。取而代之,可以把多条语句组成一个语句组。 If语句可以嵌套于其他 IF语句中,使你能够灵活地有条件的执行程序的各个部分。
2、 ELSE语句
通常你希望满足特定条件时执行一条语句,不满足条件是执行另一条语句。ELSE就是用来做这个的。ELSE 扩展IF语句,在IF语句表达式为FALSE时执行另一条语句。例如, 下面程序执行如果 $a 大于 $b则显示 'a is bigger than b',否则显示 'a is NOT bigger than b':
if ($a>$b) { print "a is bigger than b"; } else { print "a is NOT bigger than b"; }
3、 ELSEIF语句
ELSEIF,就象名字所示,是IF和ELSE的组合,类似于 ELSE,它扩展 IF 语句在IF表达式为 FALSE时执行其他的语句。但与ELSE不同,它只在ELSEIF表达式也为TRUE时执行其他语句。
function foo( &$bar ) { $bar .= ' and something extra.'; } $str = 'This is a string, '; foo( $str ); echo $str; // outputs 'This is a string, and something extra.'
function foo( $bar ) { $bar .= ' and something extra.'; } $str = 'This is a string, '; foo( $str ); echo $str; // outputs 'This is a string, ' foo( &$str ); echo $str; // outputs 'This is a string, and something extra.'
4、 默认值
函数可以定义 C++ 风格的默认值,如下:
function makecoffee( $type = "cappucino" ) { echo "Making a cup of $type.\n"; } echo makecoffee(); echo makecoffee( "espresso" );
上边这段代码的输出是:
Making a cup of cappucino. Making a cup of espresso. 注意,当使用默认参数时,所有有默认值的参数应在无默认值的参数的后边定义;否则,将不会按所想的那样工作。
5、CLASS(类)
类是一系列变量和函数的集合。类用以下语法定义:
class Cart { var $items; // Items in our shopping cart // Add $num articles of $artnr to the cart function add_item($artnr, $num) { $this->items[$artnr] += $num; } // Take $num articles of $artnr out of the cart function remove_item($artnr, $num) { if ($this->items[$artnr] > $num) { $this->items[$artnr] -= $num; return true; } else { return false; } } } ?>
$ncart = new Named_Cart; // Create a named cart $ncart->set_owner("kris"); // Name that cart print $ncart->owner; // print the cart owners name $ncart->add_item("10", 1); // (inherited functionality from cart)
class Constructor_Cart { function Constructor_Cart($item = "10", $num = 1) { $this->add_item($item, $num); } } // Shop the same old boring stuff. $default_cart = new Constructor_Cart; // Shop for real... $different_cart = new Constructor_Cart("20", 17);
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.