接触PHP也已经两个星期了,因为公司的需要不得已从java改到了PHP,想想语言都是相通的学吧就!在这两个星期中大概了解了PHP的基本语法,还参加了一些项目的功能实现,总结一下吧养成这个好的习惯。
1,include、include_once、require、require_once 的区别
include 和 include_once的区别
include 会将你所需要的文件包含进来这样可以避免重复代码,正如include_once 所示的那样文件值导入一次,再倒入之前如果检测到已经导入则不会再倒入。
include 和 require 的区别
require 读入文件会用所读入的内容替换require 的语句,但是include则不会。这也导致了,如果include() 所包含的文件不存在的话,页面下面的代码仍将继续执行,而 require则不会继续执行.
2,zend framework 中 fetcheAll() 和 fetcheAssoc()的区别
fetcheAll() 返回的是一个连续的数组(Fetches all SQL result rows as a sequential array.)但是数组中的是对象。
fetcheAssoc()返回的是一个联合的数组(Fetches all SQL result rows as an associative array.)数组中的还是数组。
例如一下对同一个结果集的返回形式
fecheAssoc():
Array (
[2] => Array ( [id] => 2 [first_name] => zhao [last_name] => liang [user_name] => liang )
[1] => Array ( [id] => 1 [first_name] => zhao [last_name] => liang1 [user_name] => liang1 )
[5] => Array ( [id] => 5 [first_name] => zhao [last_name] => liang3 [user_name] => liang3 )
[4] => Array ( [id] => 4 [first_name] => qian [last_name] => qian [user_name] => qian ) )
fetchAll()
Array (
[0] => stdClass Object ( [id] => 2 [first_name] => zhao [last_name] => liang [user_name] => liang )
[1] => stdClass Object ( [id] => 1 [first_name] => zhao [last_name] => liang1 [user_name] => liang1 )
[2] => stdClass Object ( [id] => 5 [first_name] => zhao [last_name] => liang3 [user_name] => liang3 )
[3] => stdClass Object ( [id] => 4 [first_name] => qian [last_name] => qian [user_name] => qian ) )
3,在PHP中各种数据类型的转换
向字符串的转换
布尔值的TRUE转换成字符串是“1”而FALSE则转换成“”空字符串;
数组转换成字符串“Array”;
Null转换成空字符串。
向数组的转换
对象向数组转换时,各个属性将是数组中的元素;
另外在print_r 格式化输出数组时会将数组的指针移动到数组的末尾,想要移动到数组开头还得用reset()函数来实现;
理解下面两种方法的不同
unset($bool_key);//清楚整个数组中的元素,在下一次添加时从0开始;另外这个函数在删除数组中的元素时不会重新将键值进行排序(array will not be reindexed) array_values()为重新排序函数;
foreach ($bool_key as $i => $value) {
unset($bool_key[$i]);
}//在添加元素时键值在原来的基础上增长(对于数字键)
官方的一个Array的例子有助于对数组的理解
<?php
// Show all errors
error_reporting(E_ALL);
$arr = array('fruit' => 'apple', 'veggie' => 'carrot');
// Correct
print $arr['fruit']; // apple
print $arr['veggie']; // carrot
// Incorrect. This works but also throws a PHP error of level E_NOTICE because
// of an undefined constant named fruit
//
// Notice: Use of undefined constant fruit - assumed 'fruit' in...
print $arr[fruit]; // apple
// This defines a constant to demonstrate what's going on. The value 'veggie'
// is assigned to a constant named fruit.
define('fruit', 'veggie');
// Notice the difference now
print $arr['fruit']; // apple
print $arr[fruit]; // carrot
// The following is okay, as it's inside a string. Constants are not looked for
// within strings, so no E_NOTICE occurs here
print "Hello $arr[fruit]"; // Hello apple
// With one exception: braces surrounding arrays within strings allows constants
// to be interpreted
print "Hello {$arr[fruit]}"; // Hello carrot
print "Hello {$arr['fruit']}"; // Hello apple
// This will not work, and will result in a parse error, such as:
// Parse error: parse error, expecting T_STRING' or T_VARIABLE' or T_NUM_STRING'
// This of course applies to using superglobals in strings as well
print "Hello $arr['fruit']";
print "Hello $_GET['foo']";
// Concatenation is another option
print "Hello " . $arr['fruit']; // Hello apple
?>
布尔值的转换
the boolean FALSE itself
the integer 0 (zero)
the float 0.0 (zero)
the empty string, and the string "0"
an array with zero elements
an object with zero member variables (PHP 4 only)
the special type NULL (including unset variables)
SimpleXML objects created from empty tags