您现在的位置 » 博客首页 » 后端代码

PHP的几个东西

2009-02-11
记录给自已!这是kohana框架里的几个写法!

1、字符比较:
比较适用于比较字符是否是大小写:

$class = 'Rfdsaffsadfsadfasdfsadf';
$type = ($class[0] < 'a') ? 'libraries' : 'helpers';
echo $type;


echo ('大写A:');
echo      o&#114;d('A');  
echo ('<hr />');
echo ('小写a:');
echo      o&#114;d('a');  


输出是:大写A:65 小写a:97

1、这样的比较应该是先转成ASCII 比较的;
2、如果第一个字符是中文,用$class[0]这样的方式就取不出来了!
3、$class[0]这样的方式是不推荐使用的,我有点忘了,应该是4.0以前的写法,现在应该推荐用$class{0}
4、比较中文时,可以用 o&#114;d(mb_substr($class, 0 , 1, 'utf-8')) > 127 ,这里是取第一个字符再转成ASCII ,再比较,如果大于127的,可能认为是中文;

2、首字母大写

这个和上边这个有点关系,也记一下;

//Make a string's first character uppercase
ucfirst()


手册里的例子是这样的:
<?php
$foo = 'hello world!';
$foo = ucfirst($foo);             // Hello world!
$bar = 'HELLO WORLD!';
$bar = ucfirst($bar);             // HELLO WORLD!
$bar = ucfirst(strtolower($bar)); // Hello world!
?> 


3、自动加载

之前一直好奇,一个类文件还没有require进来呢,怎么就能直接可以用了呢?

//这个函数似乎是5点几以后才支持的;之前的不支持;
spl_autoload_register(array('Kohana', 'auto_load'));

//然后kohana里可以写成类似的:

final class Kohana {
  public static function auto_load($class){
    require $class . '.php';
  }
}


这样,你new Abc(); 只要 Abc.php 这个文件存在,就会直接require进来;

看一眼应该能明白是什么意思吧!
评论:3 条 | 查看:5731 次
dengguibao于2010年07月16日17点02分45秒说
看你的blog的源程序,才学到有自动加载这么一说
你的config.php里面的__autoload硬是看不明白,后来在网上搜索了一下才知道有这个东西
debug于2010年07月03日11点27分50秒说
1.3
Note: String s may also be accessed using braces, as in $str{42}, for the same purpose. However, this syntax is deprecated as of PHP 5.3.0. Use square brackets instead, such as $str[42].
100福特于2009年04月08日8点00分00秒说
看看 顺便也提醒下自己

发布评论/留言

名字/昵称:
内容: