Posts Tagged ‘PHP’

smarty3的一些实用的新特性

October 14th, 2010

表达式

支持更加随意的表达式

{$x+$y}                           输入x和y的和
{$foo = strlen($bar)}             变量支持PHP函数
{assign var=foo value= $x+$y}     属性支持表达式
{$foo = myfunct( ($x+$y)*3 )}     函数参数支持表达式
{$foo[$x+3]}                      数组下表支持表达式


可以在Smarty标签使用其他标签的值
{$foo="this is message {counter}"}

可以在双引号里使用Smarty标签

{$foo="this is message {counter}"}

双引号中可以使用变量,也可以使用Smarty标签

{$foo="this is message $counter"}

{func var="test $foo test"} // 同$foo
{func var="test $foo_bar test"} // 同$foo_bar
{func var="test `$foo[0]` test"} // 同$foo[0]
{func var="test `$foo[bar]` test"} // 同$foo[bar]
{func var="test $foo.bar test"} // 同$foo (而不是$foo.bar)
{func var="test `$foo.bar` test"} // 同$foo.bar
{func var="test `$foo.bar` test"|escape} // 引号外加修饰符
{func var="test {$foo|escape} test"} // 引号内加修饰符
{func var="test {time()} test"} // 调用PHP方法
{func var="test {counter} test"} // 调用插件返回值
{func var="variable foo is {if !$foo}not {/if} defined"} // 调用Smarty block方法

{* 将会替换$tpl_name为对应的值*}
{include file="subdir/$tpl_name.tpl"}
{* 不会替换$tpl_name *}
{include file='subdir/$tpl_name.tpl'} // 变量需要用双引号才可以解析!
{* 用点号"."的话必须加反引号 *}
{cycle values="one,two,`$smarty.config.myval`"}
{* 用点号"."的话必须加反引号 *}
{include file="`$module.contact`.tpl"}
{* 用点号语法可以使用变量 *}
{include file="`$module.$view`.tpl"}

可以在模板里头定义数组

{assign var=foo value=[1,2,3]}
{assign var=foo value=['y'=>'yellow','b'=>'blue']}
{assign var=foo value=[1,[9,8],3]}   数组可以嵌套

简单的变量赋值

{$foo=$bar+2}

可以给指定的数组元素赋值,如果变量存在但不是数组,会先转换成数组,再进行赋值
{$foo['bar']=1}
{$foo['bar']['blar']=1}

同上,可以给数组添加值

{$foo[]=1}

点号.功能更强大:支持变量索引、支持表达式索引、支持嵌套索引
{$foo.a.b.c}        =>  $foo['a']['b']['c']
{$foo.a.$b.c}       =>  $foo['a'][$b]['c']        支持变量索引
{$foo.a.{$b+4}.c}   =>  $foo['a'][$b+4]['c']       支持表达式索引
{$foo.a.{$b.c}}     =>  $foo['a'][$b['c']]         支持嵌套索引

变量名中支持变量

$foo         一个普通的变量
$foo_{$bar}  变量名中包含变量
$foo_{$x+$y} 变量名中可以支持表达式
$foo_{$bar}_buh_{$blar}  变量名包含多个变量
{$foo_{$x}}  如果$x是1,则输出$foo_1

支持对象链,即是对象方法的连续调用,很像jquery

{$object->method1($x)->method2($y)}

{for}标签支持类似loop一样的循环

{for $x=0, $y=count($foo); $x<$y; $x++}  ....  {/for}

在FOR循环中可以通过如下特殊标示符限定位置:

$x@iteration  当前循环次数
$x@total     总循环次数
$x@first  循环第一次
$x@last     循环最后一次

新的foreach语法

{foreach $myarray as $var}...{/foreach}

同样是foreach里头的特殊表示符,看的就明白,不翻译了……

$var@key            foreach $var array key
$var@iteration      foreach current iteration count (1,2,3...)
$var@index          foreach current index count (0,1,2...)
$var@total          foreach $var array total
$var@first          true on first iteration
$var@last           true on last iteration

支持while循环

{while $foo}...{/while}
{while $x lt 10}...{/while}

可以直接使用PHP的函数

{time()}

支持将字符串作为模板显示:使用string的资源类型即可:
$smarty->display('string:This is my template, {$foo}!'); // php
{include file="string:This is my template, {$foo}!"} // template
支持纯php的模板:完全以php语法书写的模板,调用时使用php的资源类型即可:$smarty->display(‘php:foo.php’);
当然也可以{include file=”php:foo.php”},
不过在纯php的模板中是无法调用smarty的modifier/function的,以后的版本也许会考虑封装

