本文目录一览:
- 1、PHP的count(数组)和strlen(字符串)的内部实现。
- 2、PHP出现 unexpected T_STRING 错误,这里的T_STRING什么意思?
- 3、php语句,retval是什么意思
- 4、PHP执行linux系统命令的常用函数使用说明
- 5、php的 strval和string有啥区别?
- 6、PHP怎么从COM组件中获取返回的字符串
PHP的count(数组)和strlen(字符串)的内部实现。
翻了下PHP内核的定义,大概心中也有了答案了
count()和strlen()都是O(1)的时间复杂度
试想一下如果strlen()需要O(N)的复杂度那未免也太慢了,字符串长度起来的话服务器不是要直接挂掉吗
这两个函数都是典型的空间换时间的做法
我们可以先看看zvalue的结构:
typedef union _zvalue_value {
long lval; /* long value */
double dval; /* double value */
struct {
char *val;
int len;
} str;
HashTable *ht; /* hash table value */
zend_object_value obj;
zend_ast *ast;
} zvalue_value;
这里用的是一个联合体,当变量类型是string类型的时候附加存储多了一个len的整型变量,显而易见需要取长度直接利用记录值就可以了,自然就是O(1)
对于count()常用的参数类型应该为数组,对于继承Countable的类暂不作讨论
数组实现方式为Hashtable,直接看看他的结构吧
typedef struct _hashtable {
uint nTableSize; // hash Bucket的大小,最小为8,以2x增长。
uint nTableMask; // nTableSize-1 , 索引取值的优化
uint nNumOfElements; // hash Bucket中当前存在的元素个数,count()函数会直接返回此值
ulong nNextFreeElement; // 下一个数字索引的位置
Bucket *pInternalPointer; // 当前遍历的指针(foreach比for快的原因之一)
Bucket *pListHead; // 存储数组头元素指针
Bucket *pListTail; // 存储数组尾元素指针
Bucket **arBuckets; // 存储hash数组
dtor_func_t pDestructor; // 在删除元素时执行的回调函数,用于资源的释放
zend_bool persistent; //指出了Bucket内存分配的方式。如果persisient为TRUE,则使用操作系统本身的内存分配函数为Bucket分配内存,否则使用PHP的内存分配函数。
unsigned char nApplyCount; // 标记当前hash Bucket被递归访问的次数(防止多次递归)
zend_bool bApplyProtection;// 标记当前hash桶允许不允许多次访问,不允许时,最多只能递归3次
#if ZEND_DEBUG
int inconsistent;
#endif
} HashTable;
count直接获取nNumOfElements大小,所以也是O(1)
补充————————————————
count() 函数的定义在这里
/* {{{ proto int count(mixed var [, int mode])
Count the number of elements in a variable (usually an array) */
PHP_FUNCTION(count)
{
zval *array;
zend_long mode = COUNT_NORMAL;
zend_long cnt;
zval *element;
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_ZVAL(array)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(mode)
ZEND_PARSE_PARAMETERS_END();
switch (Z_TYPE_P(array)) {
case IS_NULL:
php_error_docref(NULL, E_WARNING, “Parameter must be an array or an object that implements Countable”);
RETURN_LONG(0);
break;
case IS_ARRAY:
if (mode != COUNT_RECURSIVE) {
//类型为数组时调用zend内核函数 zend_array_count()
cnt = zend_array_count(Z_ARRVAL_P(array));
} else {
cnt = php_count_recursive(Z_ARRVAL_P(array));
}
RETURN_LONG(cnt);
break;
case IS_OBJECT: {
zval retval;
/* first, we check if the handler is defined */
if (Z_OBJ_HT_P(array)-count_elements) {
RETVAL_LONG(1);
if (SUCCESS == Z_OBJ_HT(*array)-count_elements(array, Z_LVAL_P(return_value))) {
return;
}
}
/* if not and the object implements Countable we call its count() method */
if (instanceof_function(Z_OBJCE_P(array), zend_ce_countable)) {
zend_call_method_with_0_params(array, NULL, NULL, “count”, retval);
if (Z_TYPE(retval) != IS_UNDEF) {
RETVAL_LONG(zval_get_long(retval));
zval_ptr_dtor(retval);
}
return;
}
/* If There’s no handler and it doesn’t implement Countable then add a warning */
php_error_docref(NULL, E_WARNING, “Parameter must be an array or an object that implements Countable”);
RETURN_LONG(1);
break;
}
default:
php_error_docref(NULL, E_WARNING, “Parameter must be an array or an object that implements Countable”);
RETURN_LONG(1);
break;
}
}
如果没有特别指定mode参数为 COUNT_RECURSIVE 的话(即作遍历),跳转到 zend 的数组计数函数 zend_array_count()
#define zend_hash_num_elements(ht) \
(ht)-nNumOfElements
…
…
static uint32_t zend_array_recalc_elements(HashTable *ht)
{
zval *val;
uint32_t num = ht-nNumOfElements;
ZEND_HASH_FOREACH_VAL(ht, val) {
if (Z_TYPE_P(val) == IS_INDIRECT) {
if (UNEXPECTED(Z_TYPE_P(Z_INDIRECT_P(val)) == IS_UNDEF)) {
num–;
}
}
} ZEND_HASH_FOREACH_END();
return num;
}
ZEND_API uint32_t zend_array_count(HashTable *ht)
{
uint32_t num;
if (UNEXPECTED(ht-u.v.flags HASH_FLAG_HAS_EMPTY_IND)) {
num = zend_array_recalc_elements(ht);
if (UNEXPECTED(ht-nNumOfElements == num)) {
ht-u.v.flags = ~HASH_FLAG_HAS_EMPTY_IND;
}
} else if (UNEXPECTED(ht == EG(symbol_table))) {
num = zend_array_recalc_elements(ht);
} else {
num = zend_hash_num_elements(ht);
}
return num;
}
IS_REFERENCE:间接 zval 指的就是其真正的值是存储在其他地方的。注意这个 IS_REFERENCE 类型是不同的,间接 zval 是直接指向另外一个 zval 而不是像 zend_reference 结构体一样嵌入 zval。
只有当数组中有 HASH_FLAG_HAS_EMPTY_IND 这个 flag 时(间接zval)才会对数组进行遍历校验,其他情况下都是直接取 数组(hash table) 里面的 nNumOfElements 的值,答案显而易见了,就是O(1)
PHP出现 unexpected T_STRING 错误,这里的T_STRING什么意思?
是字符串常量的意思,意思就是:在php中,单独的字符串会被认为是常量,但是,当它去找该常量的值的时候没有找到,就会告诉你,未定义的字符串常量!
php语句,retval是什么意思
array() 创建数组,带有键和值。如果在规定数组时省略了键,则生成一个整数键,这个 key 从 0 开始,然后以 1 进行递增。 要用 array() 创建一个关联数组
PHP执行linux系统命令的常用函数使用说明
system函数
说明:执行外部程序并显示输出资料。
语法:string
system(string
command,
int
[return_var]);
返回值:
字符串
详细介绍:
本函数就像是
C
语中的函数
system(),用来执行指令,并输出结果。若是
return_var
参数存在,则执行
command
之后的状态会填入
return_var
中。同样值得注意的是若需要处理用户输入的资料,而又要防止用户耍花招破解系统,则可以使用
EscapeShellCmd()。若
PHP
以模块式的执行,本函数会在每一行输出后自动更新
Web
服务器的输出缓冲暂存区。若需要完整的返回字符串,且不想经过不必要的其它中间的输出界面,可以使用
PassThru()。
实例代码:
复制代码
代码如下:
?php
$last_line
=
system(‘ls’,
$retval);
echo
‘Last
line
of
the
output:
‘
.
$last_line;
echo
‘hr
/Return
value:
‘
.
$retval;
?
exec函数
说明:执行外部程序。
语法:string
exec(string
command,
string
[array],
int
[return_var]);
返回值:
字符串
详细介绍:
本函数执行输入
command
的外部程序或外部指令。它的返回字符串只是外部程序执行后返回的最后一行;若需要完整的返回字符串,可以使用
PassThru()
这个函数。
要是参数
array
存在,command
会将
array
加到参数中执行,若不欲
array
被处理,可以在执行
exec()
之前呼叫
unset()。若是
return_var
跟
array
二个参数都存在,则执行
command
之后的状态会填入
return_var
中。
值得注意的是若需要处理使用者输入的资料,而又要防止使用者耍花招破解系统,则可以使用
EscapeShellCmd()。
实例代码:
复制代码
代码如下:
?php
echo
exec(‘whoami’);
?
popen函数
说明:打开文件。
语法:int
popen(string
command,
string
mode);
返回值:
整数
详细介绍:
本函数执行指令开档,而该文件是用管道方式处理的文件。用本函数打开的文件只能是单向的
(只能读或只能写),而且一定要用
pclose()
关闭。在文件操作上可使用
fgets()、fgetss()
与
fputs()。若是开档发生错误,返回
false
值。
实例代码:
复制代码
代码如下:
?
$fp
=
popen(“/bin/ls”,”r”
);
?
PHP监控linux服务器负载
在实际项目的应用中,我们由于各种条件的现实,利用PHP来实现服务器负载监控将是一种更为灵活的方式。
由于Web
Server以及PHP的实现方式所限,我们在现实环境中很难利用PHP去调用一些Linux中需要root权限才能执行的程序,对此,我从网上找到另外一种方式来绕开这个限制。首先先写个c程序中转调用系统命令,然后用PHP去执行此c程序。
c程序
首先写个c文件,比如/usr/local/ismole/w.c
复制代码
代码如下:
#includestdio.h
#includestdlib.h
#includesystypes.h
#includeunistd.h
int
main()
{
uid_t
uid
,euid;
//note
获得当前的uid
uid
=
getuid();
//note
获得当前euid
euid
=
geteuid();
//note
交换这两个id
if(setreuid(euid,
uid))
perror(“setreuid”);
//note
执行将要执行linux系统命令
system(“/usr/bin/w”);
return0;
}
编译该文件gcc
-o
w
-Wall
w.c,这时会在当前目录下生成程序w。改变此程序的属主chmod
u+s
./w。
PHP执行
文件内容如下,放在web目录下,访问就会输出当前的服务器负载情况。
复制代码
代码如下:
?php
/*
More
Original
PHP
Framwork
Copyright
(c)
2007
–
2008
IsMole
Inc.
$Id:
serverMonitor.php
408
2008-12-02
08:07:40Z
kimi
$
*/
//note
key的验证过程
if($key
!=
$authkey)
{
//
exit(‘key
error);
}
$last_line
=
exec(‘/usr/local/ismole/w’,
$retval);
$returnArray
=
explode(“load
average:
“,
$retval[0]);
$returnString
=
$returnArray[1];
echo
$returnString;
按照上面的实例,我们可以用PHP来做任何我们想执行的Linux系统命令,SVN更新,服务器监控,备份,恢复,日常维护等等。
php的 strval和string有啥区别?
1.表面看着没有区别
2.但是在精度计算上就体现出来了
3.比如 $num = 5.2*100;
4.如果单纯存储数据没有区别 如果你进行数据库运算查询的时候两个就完全有区别了
5.比如你数据库字段上有一个字段类型为int num 值为 520
6.这时你会发现用(string)$num查询是查询不到信息 但是用 strval($num)这时绝对可以查询出来
PHP怎么从COM组件中获取返回的字符串
PHP如何从COM组件中获取返回的字符串?
COM组件中定义一个方法
HRESULT GetStr([in,out] BSTR* vStr, [in,out] LONG* vLen, [out,retval] LONG* vError);
方法实现为:
C/C++ code
STDMETHODIMP MC_MyClass::GetStr(BSTR* vStr, LONG* vLen, LONG* vError) { *vError = 0; if(0 == vStr) return S_FALSE; if(0 == vLen) return S_FALSE; wchar_t tStr[] = L”This is a string from com!”; memcpy(*vStr, tStr, wcslen(tStr)); *vLen = wcslen(tStr); *vError = 1; return S_OK; }
PHP调用该方法
C/C++ code
$tStr = “”; $tLen = 100; $tStr = str_pad($tStr, $tLen, “0”); $tRes = $tCom-GetStr($tStr, $tLen);
结果
$tRes = 1;
$tStr = “”;
$tLen = 26;
原创文章,作者:XYTW,如若转载,请注明出处:https://www.506064.com/n/137192.html