Archives by Tag 'PHP'

转自PHP手册:比较substr, mb_substr和mb_strcut

By Symphony - Last updated: Monday, August 24, 2009

I found this function to be extremely useful. Here is a practical example, showing the difference between substr(), mb_substr() and mb_strcut(): <?php mb_internal_encoding(‘UTF-8′); $string = ‘cioèòà’; var_dump( substr($string, 0, 6), mb_substr($string, 0, 6), mb_strcut($string, 0, 6) ); ?> Output: string(6) “cioè?” string(9) “cioèòà” string(5) “cioè” Explanation: $string is long 9 bytes c – 1 byte [...]

我的第一个框架

By Symphony - Last updated: Friday, August 7, 2009

下载 虚拟主机配置 ServerName paydev.taomee.com ServerAdmin symphony@taomee.com DocumentRoot “/var/www/paydev” Options FollowSymLinks RewriteEngine On RewriteCond %{REQUEST_URI} !.*(\.css|\.js|\.html|\.zf|\.gif|\.pdf|\.rar|\.ppt|\.chm|\.png|\.jpg|\.jpeg)$ RewriteRule ^(.*)$ index.php [QSA,L] ErrorLog “/var/log/apache2/paydev_errors.log” CustomLog “/var/log/apache2/paydev_accesses.log” common

获取参数信息:func_num_args和func_get_args

By Symphony - Last updated: Thursday, August 6, 2009

Examples Example #1 func_get_args() example function foo() { $numargs = func_num_args(); echo “Number of arguments: $numargs\n”; if ($numargs >= 2) { echo “Second argument is: ” . func_get_arg(1) . “\n”; } $arg_list = func_get_args(); for ($i = 0; $i < $numargs; $i++) { echo “Argument $i is: ” . $arg_list[$i] . “\n”; } } foo(1, 2, 3); ?> The above example will output: Number of arguments: 3 [...]

PHP 获取当前类名、方法名

By Symphony - Last updated: Thursday, August 6, 2009

__CLASS__ 获取当前类名(在此之前我用get_class – -!) __FUNCTION__ 当前函数名(confirm) __METHOD__ 当前方法名 (bankcard::confirm) 非常好用!

PHP遍历对象

By Symphony - Last updated: Wednesday, August 5, 2009

自PHP 5 起,还可能遍历对象。 注: 当 foreach 开始执行时,数组内部的指针会自动指向第一个单元。这意味着不需要在 foreach 循环之前调用 reset()。 注: 除非数组是被引用,foreach 所操作的是指定数组的一个拷贝,而不是该数组本身。因此数组指针不会被 each() 结构改变,对返回的数组单元的修改也不会影响原数组。不过原数组的内部指针的确在处理数组的过程中向前移动了。假定 foreach 循环运行到结束,原数组的内部指针将指向数组的结尾。

session_start的问题

By Symphony - Last updated: Thursday, July 30, 2009

之前做的表单ajax功能今天突然无法运转。 查到最后发现ajax请求的时候没有自动打开session了,才发现是session autostart被关掉了。在session请求前加session_start();就好了。 另外使用cakephp的童鞋把session.level改为low也可以解决这个问题。 这个bug我花了四个小时才分析出来的。web做到最后真是细微之处见真功。

PHP Interview questions from YAHOO

By Symphony - Last updated: Friday, June 19, 2009

1. Which of the following will not add john to the users array? B A. $users[] = ‘john’; B. array_add($users,’john’); C. array_push($users,‘john’); D. $users ||= ‘john’; 2. What’s the difference between sort(), assort() and ksort? Under what circumstances would you use each of these? sort() asort() ksort() 排序对象 元素的值 元素的值 元素键值 排序方式 字母顺序 字母顺序 字母顺序 [...]

Zabbix的一个bug

By Symphony - Last updated: Tuesday, June 9, 2009

最近公司的项目要分解zabbix。分析它的代码的时候发现其数据库类有一个bug:使用mysql_pconnect建立一个数据库连接,却用mysql_close去关闭!这样做是关不掉的!因为mysql_pconnect建立的是一个持久化的连接,使用完毕会被放回连接池的,即使脚本执行完毕,这个连接也不会关闭。 一下是php.net对mysql_close的解释: mysql_close() closes the non-persistent connection to the MySQL server that’s associated with the specified link identifier. If link_identifier isn’t specified, the last opened link is used. 一下是有问题的代码片段: $mysql_server = $DB['SERVER'].( !empty($DB['PORT']) ? ‘:’.$DB['PORT'] : ”); if (!$DB['DB']= mysql_pconnect($mysql_server,$DB['USER'],$DB['PASSWORD'])){ $error = ‘Error connecting to database ['.mysql_error().']‘; $result = false; } else{ if (!mysql_select_db($DB['DATABASE'])){ $error [...]