模板继承:类似类的继承,可以继承父模板文件,同时重载父模板中相同name的{block}区块内容
静态类,命名空间的支持:可以这样注册一个静态类,其中命名空间可选
$smarty->register->templateClass(‘foo’,'name\name2\myclass’);
在模板中调用:
{foo::method()}

参考:http://www.emptykid.com/blog/archives/133

http://shameerc.wordpress.com/2010/10/06/an-introduction-to-smarty-3/

  • Share/Save/Bookmark

一些开发常用的网站

September 17th, 2010

http://scriptsrc.net/
http://javascriptcompressor.com/
http://www.csscompressor.com/
http://fordinteractive.com/tools/emchart/
http://colorschemedesigner.com/
http://www.xml-sitemaps.com/
http://browize.com/
http://spoon.net/browsers/
http://tools.pingdom.com/
http://www.mysqlformatdate.com/
http://tester.jonasjohn.de/
http://www.htaccesseditor.com/en.shtml
http://www.phpform.org/
http://twitapi.com/explore/

  • Share/Save/Bookmark

PHP version 5.3.0 is released

June 30th, 2009

The PHP development team is proud to announce the immediate release of PHP 5.3.0. This release is a major improvement in the 5.X series, which includes a large number of new features and bug fixes.

The key features of PHP 5.3.0 include:

This release also drops several extensions and unifies the usage of internal APIs. Users should be aware of the following known backwards compatibility breaks:

For users upgrading from PHP 5.2 there is a migration guide available here, detailing the changes between those releases and PHP 5.3.0.

For a full list of changes in PHP 5.3.0, see the ChangeLog.

http://php.net/releases/5_3_0.php

  • Share/Save/Bookmark

php syntax exam

March 3rd, 2009

http://www.blueshoes.org/en/developer/syntax_exam/

在php手册中看到类型比较的时候发现的这个exam,正好测试了下基础,错了几个记录下来

21. $x = (array(’a'=>’foo’) == array(’b'=>’foo’));
what is $x?
you said: TRUE
right is: FALSE
好吧,我以为这两个数组比较时值都是ARRAY的说,其实两个数组在比较时并不是转换成字符进行比较的
array与array的比较,具有较少成员的数组较小,如果运算数 1 中的键不存在于运算数 2 中则数组无法比较,否则挨个值比较

22.$arrOne = array(”=>”);
$arrTwo = array(’9′=>’apple’, ‘15′=>’banana’, ‘20′=>’grapefruit’);
$x = array_merge($arrOne, $arrTwo);
what is $x?
1) array(”=>”, 0=>’apple’, 1=>’banana’, 2=>’grapefruit’);
2) array(”=>”, 9=>’apple’, 15=>’banana’, 20=>’grapefruit’);
you said: 2
right is: 1
具体见array_merge函数
如果只给了一个数组并且该数组是数字索引的,则键名会以连续方式重新索引。
如果你想完全保留原有数组并只想新的数组附加到后面,用 + 运算符。

50. $x = (bool)(”hello” == TRUE);
what is $x?
you said: FALSE
right is: TRUE
result: wrong
恩,比较运算符里有详细说明:
string,resource 或 number与string,resource 或 number比较时, 将字符串和资源转换成数字,按普通数学比较。

59. $a = “hello”;
$b = &$a;
unset($b);
$b = “world”;
what is $a?
you said: world
right is: hello

60. $a = “hello”;
$b = &$a;
unset($b);
what is $a?
you said: null
right is: hello

61. $a = “hello”;
$b = &$a;
$b = “world”;
unset($b);
what is $a?
you said: null
right is: world

好吧,引用后的变量unset不会影响到被引用的变量恩,我一直以为是$b指向$a,实际上是$b和$a指向了同一个地方(即变量内容)而已
在 PHP 中引用意味着用不同的名字访问同一个变量内容。这并不像 C 的指针,替代的是,引用是符号表别名。注意在 PHP 中,变量名和变量内容是不一样的,因此同样的内容可以有不同的名字。
当 unset 一个引用,只是断开了变量名和变量内容之间的绑定。这并不意味着变量内容被销毁了。

66. $a = ‘foo’;
$b = isSet($a['bar']);
what is $b?
you said: FALSE
right is: TRUE
result: wrong
‘bar’ in $a['bar'] evaluates to int 0 (see the PHP Cheat Sheet at http://www.blueshoes.org/en/developer/php_cheat_sheet/). Then it is $a[0] and now each character in the string $a (foo) can be accessed with its char number, like an array. So that is an ‘f’. And that thing is set.
这个我以前是真的没注意。。。记下了

其实对于字符串和字符串的比较,觉得文档上的有点问题,字符串和字符串比较时并不会转换成数字进行比较,只有当其中这两个字符串都为数字字符串时才会作为证书比较。不过对于66中的转换以前还真没注意TAT

  • Share/Save/Bookmark

Flash与PHP交互时不同系统的UA不同

February 26th, 2009

在mac下是Adobe Flash Player

在windows下是shockwave之类

  • Share/Save/Bookmark