Submitted by gouki on 2008, March 22, 10:25 PM
在WEB应用中,可能会经常使用一些JS框架,或者使用一些已经存在的变量,如果我全程参与开发,那么自然不存在需要判断变量是否存在的问题,但如果多人开发,或者是写一框架,最后由别人运行,那么势必需要在运行前判断某些我需要用到的变量或者对象啥的是否存在。
最后我的判断方法是采用了Javascript的标准判断,if(typeof(Test) == 'undefined')可以得知是否存在这个变量,当然直接用if(typeof(Test) == 'object')就可以判断是不是对象了。
看了流年写的Base.js才发现,原来也可以这样
function is_object(obj) {
return (obj.constructor.toString().indexOf("Object")!= -1);
}
毕竟所有的东西,在JS里几乎都可以算是对象,所以这样也能够来进行判断,学习一下……
不过,相对而言,应该是 typeof 更快捷吧
Javascript | 评论:2
| 阅读:35611
Submitted by gouki on 2008, March 21, 9:33 PM
任何一门语言,最重要的往往都是由:基本语法、类型、各种变量常量、表达式、运算符、控制结构、函数(对象、异常处理)等组成。在学习PHP的过程中,我也将逐步为以上的内容的学习作一概要,当然,首推还是手册,基本以介绍手册为主。
» 阅读全文
PHP | 评论:0
| 阅读:22628
Submitted by gouki on 2008, March 20, 8:21 PM
在网上看到这个函数的介绍。虽然这个函数比较简单,且不支持正则,但……这个函数还是相对用的比较多的,自己又比较懒,看到这篇文章,于是就转载一下。
str_replace用的最多的地方恐怕应该是str_replace(array("\r","\n","\r\n"),"<br />" , $content);了,适合添加文章或者其他的时候用,当然这个时候nl2br函数也有用。
原文地址:http://www.phpweblog.net/yemoo/archive/2008/03/15/2971.html
原文作者:Yemoo
» 阅读全文
Tags: 函数
PHP | 评论:0
| 阅读:22475
Submitted by gouki on 2008, March 19, 9:41 AM
eval是什么,相信很多都会知道。但真实有多少人使用它呢?恐怕在实际应用中,使用的也比较少吧。(详细请看全文)
Evaluates the string given in code_str as PHP code. Among other things, this can be useful for storing code in a database text field for later execution.
There are some factors to keep in mind when using eval(). Remember that the string passed must be valid PHP code, including things like terminating statements with a semicolon so the parser doesn't die on the line after the eval(), and properly escaping things in code_str. To mix HTML output and PHP code you can use a closing PHP tag to leave PHP mode.
Also remember that variables given values under eval() will retain these values in the main script afterwards.
» 阅读全文
Tags: php
PHP | 评论:4
| 阅读:42787
Submitted by gouki on 2008, March 18, 11:22 PM
在PHP里面,要想统计一个action或者一个函数或者某个过程的执行所消耗的时间往往都只有一个办法:在运算前记录下时间戳,在运算后记录下时间戳,然后相减,就能得到一个相对比较实际的时间。
基本代码如下(从phpmyadmin里复制而来,懒得打了。这段代码其实也就是phpMyadmin里SQL的执行时间的计算):
PHP代码
- <?php
-
-
- $querytime_before = array_sum(explode(' ', microtime()));
-
- $result = @PMA_DBI_try_query($full_sql_query, null, PMA_DBI_QUERY_STORE);
-
- $querytime_after = array_sum(explode(' ', microtime()));
-
- $GLOBALS['querytime'] = $querytime_after - $querytime_before;
-
- ?>
其中 array_sum(explode(' ', microtime())); 是PHP4时代的写法,到PHP5之后,microtime函数多了一个bool值的参数,加上这个参数后可以直接得到 array_sum(explode(' ', microtime())); 相等的值,即:microtime(true);
本文并无技术含量,纯粹用来记录一下。
Tags: 计算时间
PHP | 评论:0
| 阅读:24141