php变量相关
时间:2006-07-17 来源:cnscn2008
.boolean
值为true, false
When coverting to boolean, the following values are considered FALSE:
the boolean FALSE itself
integer 0 (zero)
the float 0.0 (zero)
the empty string, and the string "0"
an array with zeor elements
an object with zero member variables
the special type NULL (including unset variables)
SimpleXML objects created from empty tags
Every other values is considered TRUE (including any resource)
Warning:
-1 is considered TRUE, like any other non-zero (whether negative or positive ) number
var_dump((bool) ""); //false
var_dump((bool) 1); //true
var_dump((bool) -2); //true
var_dump((bool) "foo"); //true
var_dump((bool) 2.3e3); //true
var_dump((bool) array(12)); //true
var_dump((bool) array()); //false
var_dump((bool) "false"); //true
.integer
An integer is a number of the set Z = {..., -2, -1, 0, 1, 2,...}
Integers can be specified in decimal(10-based), hexadecimal(16-based) or octal(8-based) notation, optionally preceded by a sign (- or +)
$a = 1234; //decimal number
$a = -1234; //a negative number
$a = 0123; //octal number (equivalent to 83 decimal)
$a = 0x1A; //hexadecimal number (equivalent to 26 decmial)
Integer overflow
if you specify a number beyond the bounds of the integer type, it will be interpreted as a float instead.
$large_number = 2147483648;
var_dump($large_number); //output : float(2147483648);
.float
$a = 1.23;
$a = 1.2e3;
$a = 7E-10;
.string
A string is series of characters.
Note: It is no problem for a string to very large, There is no practical bound to the size of strings imposed by PHP, so there is no reason at all to worry about long strings
Heredoc
$str = width}
sdsdddddddd
__HTML__;
$str = width}
sdsdddddddd
__EOF__;
$sql = select a, b, c from table where a='123'__SQL__;
.array
array_key_exists($var, $base);
.object
.resource
$result = mysql_connect(...);
.NULL
unset();
is_null();
empty();
isset();
.mixed
.number
.callback
$foo = 25;
$bar = &$foo;
返回变量的拷贝,尽管函数以引用方式定义
function &GetLogin()
{
return $_SESSION['login'];
}
返回变量的拷贝,尽管函数以引用方式定义
function &GetLogin()
{
$var = &$_SESSION['login'];
return $var;
}
function GetLogin(&$var, $scope=false)
{
$var = &$_SESSION['login'];
return $var;
}
.预定义变量
$GLOBALS
$_SERVER
$_GET
$_POST
$_COOKIE
$_FILES
$_ENV
$_REQUEST
$_SESSION
if(!isset($_SERVER)){ $_GET = &$HTTP_GET_VARS; $_POST = &$HTTP_POST_VARS; $_ENV = &$HTTP_ENV_VARS; $_COOKIE = &$HTTP_COOKIE_VARS; $_REQUEST= array_merge($_GET, $_POST, $_COOKIE);}
.全局变量
$a=5;
$b=6;
function sum()
{
global $a, $b;
$b+=$a;
}
function sum()
{
$GLOBALS['b'] = $GLOBALS['a] + $GLOBALS['b'];
}
___LINE___
___FILE___
___FUNCTION___
___CLASS___
___METHOD___
gettype($x) is_int($x)is_string($x)is_array($x)is_object($x)is_number($x)string($x)settype($x, int)
isset($x)
preg_match('/str/', $x);
if(strpos($needle, $haystack) == false) { }
if(array_search($needle, $haystack) ==false ) {...}
if(array_push($needle, $haystack) == false) {...}
array_sum($arr);
ord();
chr();
pow($x,$y)
error_reporting(E_ALL);
define("NAME", "cnscn");
str_replace('"', '\"', $str);
class Test{
const ONE = 1;
public $one = 1;
public function two()
{
}
}
if(preg_match("/^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2, 6}$/i", $mail)){ ... }
if(class_exists($class_name)) {...}
相关阅读 更多 +