Php学习《五》 ------ 基本知识摘录

[b]1.php中文字符串长度及定长截取问题[/b]

使用str_len("中国") 结果为6,php系统默认一个中文字符长度为3,可改用mb_strlen函数获得长度,mb_substr函数截取字符

mb_strlen($str, "utf-8"); //1汉字为1字符
mb_strlen($str, "gb2312"); //系统会认为1汉字为2字符
mb_strlen($str); //如果没有添加,系统会认为1汉字为3字符

int mb_strlen ( string str [, string encoding] )

string mb_substr ( string str, int start [, int length [, string encoding]] )

[b]2.判断php变量是否定义,是否为空[/b]

if($keyword): 这样的语句如果在controller里没有set 到页面上判断语句会出错,改用表达式 isset($keyword)

表达式 gettype() empty() is_null() isset() boolean : if($x)
$x = ""; string TRUE FALSE TRUE FALSE
$x = null; NULL TRUE TRUE FALSE FALSE
var $x; NULL TRUE TRUE FALSE FALSE
$x is undefined NULL TRUE TRUE FALSE FALSE
$x = array(); array TRUE FALSE TRUE FALSE
$x = false; boolean TRUE FALSE TRUE FALSE
$x = true; boolean FALSE FALSE TRUE TRUE
$x = 1; integer FALSE FALSE TRUE TRUE
$x = 42; integer FALSE FALSE TRUE TRUE
$x = 0; integer TRUE FALSE TRUE FALSE

[b]3.获取request多值参数[/b]

类似java的request.getParameterValues() (居然刚知道这个方法,==!)

页面form中

<input type="hidden" name="kword[]" value="2"/>
<input type="hidden" name="kword[]" value="7"/>
<input type="hidden" name="kword[]" value="895"/>

后台处理请求

$kword=$_POST['kword'];

cakePHP对应方法为

$kword=$this->params['form']['kword'];

使用时按照设置的顺序$kword[index] index: 0-n

[b]4.php solr client api 取doc字段出现index not defined 错误解决方法:[/b]

solr文档可能某些字段不全,当取多个文档显示时,如果有的字段没有定义值会出现index not defined 错误

修改solr client api的Document文件

public function __get($key) {
//key不存在则返回空 避免出现index not defined 错误 shen guanpu 2010年7月15日13:51:52
return array_key_exists($key,$this->_fields)?$this->_fields[$key]:"";
//return $this->_fields[$key]; 原代码}

[b]5. Install CakePHP in a Subdirectory Via an Apache Alias[/b]
httpd.conf

In httpd.conf, add the following line:Alias /directory_name /absolute/path/to/install/directory/app/webroot
.htaccess

In app/webroot/.htaccess, add the following line:RewriteBase /directory_name
Your .htaccess file should now appear as such:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /directory_name
RewriteCond % REQUEST_FILENAME !-d
RewriteCond % REQUEST_FILENAME !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
index.php

Finally, in app/webroot/index.php, at line 63, right below where it says not to edit below this line, change it to: define('WEBROOT_DIR', 'directory_name');
出处
[url]http://www.chriscassell.net/log/2006/07/27/how_to_install_.html[/url]

[b]6. mysql 远程访问设置[/b]

GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY 'password' WITH GRANT OPTION;

FLUSH PRIVILEGES;
[b]
7.PHP 闭合标签[/b]

PHP闭合标签“?>”在PHP中对PHP的分析器是可选的。 但是,如果使用闭合标签,任何由开发者,用户,或者FTP应用程序插入闭合标签后面的空格都有可能会引起多余的输出、php错误、之后的输出无法显示、空白页。因此,所有的php文件应该省略这个php闭合标签,并插入一段注释来标明这是文件的底部并定位这个文件在这个应用的相对路径。这样有利于你确定这个文件已经结束而不是被删节的。

INCORRECT: <?php echo "Here's my code!"; ?>

CORRECT: <?php echo "Here's my code!";

/* End of file myfile.php // Location: ./system/modules/mymodule/myfile.php */

[b]8.php判断数字[/b]

bool is_numeric ( mixed var )

[b]9.mysql IGNORE_SPACE mode[/b]

写concat函数时出现 concat dose not exist错误

更改set sql_mode='IGNORE_SPACE'; 再写concat得到正确结果

mysql workbench字体太小,函数和“(”之间有空格居然没看到。。。

[b]10.php solr 搜索排序[/b]

$response = $this->searchSolr->search( $query, $offset, $limit,array('sort'=>'wiki-recommend desc,wiki-score desc') );

[b]11.字符编码转换[/b]

string mb_convert_encoding ( string str, string to_encoding [, mixed from_encoding] )

[b]12. cakePHP之XP下apache配置[/b]

php.ini文件设置 date.timezone = HongKong 不然cakePHP首页会出现警告

apache httpd.conf配置主要是设置php支持及urlrewrite模块启动

LoadModule php5_module C:/php/php5apache2_2.dll

AddType application/x-httpd-php .php
PHPIniDir "C:/php"

#使用cake php 则去掉下行的注释
LoadModule rewrite_module modules/mod_rewrite.so

<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>

# 配置默认的目录设置 是否允许跳转.
#
<Directory />
Options FollowSymLinks
AllowOverride all
# Order deny,allow
# Deny from all
</Directory>

# This should be changed to whatever you set DocumentRoot to.
#这个目录的配置同上做更改
<Directory "C:\Apache2.2\htdocs">

[b]13.二维数组赋值[/b]

$a1 = array( "a" => 0, "b" => 1 );
$a2 = array( "aa" => 00, "bb" => 11 );
$together = array( $a1, $a2 );
foreach( $together as $single ) {
$single["c" ] = 3 ;
}

这样赋值不会有任何变化,必须如下做法:

foreach( $together as $key => $value ) {
$together[$key]["c"] = 3 ;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值