PHP小知识总结(1)

   1. mysqli_query — 对数据库执行一次查询 

    失败时返回 FALSE ,通过 mysqli_query() 成功执行SELECT, SHOW, DESCRIBE或 EXPLAIN查询会返回一个mysqli_result 对象,其他查询则返回 TRUE;
   返回一个对象:object(mysqli_result)#2 (5) {
                                                             ["current_field"]=> int(0)
                                                             ["field_count"]=>int(3)
                                                             ["lengths"]=>NULL
                                                             ["num_rows"]=>int(1)‘’
                                                             ["type"]=>int(0)}
     mysqli_fetch_all--->Fetches all result rows as an associative array, a numeric array, or both

                            参数:result

                                        仅以过程化样式:由 mysqli_query() mysqli_store_result()mysqli_use_result() 返回的结果集标识。

             resulttype

                             This optional parameter is a constant indicating what type of array should be produced from the current row data. The possible value for this parameter are the constants MYSQLI_ASSOC , MYSQLI_NUM , or MYSQLI_BOTH .

                             返回值: Returns an array of associative or numeric arrays holding result rows
                               mysqli_fetch_all ( mysqli_result $result [, int $resulttype = MYSQLI_NUM ] )

    mysql_fetch_assoc --->从结果集中取得一行作为关联数组( Fetch a result row as an associative array)

     假如从数据库取出一个用户的用户名和密码

 username
password
test  
   123456


用assoc来取得结果集中的一行,是array([username]=>'test',[password]=>'123456')

也就是结果的数组中的索引是 所查数据库表的字段名。
说明 ---面向对象风格

 

array mysqli_result::fetch_assoc ( void )

过程化风格

array mysqli_fetch_assoc ( mysqli_result $result )

Returns an associative array that corresponds to the fetched row or NULL if there are no more rows.

Note: 此函数返回的字段名大小写敏感

Note: 此函数将 NULL 字段设置为 PHP NULL 值。

     mysqli_fetch_row

                    mysqli_result::fetch_row -- mysqli_fetch_row — Get a result row as an enumerated array

说明

面向对象风格

mixed mysqli_result::fetch_row ( void )

过程化风格

mixed mysqli_fetch_row ( mysqli_result $result )

Fetches one row of data from the result set and returns it as an enumerated array, where each column is stored in an array offset starting from 0 (zero). Each subsequent call to this function will return the next row within the result set, or NULL if there are no more rows. 

2. 字符串转换为数值

    当一个字符串被当作一个数值来取值,其结果和类型如下:     如果该字符串没有包含 '.','e' 或 'E' 并且其数字值在整型的范围之内(由 PHP_INT_MAX 所定义),该字符串将被当成 integer 来取值。其它所有情况下都被作为 float 来取值。 该字符串的开始部分决定了它的值。如果该字符串以合法的数值开始,则使用该数值。否则其值为 0(零)。合法数值由可选的正负号,后面跟着一个或多个数字(可能有小数点),再跟着可选的指数部分。指数部分由 'e' 或 'E' 后面跟着一个或多个数字构成。 

 1 <?php
 2 $foo = 1 + "10.5" ; // $foo is float (11.5)
 3 $foo = 1 + "-1.3e3" ; // $foo is float (-1299)
 4 $foo = 1 + "bob-1.3e3" ; // $foo is integer (1)
 5 $foo = 1 + "bob3" ; // $foo is integer (1)
 6 $foo = 1 + "10 Small Pigs" ; // $foo is integer (11)
 7 $foo = 4 + "10.2 Little Piggies" ; // $foo is float (14.2)
 8 $foo = "10.0 pigs " + 1 ; // $foo is float (11)
 9 $foo = "10.0 pigs " + 1.0 ; // $foo is float (11) 
10 ?>

3.

var_dump — 打印变量的相关信息
此函数显示关于一个或多个表达式的结构信息,包括表达式的类型与值。数组将递归展开值,通过缩进显示其结构。
print_r — 打印关于变量的易于理解的信息。
print_r() 显示关于一个变量的易于理解的信息。如果给出的是 string 、 integer 或 float ,将打印变量值本身。
如果给出的是 array ,将会按照一定格式显示键和元素。 object 与数组类似。
记住, print_r() 将把数组的指针移到最后边。使用 reset() 可让指针回到开始处。

 1 <pre>
 2 <?php
 3 $a = array ( 'a' => 'apple' , 'b' => 'banana' , 'c' => array ( 'x' , 'y' , 'z' ));
 4 print_r ( $a );?></pre
 5 输出:<pre>Array
 6 ([a] => apple
 7 [b] => banana
 8 [c] => Array(
 9 [0] => x
10 [1] => y
11 [2] => z))</pre>

4.

str_replace — 子字符串替换
mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )
该函数返回一个字符串或者数组。该字符串或数组是将 subject 中全部的 search 都被 replace 替换之后的结果。
如果 search 和 replace 为数组,那么 str_replace() 将对 subject 做二者的映射替换。如果 replace 的值的
个数少于 search 的个数,多余的替换将使用空字符串来进行。如果 search 是一个数组而 replace 是一个字符
串,那么 search 中每个元素的替换将始终使用这个字符串。该转换不会改变大小写。 如果 search 和 replace都
是数组,它们的值将会被依次处理。

例子:

1 $vowels = array( "a" , "e" , "i" , "o" , "u" , "A" , "E" , "I" , "O" , "U" );
2 $onlyconsonants = str_replace ( $vowels , "" , "Hello World of PHP" );
3 echo $onlyconsonants;
4 // 赋值: You should eat pizza, beer, and ice cream every day
5 $phrase = "You should eat fruits, vegetables, and fiber every day." ;
6 $healthy = array( "fruits" , "vegetables" , "fiber" );
7 $yummy = array( "pizza" , "beer" , "ice cream" );
8 $newphrase = str_replace ( $healthy , $yummy , $phrase );
原文地址:https://www.cnblogs.com/xuzhudong/p/6748144.html