29 January 2016

相等

$num = 0;
var_dump($num == '');           // true
var_dump($num == null);         // true

var_dump($num === '');          // false
var_dump($num === null);        // false

var_dump('' == null);           // true

var_dump(empty($num));          // true

字符串的坑

$count = (int) '3';
$str = 'test'.$count + 1;   <== xxx + 1 不会先被执行

正确

$count = ((int) '3') + 1;
$str = 'test'.$count;

表单输入

表单接受数组信息

<form action="file-upload.php" method="post" enctype="multipart/form-data">
  <input name="file[]" type="file" />
  <input name="file[]" type="file" />
  <input type="submit" value="Send files" />
</form>

<form action="file-upload.php" method="post" enctype="multipart/form-data">
  <input name="file[]" type="file" multiple />
  <input type="submit" value="Send files" />
</form>

<form action="process.php" method="post">
  <input type="checkbox" name="hobby[]" value="1">
  <input type="checkbox" name="hobby[]" value="2">
  <input type="checkbox" name="hobby[]" value="3">
</form>

parse_url

$url = 'http://www.baidu.com:80/index.php';
$arr = parse_url($url);

// Array (
//     [scheme] => http
//     [host] => www.baidu.com
//     [port] => 80
//     [path] => /index.php
// )


$url =  'http://username:password@hostname/path/some/where?arg=value#anchor' ;
$res =  parse_url ( $url );

// Array (
//   [scheme] => http
//   [host] => hostname
//   [user] => username
//   [pass] => password
//   [path] => /path/some/where
//   [query] => arg=value
//   [fragment] => anchor
// )

echo

$foo = array('bar' => 'baz');
echo "Hello {$foo['bar']}!";     // Hello baz!
echo "Hello $foo['bar']!";       // 出错
$s = '&lt;body text="body">';
echo $s;                         // <body text="body">

char

ord('a')
chr(97)

换行

$a = '\n';           <== 不能正常换行
$b = "\n";           <== 正常换行

不一样的控制语句

<?php if (condition): ?>

html code to run if condition is true

<?php else: ?>

html code to run if condition is false

<?php endif ?>
<?php $i = 0; while ($i < 3): ?>
  html code here </br>
<?php $i++; endwhile ?>

<?php for ($i = 0; $i < 3; $i++): ?>
  html code here </br>
<?php endfor ?>

include/require

test.php

echo __FILE__;

index.php

require 'test.php';             // test.php

打开 index.php,显示的内容是 test.php

random

rand() 和 mt_rand()  都是闭区间

计算一个随机浮点数

return $min + mt_rand() / mt_getrandmax() * ($max - $min);

强制转化成数字

$val = 'a12';                   // 0
$val = '1a2';                   // 1
$val = '12a';                   // 12
$val = intval($val);

$val = floatval($val);

判断

is_bool     (...)
is_numeric  (...)    <== is_float + is_int + 字符串数字
is_string   (...)
is_array    (...)
is_callable (...)
is_float    (...)
is_int      (...)
is_null     (...)    <== isset() 的反义
is_object   (...)

isset       (...)
empty       (...)    <== "", 0, "0", null, false, array()